cry

package
v0.0.96 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package cry provides utilities for cryptography. This library has not been vetted and people are discouraged from using it. Instead, use the cryptography facilities in the Go standard library and/or golang.org/x/crypto

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eql

func Eql(password, hash string) error

Eql performs a constant-time comparison between the password and the hash. The hash ought to have been produced by Hash

func Hash

func Hash(password string) (string, error)

Hash returns the scrypt hash of the password. It is safe to persist the result in your database instead of storing the actual password.

Example
package main

import (
	"fmt"

	"github.com/komuw/ong/cry"
)

func main() {
	password := "my NSA-hard password"
	// it is okay to save hashedPasswd to the database, as an example.
	hashedPasswd, err := cry.Hash(password)
	if err != nil {
		panic(err)
	}

	err = cry.Eql(password, hashedPasswd)
	if err != nil {
		panic(err)
	}

	fmt.Println(hashedPasswd)
}
Output:

Types

type Enc

type Enc struct {
	// contains filtered or unexported fields
}

Enc is an AEAD cipher mode providing authenticated encryption with associated data, ie cipher.AEAD

Use New to get a valid Enc.

func New

func New(secretKey string) Enc

New returns a cipher.AEAD

It panics on error.

It uses scrypt to derive the final key that will be used for encryption.

func (Enc) Decrypt

func (e Enc) Decrypt(encryptedMsg []byte) (decryptedMsg []byte, err error)

Decrypt authenticates and un-encrypts the encryptedMsg using XChaCha20-Poly1305 and returns decrypted bytes.

func (Enc) DecryptDecode

func (e Enc) DecryptDecode(encryptedEncodedMsg string) (plainTextMsg string, err error)

DecryptDecode takes an encryptedEncodedMsg that was generated using Enc.EncryptEncode and returns the original un-encrypted string.

func (Enc) Encrypt

func (e Enc) Encrypt(plainTextMsg string) (encryptedMsg []byte)

Encrypt, encrypts and authenticates(tamper-proofs) the plainTextMsg using XChaCha20-Poly1305 and returns encrypted bytes.

Example
package main

import (
	"github.com/komuw/ong/cry"
)

func main() {
	key := "super-h@rd-Pas1word"
	e := cry.New(key)

	plainTextMsg := "Muziki asili yake - Remmy Ongala." // English: `What is the origin of music by Remmy Ongala`
	encryptedMsg := e.Encrypt(plainTextMsg)
	_ = encryptedMsg

}
Output:

func (Enc) EncryptEncode

func (e Enc) EncryptEncode(plainTextMsg string) (encryptedEncodedMsg string)

EncryptEncode is like Enc.Encrypt except that it returns a string that is encoded using base64.RawURLEncoding

Example
package main

import (
	"fmt"

	"github.com/komuw/ong/cry"
)

func main() {
	key := "super-h@rd-Pas1word"
	e := cry.New(key)

	originalPlainTextMsg := "three little birds."
	encryptedEncodedMsg := e.EncryptEncode(originalPlainTextMsg)

	resultantPlainTextMsg, err := e.DecryptDecode(encryptedEncodedMsg)
	if err != nil {
		panic(err)
	}

	if resultantPlainTextMsg != originalPlainTextMsg {
		panic("something went wrong")
	}

	fmt.Println(resultantPlainTextMsg)

}
Output:

three little birds.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL