Documentation ¶
Overview ¶
Package cryptu contains injectable base64 symmetric encryption wrappers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecryptFromBase64 ¶
DecryptFromBase64 takes a string encoded with EncryptToBase64 unencodes it, decrypts it, and returns the plaintext string using encoding/base64.StdEncoding. cryptKey must be 16, 24, or 32 bytes long.
func EncryptToBase64 ¶
EncryptToBase64 takes a plain text string, encrypts it using AES, and returns the base64 encoded version of the string using encoding/base64.StdEncoding.
cryptKey must be 16, 24, or 32 bytes long
Example ¶
package main import ( "encoding/base64" "fmt" "log" "github.com/clavoie/cryptu" ) func main() { // keys must be 16, 24, or 32 in length key := "zqpf8VWyrUP9j1gC" secret := "sensitive data" encryptedValue, err := cryptu.EncryptToBase64(key, secret) if err != nil { log.Fatal(err) } fmt.Println(encryptedValue == secret) decodedValue, err := base64.StdEncoding.DecodeString(encryptedValue) if err != nil { log.Fatal(err) } fmt.Println(string(decodedValue) == secret) decryptedSecret, err := cryptu.DecryptFromBase64(key, encryptedValue) if err != nil { log.Fatal(err) } fmt.Println(decryptedSecret) }
Output: false false sensitive data
Types ¶
type Base64 ¶
type Base64 interface { // Decrypt decrypts a base64 encoded value and returns the decrypted // value. Decrypt(string) (string, error) // Encrypt encrypts a raw value and returns the encrypted base64 encoded // value. Encrypt(string) (string, error) }
Base64 symmetrically encrypts / decrypts strings to and from base64 encoded values.
Example ¶
package main import ( "encoding/base64" "fmt" "log" "net/http" "github.com/clavoie/cryptu" "github.com/clavoie/di" ) func NewKey() (cryptu.Key, error) { return cryptu.NewStrKey("0dJFIeW64bTgtjTU") } func NewEncoding() (cryptu.Base64Encoding, error) { return cryptu.NewBase64Encoding(base64.StdEncoding) } var defs = []*di.Def{ {NewKey, di.Singleton}, {NewEncoding, di.Singleton}, } func errFn(err *di.ErrResolve, w http.ResponseWriter, r *http.Request) {} func main() { resolver, err := di.NewResolver(errFn, cryptu.NewDiDefs(), defs) if err != nil { log.Fatal(err) } resolver.Invoke(func(encoder cryptu.Base64) { secret := "my secret" encryptedValue, err := encoder.Encrypt(secret) if err != nil { log.Fatal(err) } fmt.Println(encryptedValue == secret) decodedValue, err := base64.StdEncoding.DecodeString(encryptedValue) if err != nil { log.Fatal(err) } fmt.Println(string(decodedValue) == secret) decryptedSecret, err := encoder.Decrypt(encryptedValue) if err != nil { log.Fatal(err) } fmt.Println(decryptedSecret) }) }
Output: false false my secret
func NewBase64 ¶
func NewBase64(cipher Symmetric, encoding Base64Encoding) Base64
NewBase64 returns a new implementation instance of Base64.
type Base64Encoding ¶
type Base64Encoding interface { // Encoding returns the *Encoding value Encoding() *base64.Encoding }
Base64Encoding is an injectable wrapper around the encoding used by Base64.
func NewBase64Encoding ¶
func NewBase64Encoding(encoding *base64.Encoding) (Base64Encoding, error)
NewBase64Encoding returns a new instance of Base64Encoding from an existing encoding.
type Key ¶
type Key interface { // Bytes returns the encryption key byte slice. Bytes() []byte }
Key represents the symmetric encryption key used in operations by this package.
type Symmetric ¶
type Symmetric interface { // Decrypt decrypts a byte slice encrypted with Encrypt() and // returns the unencrypted result. Decrypt([]byte) ([]byte, error) // Encrypt encrypts the byte slice using the underlying // algorithm, returning the encrypted bytes. Encrypt([]byte) ([]byte, error) }
Symmetric represents an implementation of a symmetric encryption algorithm.