Documentation
¶
Overview ¶
Package crypt provides AES-CBC + PKCS#7 encryption with hex-encoded ciphertext output. Designed for storing short secrets (API keys, tokens) at rest in a database column.
Keys must be 16, 24, or 32 bytes long, selecting AES-128, AES-192, or AES-256 respectively. AES-256 is strongly recommended for new code.
The package has zero third-party dependencies and uses crypto/rand for IV generation. The output is hex-encoded for easy storage in TEXT-style columns; switch to base64 if you prefer.
Example:
c, err := crypt.New("32-byte-aes-256-key-go-here-padded")
if err != nil { return err }
enc, _ := c.Encrypt("my-secret")
dec, _ := c.Decrypt(enc) // "my-secret"
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCiphertextNotBlockAligned = errors.New("crypt: ciphertext is not a multiple of the block size")
ErrCiphertextNotBlockAligned is returned when ciphertext (excluding IV) is not a multiple of the AES block size.
var ErrCiphertextTooShort = errors.New("crypt: ciphertext too short")
ErrCiphertextTooShort is returned when ciphertext is smaller than the AES block size — which means it cannot contain the IV.
var ErrInvalidPadding = errors.New("crypt: invalid PKCS#7 padding")
ErrInvalidPadding is returned when PKCS#7 padding cannot be removed because the padding length byte is out of range or inconsistent.
Functions ¶
func DecryptWithKey ¶
DecryptWithKey is the package-level form of Cipher.Decrypt.
func EncryptWithKey ¶
EncryptWithKey is the package-level form of Cipher.Encrypt. Useful for one-shot calls when you don't want to construct a Cipher.
Types ¶
type Cipher ¶
type Cipher struct {
// contains filtered or unexported fields
}
Cipher is a reusable AES-CBC + PKCS#7 cipher pre-keyed at construction.
Cipher is safe for concurrent use — every Encrypt / Decrypt call constructs a fresh cipher.Block from the stored key.