crypto

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package crypto provides secure cryptographic operations for TinyVault. It implements AES-256-GCM for symmetric encryption and Argon2id for key derivation.

Index

Constants

View Source
const (
	// KeySize is the size of AES-256 keys in bytes.
	KeySize = 32

	// NonceSize is the size of GCM nonces in bytes.
	NonceSize = 12

	// TagSize is the size of GCM authentication tags in bytes.
	TagSize = 16

	// SaltSize is the size of salts for key derivation in bytes.
	SaltSize = 16

	// Argon2Time is the time parameter for Argon2id.
	Argon2Time = 3

	// Argon2Memory is the memory parameter for Argon2id in KiB.
	Argon2Memory = 64 * 1024

	// Argon2Threads is the parallelism parameter for Argon2id.
	Argon2Threads = 4
)
View Source
const PasswordHashSize = 32

PasswordHashSize is the size of the derived key for password hashing.

Variables

View Source
var (
	// ErrInvalidKeySize is returned when a key has an incorrect size.
	ErrInvalidKeySize = errors.New("key must be 32 bytes")

	// ErrInvalidCiphertext is returned when ciphertext is malformed.
	ErrInvalidCiphertext = errors.New("ciphertext too short")

	// ErrDecryptionFailed is returned when decryption fails (authentication error).
	ErrDecryptionFailed = errors.New("decryption failed: authentication error")

	// ErrInvalidSaltSize is returned when a salt has an incorrect size.
	ErrInvalidSaltSize = errors.New("salt must be 16 bytes")
)

Functions

func CompareTokens

func CompareTokens(hash1, hash2 []byte) bool

CompareTokens compares two token hashes in constant time. Returns true if they are equal, false otherwise.

func DecodeKey

func DecodeKey(encoded string) ([]byte, error)

DecodeKey decodes a base64-encoded key.

func Decrypt

func Decrypt(key, ciphertext []byte) ([]byte, error)

Decrypt decrypts ciphertext using AES-256-GCM. It expects the nonce to be prepended to the ciphertext.

func DecryptString

func DecryptString(key []byte, ciphertextB64 string) (string, error)

DecryptString decrypts base64-encoded ciphertext and returns the plaintext string.

func DeriveKey

func DeriveKey(password, salt []byte) ([]byte, error)

DeriveKey derives a 32-byte key from a password using Argon2id. The salt must be 16 bytes.

func EncodeKey

func EncodeKey(key []byte) string

EncodeKey encodes a key to base64 for storage/transmission.

func Encrypt

func Encrypt(key, plaintext []byte) ([]byte, error)

Encrypt encrypts plaintext using AES-256-GCM. It generates a random nonce and prepends it to the ciphertext. The result is: nonce (12 bytes) + ciphertext + tag (16 bytes).

func EncryptString

func EncryptString(key []byte, plaintext string) (string, error)

EncryptString encrypts a string and returns base64-encoded ciphertext.

func GenerateKey

func GenerateKey() ([]byte, error)

GenerateKey generates a cryptographically secure random 32-byte key.

func GenerateSalt

func GenerateSalt() ([]byte, error)

GenerateSalt generates a cryptographically secure random 16-byte salt.

func GenerateToken

func GenerateToken(length int) ([]byte, error)

GenerateToken generates a random token of the specified length in bytes. Returns the raw bytes.

func GenerateTokenString

func GenerateTokenString(length int) (string, error)

GenerateTokenString generates a random token and returns it as a base64 string.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword hashes a password using Argon2id and returns a base64-encoded string. The format is: base64(salt || hash) where salt is 16 bytes and hash is 32 bytes.

func HashToken

func HashToken(token []byte) []byte

HashToken creates a SHA-256 hash of a token. Use this to hash tokens before storing them in the database.

func HashTokenString

func HashTokenString(token string) []byte

HashTokenString is a convenience function that hashes a string token.

func VerifyPassword

func VerifyPassword(password, encodedHash string) bool

VerifyPassword verifies a password against a hash created by HashPassword. Returns true if the password matches, false otherwise.

func ZeroBytes

func ZeroBytes(b []byte)

ZeroBytes securely zeros a byte slice. Use this to clear sensitive data from memory when done.

Types

This section is empty.

Jump to

Keyboard shortcuts

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