crypto

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: Unlicense Imports: 16 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 trims leading and trailing Unicode whitespace as defined by strings.TrimSpace, including \r and \n. Use on every passphrase read from the terminal so pasted passphrases and Windows line endings do not break unlock. Intentional leading/trailing spaces in a passphrase are not supported after normalization.

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