uncrypticated

package module
v0.0.0-...-379379c Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2021 License: MIT Imports: 9 Imported by: 0

README

uncrypticated - cryptography in Go made easy as it should be

No dependencies. Small and reliable.

Features

Encrypt/Decrypt bytes
key := PassphraseToKey("some very secure passphrase no hacker can hack")
plainText := []byte("some very secret text to encrypt")
cipherText, err := Encrypt(text, key) // encryption done

// to decrypt back
plainText, err := Decrypt(cipherText, key)
Encrypt/Decrypt strings
key := PassphraseToKey("some very secure passphrase no hacker can hack")
plainText := []byte("some very secret text to encrypt")
cipherText, err := EncryptToString(text, key) // encryption done

// to decrypt back
plainText, err := DecryptFromString(cipherText, key)
Generate random string with a given length
randomString, err := RandomString(16)
Generate/Validate an encrypted token
id := "some user ID or basically any ID you can use"
key := PassphraseToKey("some very secure passphrase no hacker can hack")
token, err := CreateToken(key, id)

// then you can send this token to a user, or publish anywhere
// and of course you can validate it when you get it back
lifetime := 5 * time.Minute // let's say the token is valid for 5 minutes
err := ValidateToken(token, key, id, lifetime)

Can return following errors:

var (
	// ErrTokenInvalid occurs when `ValidateToken` failed to decode the token
	ErrTokenInvalid = errors.New("crypto: invalid token, failed to decode")
	// ErrTokenExpired occurs when `ValidateToken` succeeded
	// to decode the token but it's expired
	ErrTokenExpired = errors.New("crypto: token expired")
	// ErrTokenNoIDMatch occurs when `ValidateToken` succeeded
	// to decode the token but its ID does not match the validation request
	ErrTokenNoIDMatch = errors.New("crypto: token ID does not match")
)

MIT License

Denis Rechkunov denis@rdner.de

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTokenInvalid occurs when `ValidateToken` failed to decode the token
	ErrTokenInvalid = errors.New("crypto: invalid token, failed to decode")
	// ErrTokenExpired occurs when `ValidateToken` succeeded
	// to decode the token but it's expired
	ErrTokenExpired = errors.New("crypto: token expired")
	// ErrTokenNoIDMatch occurs when `ValidateToken` succeeded
	// to decode the token but its ID does not match the validation request
	ErrTokenNoIDMatch = errors.New("crypto: token ID does not match")
	// ErrCipherTooShort occurs when `Decrypt` does not
	// have input of enough length to decrypt using AES256
	ErrCipherTooShort = errors.New("crypto: cipher plainText is too short for AES encryption")
)

Functions

func CreateToken

func CreateToken(key []byte, id string) (token string, err error)

CreateToken creates an encrypted token that can be validated later. `id` parameter can be any identifier and it's used to validate the token source later in the `ValidateToken` function. It can be an email, user ID, form name (for CSRF tokens), etc.

func Decrypt

func Decrypt(cipherText, key []byte) (decrypted []byte, err error)

Decrypt decrypts content with a key using AES256

func DecryptFromString

func DecryptFromString(cipherTextStr string, key []byte) (decrypted []byte, err error)

DecryptFromString decrypts a string with a key

func Encrypt

func Encrypt(plainText, key []byte) (encrypted []byte, err error)

Encrypt encrypts content with a key using AES256

func EncryptToString

func EncryptToString(plainText, key []byte) (string, error)

EncryptToString encrypts content with a key using AES256 and encodes it to a hexadecimal string

func PassphraseToKey

func PassphraseToKey(passphrase string) (key []byte)

PassphraseToKey converts a string to a key for encryption

func RandomString

func RandomString(length int) (string, error)

RandomString generates a random string of the specified length

func ValidateToken

func ValidateToken(token string, key []byte, id string, lifetime time.Duration) error

ValidateToken validates the encrypted token. `id` must be a string which was used in `CreateToken` earlier

Types

This section is empty.

Jump to

Keyboard shortcuts

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