crypt

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package crypt defines pluggable encryption and key-derivation interfaces. Implementations can swap in NACL, AES-GCM (FIPS 140), or any AEAD primitive without touching keeper internals.

Index

Constants

This section is empty.

Variables

View Source
var ErrDecrypt = errors.New("decryption failed")

ErrDecrypt is returned when authenticated decryption fails.

Functions

This section is empty.

Types

type Argon2KDF

type Argon2KDF struct {
	Time        uint32
	Memory      uint32
	Parallelism uint8
}

Argon2KDF implements KDF using Argon2id — the recommended password-hashing algorithm for new systems (winner of the Password Hashing Competition).

To meet FIPS 140 requirements swap this for a PBKDF2-SHA-256 implementation that satisfies the same KDF interface.

func DefaultArgon2KDF

func DefaultArgon2KDF() *Argon2KDF

DefaultArgon2KDF returns a KDF with sensible interactive defaults. Tune for your threat model: higher Time/Memory → slower but stronger.

func (*Argon2KDF) DeriveKey

func (a *Argon2KDF) DeriveKey(password, salt []byte, keyLen int) ([]byte, error)

DeriveKey implements KDF.

type Cipher

type Cipher interface {
	// Encrypt returns authenticated ciphertext for plaintext.
	// The implementation is responsible for generating and prepending a nonce.
	Encrypt(plaintext []byte) ([]byte, error)

	// Decrypt recovers plaintext from ciphertext produced by Encrypt.
	// Returns ErrDecrypt on authentication failure or truncation.
	Decrypt(ciphertext []byte) ([]byte, error)
}

Cipher is the encryption contract used throughout keeper. Any authenticated-encryption scheme (XChaCha20-Poly1305, AES-256-GCM, NaCl secretbox, …) must satisfy this interface.

type KDF

type KDF interface {
	// DeriveKey derives a key of keyLen bytes from password and salt.
	DeriveKey(password, salt []byte, keyLen int) ([]byte, error)
}

KDF is the key-derivation contract. Swap between scrypt, Argon2id, PBKDF2, or an HSM-backed KDF without changing caller code.

type ScryptKDF

type ScryptKDF struct {
	// N is the CPU/memory cost parameter. Must be a power of 2 (≥2).
	N int
	// R is the block size parameter.
	R int
	// P is the parallelisation parameter.
	P int
}

ScryptKDF implements KDF using scrypt. It exists solely for backward compatibility with databases that were created with scrypt-derived keys. New deployments should use DefaultArgon2KDF instead.

Recommended migration path: Open the existing database with a ScryptKDF in Config.KDF. Call Rotate with the same passphrase — this re-derives with Argon2id and

re-encrypts all secrets in one atomic operation.

Replace ScryptKDF with DefaultArgon2KDF in your Config going forward.

func DefaultScryptKDF

func DefaultScryptKDF() *ScryptKDF

DefaultScryptKDF returns a ScryptKDF with the same defaults that the old keeper hard-coded (N=32768, r=8, p=1).

func (*ScryptKDF) DeriveKey

func (s *ScryptKDF) DeriveKey(password, salt []byte, keyLen int) ([]byte, error)

DeriveKey implements KDF.

type XChacha20Cipher

type XChacha20Cipher struct {
	// contains filtered or unexported fields
}

XChacha20Cipher implements Cipher using XChaCha20-Poly1305. It is the default cipher for keeper.

func NewCipher

func NewCipher(secret string) (*XChacha20Cipher, error)

NewCipher creates an XChacha20Cipher from a human-readable secret. It first tries to decode secret as standard Base64; if that fails or the decoded key is not exactly 32 bytes it hashes the string with SHA-256 to produce a 32-byte key.

func NewCipherFromKey

func NewCipherFromKey(key []byte) (*XChacha20Cipher, error)

NewCipherFromKey creates an XChacha20Cipher directly from a 32-byte key.

func (*XChacha20Cipher) Decrypt

func (c *XChacha20Cipher) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt implements Cipher.

func (*XChacha20Cipher) Encrypt

func (c *XChacha20Cipher) Encrypt(plaintext []byte) ([]byte, error)

Encrypt implements Cipher. A random nonce is prepended to the ciphertext.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL