crypto

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package crypto provides age-based encryption functionality for OpenPass. It supports encryption with X25519 recipients, passphrase-based encryption, and multi-recipient encryption for sharing vault entries.

Index

Constants

View Source
const MaxPasswordLength = 1024

MaxPasswordLength is the upper bound for generated password length.

Variables

View Source
var (
	ErrNilRecipient     = errors.New("recipient is nil")
	ErrNilIdentity      = errors.New("identity is nil")
	ErrNoRecipients     = errors.New("no recipients provided")
	ErrInvalidKeyFormat = errors.New("invalid key format")
	ErrDecryptionFailed = errors.New("decryption failed")
	ErrEmptyPlaintext   = errors.New("plaintext is empty")
	ErrEmptyCiphertext  = errors.New("ciphertext is empty")
)

Common errors for crypto operations

Functions

func Decrypt

func Decrypt(ciphertext []byte, identity *age.X25519Identity) ([]byte, error)

Decrypt decrypts ciphertext using the provided identity. Returns the decrypted plaintext or an error if decryption fails.

func DecryptWithPassphrase

func DecryptWithPassphrase(ciphertext []byte, passphrase string) ([]byte, error)

DecryptWithPassphrase decrypts ciphertext using a passphrase. The passphrase must match the one used during encryption.

func Encrypt

func Encrypt(plaintext []byte, recipient *age.X25519Recipient) ([]byte, error)

Encrypt encrypts plaintext for a single recipient. Returns the encrypted ciphertext or an error if encryption fails.

func EncryptWithPassphrase

func EncryptWithPassphrase(plaintext []byte, passphrase string) ([]byte, error)

EncryptWithPassphrase encrypts plaintext using a passphrase. The passphrase is used to derive a scrypt-based recipient. This is useful for encrypting data that should be decryptable with a password.

func EncryptWithRecipients

func EncryptWithRecipients(plaintext []byte, recipients ...*age.X25519Recipient) ([]byte, error)

EncryptWithRecipients encrypts plaintext for multiple recipients. All provided recipients will be able to decrypt the resulting ciphertext. Returns an error if no recipients are provided or if any recipient is nil.

func GenerateIdentity

func GenerateIdentity() (*age.X25519Identity, error)

GenerateIdentity generates a new age X25519 identity. Returns the generated identity or an error if generation fails.

func GenerateIdentityString

func GenerateIdentityString() (string, error)

GenerateIdentityString generates a new age identity and returns it as a string.

func GeneratePassword

func GeneratePassword(length int, useSymbols bool) (string, error)

func GetRecipientFromIdentity

func GetRecipientFromIdentity(identity *age.X25519Identity) (*age.X25519Recipient, error)

GetRecipientFromIdentity extracts the public recipient from an identity.

func IdentityExists

func IdentityExists(path string) bool

IdentityExists checks if an identity file exists at the given path.

func IsValidIdentity

func IsValidIdentity(identityStr string) bool

IsValidIdentity checks if the given string is a valid age identity without returning the parsed identity.

func IsValidRecipient

func IsValidRecipient(recipientStr string) bool

IsValidRecipient checks if the given string is a valid age recipient without returning the parsed recipient.

func LoadIdentity

func LoadIdentity(path string, passphrase string) (*age.X25519Identity, error)

LoadIdentity loads and decrypts an identity from a file using a passphrase. Returns the decrypted identity or an error if loading/decryption fails.

func ParseRecipients

func ParseRecipients(recipientStrs []string) ([]*age.X25519Recipient, error)

ParseRecipients parses a slice of recipient strings into X25519Recipient objects. Returns an error if any recipient string is invalid.

func RecipientsToStrings

func RecipientsToStrings(recipients []*age.X25519Recipient) []string

RecipientsToStrings converts a slice of X25519Recipient to their string representations.

func SaveIdentity

func SaveIdentity(id *age.X25519Identity, path string, passphrase string) error

SaveIdentity encrypts and saves an identity to a file using a passphrase. The identity is encrypted with scrypt before being written to disk. The file permissions are set to 0o600 (readable/writable by owner only).

func SetScryptWorkFactorForTests

func SetScryptWorkFactorForTests(workFactor int) func()

SetScryptWorkFactorForTests overrides the scrypt work factor for identities created in tests and returns a restore function.

func ValidateIdentity

func ValidateIdentity(identityStr string) (*age.X25519Identity, error)

ValidateIdentity validates that the given string is a valid age identity. A valid identity starts with "AGE-SECRET-KEY-1" and contains valid bech32 encoding.

func ValidateRecipient

func ValidateRecipient(recipientStr string) (*age.X25519Recipient, error)

ValidateRecipient validates that the given string is a valid age recipient. A valid recipient starts with "age1" and contains valid bech32 encoding.

func ValidateTOTPParams added in v1.1.0

func ValidateTOTPParams(algorithm string, digits, period int) error

ValidateTOTPParams enforces RFC 6238 bounds on TOTP parameters. Empty algorithm and zero digits/period are accepted (used to detect if values need to be set). This prevents DoS/overflow from malformed entries.

func ValidateTOTPSecret added in v1.3.0

func ValidateTOTPSecret(secret string) error

Types

type TOTPCode

type TOTPCode struct {
	ExpiresAt time.Time
	Code      string
	Period    int
}

TOTPCode represents a generated TOTP code with metadata

func GenerateTOTP

func GenerateTOTP(secret string, algorithm string, digits int, period int) (*TOTPCode, error)

GenerateTOTP generates a TOTP code from the given secret and configuration This is a standard TOTP implementation per RFC 6238

Jump to

Keyboard shortcuts

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