encryption

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidKey        = errors.New("encryption: key must be 16, 24, or 32 bytes (AES-128/192/256)")
	ErrDecryptFailed     = errors.New("encryption: decryption failed — ciphertext tampered or wrong key")
	ErrInvalidCiphertext = errors.New("encryption: ciphertext too short")
)

Functions

func GenerateKey

func GenerateKey(size int) (string, error)

GenerateKey generates a random key of the given byte length (16/24/32) and returns it hex-encoded.

func GenerateKey256

func GenerateKey256() (string, error)

GenerateKey256 generates a random 32-byte AES-256 key (hex-encoded).

Types

type Encrypter

type Encrypter struct {
	// contains filtered or unexported fields
}

Encrypter provides AES-256-GCM authenticated encryption.

func MustNew

func MustNew(key string) *Encrypter

MustNew is like New but panics on error.

func New

func New(key string) (*Encrypter, error)

New creates an Encrypter. key must be 16/24/32 bytes. You can pass a hex-encoded or base64-encoded key — it will be decoded automatically.

func (*Encrypter) Decrypt

func (e *Encrypter) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt decrypts data produced by Encrypt.

func (*Encrypter) DecryptString

func (e *Encrypter) DecryptString(encoded string) (string, error)

DecryptString decrypts a base64-encoded ciphertext produced by EncryptString.

func (*Encrypter) Encrypt

func (e *Encrypter) Encrypt(plaintext []byte) ([]byte, error)

Encrypt encrypts plaintext and returns nonce+ciphertext (raw bytes).

func (*Encrypter) EncryptDeterministicUNSAFE

func (e *Encrypter) EncryptDeterministicUNSAFE(plaintext []byte) ([]byte, error)

EncryptDeterministicUNSAFE encrypts with a zero nonce (same input → same output). SECURITY WARNING: Using AES-GCM with a zero nonce is cryptographically unsafe as it leaks information about identical plaintexts and enables key recovery attacks. DO NOT use for general-purpose encryption; only use when deterministic ciphertexts are strictly required (e.g. for exact match queries on searchable columns).

func (*Encrypter) EncryptString

func (e *Encrypter) EncryptString(plaintext string) (string, error)

EncryptString encrypts a string and returns a base64-encoded ciphertext.

Jump to

Keyboard shortcuts

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