crypto

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: Unlicense Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrWrongPrivateKeyPassphrase = errors.New("wrong passphrase")

ErrWrongPrivateKeyPassphrase is returned when the passphrase does not decrypt the stored key blob.

Functions

func DecodePublicKey

func DecodePublicKey(data []byte) (*[32]byte, error)

DecodePublicKey decodes a base64-encoded public key.

func DecodePublicKeyString

func DecodePublicKeyString(s string) (*[32]byte, error)

DecodePublicKeyString decodes a base64-encoded public key string.

func DecryptPrivateKey

func DecryptPrivateKey(data []byte, passphrase string) (*[32]byte, error)

DecryptPrivateKey decrypts a private key blob produced by EncryptPrivateKey.

func DerivePublicKey added in v0.1.1

func DerivePublicKey(priv *[32]byte) *[32]byte

DerivePublicKey returns the X25519 public key for the given private key.

func EncodePublicKey

func EncodePublicKey(pub *[32]byte) []byte

EncodePublicKey encodes a public key to base64 bytes for storage.

func EncodePublicKeyString

func EncodePublicKeyString(pub *[32]byte) string

EncodePublicKeyString encodes a public key to a base64 string.

func EncryptPrivateKey

func EncryptPrivateKey(privKey *[32]byte, passphrase string) ([]byte, error)

EncryptPrivateKey encrypts a private key with a passphrase using Argon2id + NaCl secretbox. Returns: salt (16 bytes) || nonce (24 bytes) || secretbox ciphertext

func GenerateKeyPair

func GenerateKeyPair() (priv *[32]byte, pub *[32]byte, err error)

GenerateKeyPair creates a new X25519 keypair.

func HasLocalKeys

func HasLocalKeys() bool

HasLocalKeys returns true if local keys exist.

func LoadPrivateKey

func LoadPrivateKey() (*[32]byte, error)

LoadPrivateKey loads the private key from local storage.

func LoadPublicKey

func LoadPublicKey() (*[32]byte, error)

LoadPublicKey loads the public key from local storage.

func NormalizePassphrase added in v0.1.1

func NormalizePassphrase(s string) string

NormalizePassphrase prepares a passphrase read from the terminal for use with Argon2 and storage. It:

  1. Trims leading/trailing Unicode whitespace (strings.TrimSpace).
  2. Strips a leading UTF-8 BOM (U+FEFF) if present (not removed by TrimSpace).
  3. Applies Unicode NFC so the same visual string yields the same UTF-8 bytes across OS/IME normalization differences (NFD vs NFC).

Intentional leading/trailing spaces in a passphrase are not supported. Rare passphrases that depend on a specific non-NFC byte sequence for non-ASCII characters may need passphrase rotate after upgrading.

func PassphraseFingerprint added in v0.1.5

func PassphraseFingerprint(passphrase string) (length int, sha256Hex string)

PassphraseFingerprint returns the UTF-8 byte length and hex-encoded SHA-256 of the passphrase bytes (after the caller has applied NormalizePassphrase).

func PrintBlobFingerprint added in v0.1.8

func PrintBlobFingerprint(label string, data []byte)

PrintBlobFingerprint writes length and SHA-256 of an arbitrary byte slice to stderr. Use to verify encrypted key blobs match between write (recover/rotate) and read (login).

func PrintPassphraseFingerprint added in v0.1.5

func PrintPassphraseFingerprint(label string, passphrase string)

PrintPassphraseFingerprint writes length and SHA-256 of the passphrase to stderr for cross-machine verification without printing the secret.

func PromptPassphrase

func PromptPassphrase(create bool) (string, error)

PromptPassphrase prompts the user for a passphrase via the terminal. If create is true, asks for confirmation and validates the passphrase.

func PromptPassphraseCreate added in v0.1.1

func PromptPassphraseCreate(primaryPrompt, confirmPrompt string) (string, error)

PromptPassphraseCreate prompts for a new passphrase with validation and confirmation. Empty primaryPrompt / confirmPrompt use defaults "Vault passphrase: " and "Confirm passphrase: ".

func PromptPassphraseUnlock added in v0.1.1

func PromptPassphraseUnlock(prompt string) (string, error)

PromptPassphraseUnlock prompts once for the vault passphrase (normalized). If prompt is empty, uses "Vault passphrase: ".

func PublicKeysEqual added in v0.1.1

func PublicKeysEqual(a, b *[32]byte) bool

PublicKeysEqual returns true if a and b are the same 32-byte X25519 public key.

func SaveKeyPair

func SaveKeyPair(priv *[32]byte, pub *[32]byte) error

SaveKeyPair saves the keypair to ~/.config/vaultenv/keys/

func ValidatePassphrase

func ValidatePassphrase(passphrase string) error

ValidatePassphrase checks that a passphrase meets the minimum requirements: - at least 12 characters - at least one uppercase letter - at least one digit - at least one special character

Types

type Engine

type Engine interface {
	EncryptForRecipients(plaintext []byte, recipients map[string][32]byte) (ciphertext []byte, envelopes map[string]vault.Envelope, err error)
	DecryptWithEnvelope(ciphertext []byte, envelope vault.Envelope, privateKey *[32]byte) (plaintext []byte, err error)
	WrapKeyForRecipient(symKey *[32]byte, recipientPub *[32]byte) (vault.Envelope, error)
	UnwrapKey(envelope vault.Envelope, privateKey *[32]byte) (*[32]byte, error)
}

Engine defines the encryption/decryption interface. Implementations can use different crypto backends (NaCl, age, KMS, etc.).

type NaClEngine

type NaClEngine struct{}

func NewNaClEngine

func NewNaClEngine() *NaClEngine

func (*NaClEngine) DecryptWithEnvelope

func (n *NaClEngine) DecryptWithEnvelope(ciphertext []byte, envelope vault.Envelope, privateKey *[32]byte) ([]byte, error)

func (*NaClEngine) EncryptForRecipients

func (n *NaClEngine) EncryptForRecipients(plaintext []byte, recipients map[string][32]byte) ([]byte, map[string]vault.Envelope, error)

func (*NaClEngine) UnwrapKey

func (n *NaClEngine) UnwrapKey(envelope vault.Envelope, privateKey *[32]byte) (*[32]byte, error)

func (*NaClEngine) WrapKeyForRecipient

func (n *NaClEngine) WrapKeyForRecipient(symKey *[32]byte, recipientPub *[32]byte) (vault.Envelope, error)

Jump to

Keyboard shortcuts

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