Documentation
¶
Overview ¶
Package crypt provides small, hard-to-misuse helpers for encrypting and decrypting data with well-established cryptographic primitives, wrapping the standard library and golang.org/x/crypto.
It offers:
- AES-GCM authenticated encryption (AES-128/192/256);
- ChaCha20-Poly1305 (96-bit nonce) and XChaCha20-Poly1305 (192-bit nonce) AEAD;
- RSA-OAEP public-key encryption with SHA-256 or SHA-512;
- Base64 encoding helpers (standard, raw, and URL alphabets).
Symmetric API conventions ¶
Each symmetric cipher exposes a consistent set of functions: a string form (for example EncryptAesGcm) and a byte form (EncryptByteAesGcm), and each of those has a plain variant that returns the nonce separately plus a WithNonceAppended variant that prepends the freshly generated nonce to the ciphertext, so the whole message can be stored as a single value. Every encryption generates its nonce with crypto/rand, and every decryption authenticates the ciphertext, returning an error on tampering or a wrong key.
The package does not derive keys. Callers pass a key of the correct length (AES accepts 16, 24, or 32 bytes; ChaCha20 and XChaCha20 require 32) and should derive keys from passwords with a KDF such as Argon2id.
Public-key and Base64 ¶
RSA-OAEP and the Base64 helpers are methods on the Encoder and Decoder types, which are built from PEM-encoded keys with NewEncoder and NewDecoder.
Higher-level scheme ¶
For envelope encryption with a key hierarchy and a rotatable secret (useful for protecting many records at rest), see the subpackage github.com/pilinux/crypt/envelope.
Index ¶
- func DecryptAesGcm(key, nonce, ciphertext []byte) (text string, err error)
- func DecryptAesGcmWithNonceAppended(key, ciphertext []byte) (text string, err error)
- func DecryptByteAesGcm(key, nonce, ciphertext []byte) (plaintext []byte, err error)
- func DecryptByteAesGcmWithNonceAppended(key, ciphertext []byte) (plaintext []byte, err error)
- func DecryptByteChacha20poly1305(key, nonce, ciphertext []byte) (plaintext []byte, err error)
- func DecryptByteChacha20poly1305WithNonceAppended(key, ciphertext []byte) (plaintext []byte, err error)
- func DecryptByteChacha20poly1305WithNonceAppendedAAD(key, ciphertext, additionalData []byte) (plaintext []byte, err error)
- func DecryptByteXChacha20poly1305(key, nonce, ciphertext []byte) (plaintext []byte, err error)
- func DecryptByteXChacha20poly1305WithNonceAppended(key, ciphertext []byte) (plaintext []byte, err error)
- func DecryptByteXChacha20poly1305WithNonceAppendedAAD(key, ciphertext, additionalData []byte) (plaintext []byte, err error)
- func DecryptChacha20poly1305(key, nonce, ciphertext []byte) (text string, err error)
- func DecryptChacha20poly1305WithNonceAppended(key, ciphertext []byte) (text string, err error)
- func DecryptXChacha20poly1305(key, nonce, ciphertext []byte) (text string, err error)
- func DecryptXChacha20poly1305WithNonceAppended(key, ciphertext []byte) (text string, err error)
- func EncryptAesGcm(key []byte, text string) (ciphertext []byte, nonce []byte, err error)
- func EncryptAesGcmWithNonceAppended(key []byte, text string) (ciphertext []byte, err error)
- func EncryptByteAesGcm(key []byte, input []byte) (ciphertext []byte, nonce []byte, err error)
- func EncryptByteAesGcmWithNonceAppended(key []byte, input []byte) (ciphertext []byte, err error)
- func EncryptByteChacha20poly1305(key []byte, input []byte) (ciphertext []byte, nonce []byte, err error)
- func EncryptByteChacha20poly1305WithNonceAppended(key []byte, input []byte) (ciphertext []byte, err error)
- func EncryptByteChacha20poly1305WithNonceAppendedAAD(key, input, additionalData []byte) (ciphertext []byte, err error)
- func EncryptByteXChacha20poly1305(key []byte, input []byte) (ciphertext []byte, nonce []byte, err error)
- func EncryptByteXChacha20poly1305WithNonceAppended(key []byte, input []byte) (ciphertext []byte, err error)
- func EncryptByteXChacha20poly1305WithNonceAppendedAAD(key, input, additionalData []byte) (ciphertext []byte, err error)
- func EncryptChacha20poly1305(key []byte, text string) (ciphertext []byte, nonce []byte, err error)
- func EncryptChacha20poly1305WithNonceAppended(key []byte, text string) (ciphertext []byte, err error)
- func EncryptXChacha20poly1305(key []byte, text string) (ciphertext []byte, nonce []byte, err error)
- func EncryptXChacha20poly1305WithNonceAppended(key []byte, text string) (ciphertext []byte, err error)
- type Decoder
- func (d *Decoder) DecryptByteRSA(ciphertext []byte) (plaintext []byte, err error)
- func (d *Decoder) DecryptRSA(ciphertext []byte) (text string, err error)
- func (d *Decoder) FromBase64RawStd(text string) ([]byte, error)
- func (d *Decoder) FromBase64RawURL(text string) ([]byte, error)
- func (d *Decoder) FromBase64Std(text string) ([]byte, error)
- func (d *Decoder) FromBase64URL(text string) ([]byte, error)
- type Encoder
- func (e *Encoder) EncryptByteRSA(input []byte) (ciphertext []byte, err error)
- func (e *Encoder) EncryptRSA(text string) (ciphertext []byte, err error)
- func (e *Encoder) ToBase64RawStd(text []byte) string
- func (e *Encoder) ToBase64RawURL(text []byte) string
- func (e *Encoder) ToBase64Std(text []byte) string
- func (e *Encoder) ToBase64URL(text []byte) string
- type HashAlgorithm
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecryptAesGcm ¶
DecryptAesGcm decrypts and authenticates the given message with AES in GCM mode using the given 128, 192 or 256-bit key and 96-bit nonce.
func DecryptAesGcmWithNonceAppended ¶ added in v0.0.10
DecryptAesGcmWithNonceAppended decrypts and authenticates the given ciphertext with AES in GCM mode using the given 128, 192 or 256-bit key. It expects the ciphertext along with the nonce [ciphertext = nonce + ciphertext].
func DecryptByteAesGcm ¶ added in v0.0.21
DecryptByteAesGcm decrypts and authenticates the given message with AES in GCM mode using the given 128, 192 or 256-bit key and 96-bit nonce.
func DecryptByteAesGcmWithNonceAppended ¶ added in v0.0.21
DecryptByteAesGcmWithNonceAppended decrypts and authenticates the given ciphertext with AES in GCM mode using the given 128, 192 or 256-bit key. It expects the ciphertext along with the nonce [ciphertext = nonce + ciphertext].
func DecryptByteChacha20poly1305 ¶ added in v0.0.11
DecryptByteChacha20poly1305 decrypts and authenticates the given ciphertext with ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce.
func DecryptByteChacha20poly1305WithNonceAppended ¶ added in v0.0.11
func DecryptByteChacha20poly1305WithNonceAppended(key, ciphertext []byte) (plaintext []byte, err error)
DecryptByteChacha20poly1305WithNonceAppended decrypts and authenticates the given ciphertext with ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce. It expects the ciphertext along with the nonce [ciphertext = nonce + ciphertext].
func DecryptByteChacha20poly1305WithNonceAppendedAAD ¶ added in v0.0.22
func DecryptByteChacha20poly1305WithNonceAppendedAAD(key, ciphertext, additionalData []byte) (plaintext []byte, err error)
DecryptByteChacha20poly1305WithNonceAppendedAAD decrypts and authenticates the given ciphertext with ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce, verifying additionalData (AAD) against the value supplied at encryption. Decryption fails if the AAD differs. A nil AAD makes this equivalent to DecryptByteChacha20poly1305WithNonceAppended. It expects the ciphertext along with the nonce [ciphertext = nonce + ciphertext].
func DecryptByteXChacha20poly1305 ¶ added in v0.0.11
DecryptByteXChacha20poly1305 decrypts and authenticates the given ciphertext with XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce.
func DecryptByteXChacha20poly1305WithNonceAppended ¶ added in v0.0.11
func DecryptByteXChacha20poly1305WithNonceAppended(key, ciphertext []byte) (plaintext []byte, err error)
DecryptByteXChacha20poly1305WithNonceAppended decrypts and authenticates the given ciphertext with XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce. It expects the ciphertext along with the nonce [ciphertext = nonce + ciphertext].
func DecryptByteXChacha20poly1305WithNonceAppendedAAD ¶ added in v0.0.22
func DecryptByteXChacha20poly1305WithNonceAppendedAAD(key, ciphertext, additionalData []byte) (plaintext []byte, err error)
DecryptByteXChacha20poly1305WithNonceAppendedAAD decrypts and authenticates the given ciphertext with XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce, verifying additionalData (AAD) against the value supplied at encryption. Decryption fails if the AAD differs. A nil AAD makes this equivalent to DecryptByteXChacha20poly1305WithNonceAppended. It expects the ciphertext along with the nonce [ciphertext = nonce + ciphertext].
func DecryptChacha20poly1305 ¶
DecryptChacha20poly1305 decrypts and authenticates the given ciphertext with ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce.
func DecryptChacha20poly1305WithNonceAppended ¶ added in v0.0.10
DecryptChacha20poly1305WithNonceAppended decrypts and authenticates the given ciphertext with ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce. It expects the ciphertext along with the nonce [ciphertext = nonce + ciphertext].
func DecryptXChacha20poly1305 ¶ added in v0.0.10
DecryptXChacha20poly1305 decrypts and authenticates the given ciphertext with XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce.
func DecryptXChacha20poly1305WithNonceAppended ¶ added in v0.0.10
DecryptXChacha20poly1305WithNonceAppended decrypts and authenticates the given ciphertext with XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce. It expects the ciphertext along with the nonce [ciphertext = nonce + ciphertext].
func EncryptAesGcm ¶
EncryptAesGcm encrypts and authenticates the given message (string) with AES in GCM mode using the given 128, 192 or 256-bit key.
func EncryptAesGcmWithNonceAppended ¶ added in v0.0.10
EncryptAesGcmWithNonceAppended encrypts and authenticates the given message (string) with AES in GCM mode using the given 128, 192 or 256-bit key. It appends the ciphertext to the nonce [ciphertext = nonce + ciphertext].
func EncryptByteAesGcm ¶ added in v0.0.21
EncryptByteAesGcm encrypts and authenticates the given message (bytes) with AES in GCM mode using the given 128, 192 or 256-bit key.
func EncryptByteAesGcmWithNonceAppended ¶ added in v0.0.21
EncryptByteAesGcmWithNonceAppended encrypts and authenticates the given message (bytes) with AES in GCM mode using the given 128, 192 or 256-bit key. It appends the ciphertext to the nonce [ciphertext = nonce + ciphertext].
func EncryptByteChacha20poly1305 ¶ added in v0.0.11
func EncryptByteChacha20poly1305(key []byte, input []byte) (ciphertext []byte, nonce []byte, err error)
EncryptByteChacha20poly1305 encrypts and authenticates the given message (bytes) with ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce.
func EncryptByteChacha20poly1305WithNonceAppended ¶ added in v0.0.11
func EncryptByteChacha20poly1305WithNonceAppended(key []byte, input []byte) (ciphertext []byte, err error)
EncryptByteChacha20poly1305WithNonceAppended encrypts and authenticates the given message (bytes) with ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce. It appends the ciphertext to the nonce [ciphertext = nonce + ciphertext].
func EncryptByteChacha20poly1305WithNonceAppendedAAD ¶ added in v0.0.22
func EncryptByteChacha20poly1305WithNonceAppendedAAD(key, input, additionalData []byte) (ciphertext []byte, err error)
EncryptByteChacha20poly1305WithNonceAppendedAAD encrypts and authenticates the given message (bytes) with ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce, and additionally authenticates additionalData (AAD). The AAD is neither encrypted nor included in the output; the identical bytes must be supplied at decryption. A nil AAD makes this equivalent to EncryptByteChacha20poly1305WithNonceAppended. It appends the ciphertext to the nonce [ciphertext = nonce + ciphertext].
func EncryptByteXChacha20poly1305 ¶ added in v0.0.11
func EncryptByteXChacha20poly1305(key []byte, input []byte) (ciphertext []byte, nonce []byte, err error)
EncryptByteXChacha20poly1305 encrypts and authenticates the given message (bytes) with XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce.
func EncryptByteXChacha20poly1305WithNonceAppended ¶ added in v0.0.11
func EncryptByteXChacha20poly1305WithNonceAppended(key []byte, input []byte) (ciphertext []byte, err error)
EncryptByteXChacha20poly1305WithNonceAppended encrypts and authenticates the given message (bytes) with XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce. It appends the ciphertext to the nonce [ciphertext = nonce + ciphertext].
func EncryptByteXChacha20poly1305WithNonceAppendedAAD ¶ added in v0.0.22
func EncryptByteXChacha20poly1305WithNonceAppendedAAD(key, input, additionalData []byte) (ciphertext []byte, err error)
EncryptByteXChacha20poly1305WithNonceAppendedAAD encrypts and authenticates the given message (bytes) with XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce, and additionally authenticates additionalData (AAD). The AAD is neither encrypted nor included in the output; the identical bytes must be supplied at decryption. A nil AAD makes this equivalent to EncryptByteXChacha20poly1305WithNonceAppended. It appends the ciphertext to the nonce [ciphertext = nonce + ciphertext].
func EncryptChacha20poly1305 ¶
EncryptChacha20poly1305 encrypts and authenticates the given message (string) with ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce.
func EncryptChacha20poly1305WithNonceAppended ¶ added in v0.0.10
func EncryptChacha20poly1305WithNonceAppended(key []byte, text string) (ciphertext []byte, err error)
EncryptChacha20poly1305WithNonceAppended encrypts and authenticates the given message (string) with ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce. It appends the ciphertext to the nonce [ciphertext = nonce + ciphertext].
func EncryptXChacha20poly1305 ¶ added in v0.0.10
EncryptXChacha20poly1305 encrypts and authenticates the given message (string) with XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce.
func EncryptXChacha20poly1305WithNonceAppended ¶ added in v0.0.10
func EncryptXChacha20poly1305WithNonceAppended(key []byte, text string) (ciphertext []byte, err error)
EncryptXChacha20poly1305WithNonceAppended encrypts and authenticates the given message (string) with XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce. It appends the ciphertext to the nonce [ciphertext = nonce + ciphertext].
Types ¶
type Decoder ¶
type Decoder struct {
// PriKeyBlock is the decoded PEM block of the private key.
PriKeyBlock *pem.Block
// HashAlg is the hash used by DecryptRSA; the zero value is SHA256.
HashAlg HashAlgorithm
// Err is non-nil when NewDecoder could not decode the private key PEM.
Err error
}
Decoder holds a PEM-decoded RSA private key and is the entry point for Decoder.DecryptRSA and the Base64 decoding helpers.
Construct one with NewDecoder and check Err before use: the constructor reports a bad PEM input on the Err field instead of returning an error.
func NewDecoder ¶
NewDecoder decodes a PEM-encoded RSA private key (a PKCS#8 "PRIVATE KEY" block) and returns a Decoder for it. It never returns nil; if the input is not a valid private-key PEM block, the returned Decoder has its Err field set, so callers should check Err before calling DecryptRSA.
func (*Decoder) DecryptByteRSA ¶ added in v0.0.21
DecryptByteRSA decrypts the given message with RSA-OAEP and using SHA-256 (default) or SHA-512.
func (*Decoder) DecryptRSA ¶
DecryptRSA decrypts the given message with RSA-OAEP and using SHA-256 (default) or SHA-512.
func (*Decoder) FromBase64RawStd ¶
FromBase64RawStd decodes an unpadded Base64 string produced with the standard alphabet (RFC 4648 section 3.2) back into binary data.
func (*Decoder) FromBase64RawURL ¶
FromBase64RawURL decodes an unpadded Base64 string produced with the URL- and filename-safe alphabet (RFC 4648 section 5) back into binary data.
func (*Decoder) FromBase64Std ¶
FromBase64Std decodes a Base64 string produced with the standard alphabet (RFC 4648) back into binary data.
func (*Decoder) FromBase64URL ¶
FromBase64URL decodes a Base64 string produced with the URL- and filename-safe alphabet (RFC 4648 section 5) back into binary data.
type Encoder ¶
type Encoder struct {
// PubKeyBlock is the decoded PEM block of the public key.
PubKeyBlock *pem.Block
// HashAlg is the hash used by EncryptRSA; the zero value is SHA256.
HashAlg HashAlgorithm
// Err is non-nil when NewEncoder could not decode the public key PEM.
Err error
}
Encoder holds a PEM-decoded RSA public key and is the entry point for Encoder.EncryptRSA and the Base64 encoding helpers.
Construct one with NewEncoder and check Err before use: the constructor reports a bad PEM input on the Err field instead of returning an error.
func NewEncoder ¶
NewEncoder decodes a PEM-encoded RSA public key (a "PUBLIC KEY" block) and returns an Encoder for it. It never returns nil; if the input is not a valid public-key PEM block, the returned Encoder has its Err field set, so callers should check Err before calling EncryptRSA.
func (*Encoder) EncryptByteRSA ¶ added in v0.0.21
EncryptByteRSA encrypts the given message (bytes) with RSA-OAEP and using SHA-256 (default) or SHA-512.
func (*Encoder) EncryptRSA ¶
EncryptRSA encrypts the given message (string) with RSA-OAEP and using SHA-256 (default) or SHA-512.
func (*Encoder) ToBase64RawStd ¶
ToBase64RawStd encodes binary data to a Base64 string using the standard alphabet without padding (RFC 4648 section 3.2): the same as ToBase64Std but with the trailing '=' characters omitted.
func (*Encoder) ToBase64RawURL ¶
ToBase64RawURL encodes binary data to a Base64 string using the URL- and filename-safe alphabet without padding (RFC 4648 section 5).
func (*Encoder) ToBase64Std ¶
ToBase64Std encodes binary data to a Base64 string using the standard alphabet (RFC 4648).
func (*Encoder) ToBase64URL ¶
ToBase64URL encodes binary data to a Base64 string using the URL- and filename-safe alphabet (RFC 4648 section 5).
type HashAlgorithm ¶
type HashAlgorithm int
HashAlgorithm selects the hash used by RSA-OAEP in Encoder.EncryptRSA and Decoder.DecryptRSA.
const ( // SHA256 selects SHA-256. It is the default (the zero value). SHA256 HashAlgorithm = iota // SHA512 selects SHA-512. SHA512 )
Directories
¶
| Path | Synopsis |
|---|---|
|
_example
|
|
|
aes
command
Package main - example usage of AES encryption - decryption
|
Package main - example usage of AES encryption - decryption |
|
chacha20poly1305
command
Package main - example usage of chacha20poly1305 encryption - decryption
|
Package main - example usage of chacha20poly1305 encryption - decryption |
|
envelope
command
Package main - example usage of the envelope encryption scheme.
|
Package main - example usage of the envelope encryption scheme. |
|
hashing
command
Package main - example implementation of different hashing algorithms
|
Package main - example implementation of different hashing algorithms |
|
rsa
command
Package main - example usage of RSA encryption - decryption
|
Package main - example usage of RSA encryption - decryption |
|
xchacha20poly1305
command
Package main - example usage of XChaCha20-Poly1305 encryption - decryption
|
Package main - example usage of XChaCha20-Poly1305 encryption - decryption |
|
Package envelope implements a small, self-contained envelope-encryption scheme layered on top of the low-level AEAD primitives in github.com/pilinux/crypt.
|
Package envelope implements a small, self-contained envelope-encryption scheme layered on top of the low-level AEAD primitives in github.com/pilinux/crypt. |
