Documentation
¶
Overview ¶
Package envelope is a tiny, dependency-free AES-256-GCM envelope library - the Go twin of github.com/JacobStephens2/webcrypto-envelope (TypeScript, Web Crypto). The two are wire-compatible: an envelope produced by either implementation opens in the other.
Two patterns, one primitive (AES-256-GCM in a compact "iv:tag:ciphertext" base64 envelope):
- Password vault - derive a key from a password (PBKDF2-SHA-256) and encrypt/decrypt locally. Good for offline-first apps that sync ciphertext the server can't read.
- Sealed share - Seal encrypts under a fresh random key and hands the key back to you. Store the ciphertext anywhere (the server only ever holds ciphertext); deliver the key out-of-band - e.g. in a URL fragment, which browsers never send to the server - and the recipient Opens it. This is the zero-knowledge shareable-link primitive.
Standard library only (crypto/pbkdf2 needs Go 1.24+). Every function returns an error rather than panicking on attacker-controlled input.
Index ¶
- Constants
- Variables
- func Decrypt(envelopeStr string, key []byte) (string, error)
- func DeriveKey(password, saltB64 string, opts *DeriveOptions) ([]byte, error)
- func Encrypt(plaintext string, key []byte) (string, error)
- func Open(envelopeStr, keyB64 string) (string, error)
- func RandomKey() string
- func RandomSalt() string
- func Seal(plaintext string) (key, envelope string, err error)
- type DeriveOptions
Constants ¶
const DefaultIterations = 600_000
DefaultIterations is the PBKDF2 iteration count used when DeriveOptions is nil or its Iterations field is zero. It matches the TypeScript twin.
Variables ¶
var ErrInvalidEnvelope = errors.New("invalid envelope format (expected iv:tag:ciphertext)")
ErrInvalidEnvelope is returned when an envelope is not three base64 parts separated by colons.
Functions ¶
func Decrypt ¶
Decrypt opens an "iv:tag:ciphertext" envelope with a 32-byte key. It returns an error if the envelope is malformed, the key is wrong, or the data was tampered with.
func DeriveKey ¶
func DeriveKey(password, saltB64 string, opts *DeriveOptions) ([]byte, error)
DeriveKey derives a 32-byte AES-256-GCM key from a password and a base64 salt using PBKDF2-SHA-256.
func Encrypt ¶
Encrypt encrypts a UTF-8 string under a 32-byte key. It returns a compact "iv:tag:ciphertext" base64 envelope with a fresh random IV per call.
func RandomKey ¶
func RandomKey() string
RandomKey returns a random base64 256-bit key - e.g. for a sealed, shareable payload.
func RandomSalt ¶
func RandomSalt() string
RandomSalt returns a random base64 salt for password derivation (16 bytes).
func Seal ¶
Seal encrypts plaintext under a fresh random key and returns both.
Store the envelope wherever you like - it is opaque ciphertext, so the storage service can't read it. Deliver the key out-of-band (a URL fragment is ideal: browsers never send the fragment to the server). The recipient calls Open(envelope, key). The storage service is therefore zero-knowledge with respect to the content.
Types ¶
type DeriveOptions ¶
type DeriveOptions struct {
Iterations int
}
DeriveOptions configures DeriveKey. A nil pointer (or zero Iterations) means DefaultIterations.