No dependencies. Small and reliable.
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)
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)
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 [email protected]