cryptutil

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package cryptutil provides AES-256-GCM symmetric encryption and decryption with PBKDF2 key derivation. It supports functional options for iteration count and base64 output encoding.

Index

Constants

View Source
const (
	SaltSize    = 16
	KeySize     = 32 // AES-256
	NonceSize   = 12 // GCM nonce size
	DefaultIter = 100000
)

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(data []byte, password string, opts ...Option) ([]byte, error)

Decrypt decrypts data encrypted by Encrypt using AES-256-GCM with PBKDF2 key derivation. Input should be salt + nonce + ciphertext (raw bytes), or base64-encoded if WithBase64 is set.

func DeriveKey

func DeriveKey(password string, salt []byte, iterations, keySize int) []byte

DeriveKey derives an encryption key from a password and salt using PBKDF2.

func Encrypt

func Encrypt(plaintext []byte, password string, opts ...Option) ([]byte, error)

Encrypt encrypts plaintext using AES-256-GCM with PBKDF2 key derivation. Returns salt + nonce + ciphertext as raw bytes, or base64-encoded if WithBase64 is set.

func GenerateKey

func GenerateKey(size int) (string, error)

GenerateKey generates a random encryption key of the specified size and returns it base64-encoded.

Types

type Option

type Option func(*Options)

Option is a functional option for encryption/decryption

func WithBase64

func WithBase64() Option

WithBase64 enables base64 encoding/decoding

func WithIterations

func WithIterations(n int) Option

WithIterations sets the PBKDF2 iteration count

type Options

type Options struct {
	Iterations int  // PBKDF2 iterations (default 100000)
	Base64     bool // Base64 encode/decode the output/input
}

Options configures encryption/decryption behavior

Jump to

Keyboard shortcuts

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