cryptox

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

README

Crypto

$ go test -bench=. -benchmem ./crypto/...
goos: darwin
goarch: amd64
pkg: github.com/meysam81/x/crypto
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz

Bench 1

BenchmarkAESGCM_Encrypt_1KB-16                    459598              2316 ns/op         442.05 MB/s        2448 B/op          4 allocs/op
BenchmarkAESGCM_Decrypt_1KB-16                    734086              1404 ns/op         729.29 MB/s        2304 B/op          3 allocs/op
BenchmarkAESGCM_Encrypt_1MB-16                      1382            817338 ns/op        1282.92 MB/s     1058065 B/op          4 allocs/op
BenchmarkChaCha20_Encrypt_1KB-16                  640393              2124 ns/op         482.01 MB/s        1200 B/op          3 allocs/op
BenchmarkChaCha20_Decrypt_1KB-16                  925143              1340 ns/op         764.14 MB/s        1056 B/op          2 allocs/op
BenchmarkChaCha20_Encrypt_1MB-16                     888           1366304 ns/op         767.45 MB/s     1056819 B/op          3 allocs/op
BenchmarkArgon2id_Encrypt_Default-16                  18          62727618 ns/op        67121875 B/op         96 allocs/op
BenchmarkArgon2id_Decrypt_Default-16                  18          62887576 ns/op        67120728 B/op         93 allocs/op
BenchmarkArgon2id_Encrypt_HighSecurity-16              5         205374221 ns/op        134232331 B/op       136 allocs/op
BenchmarkPBKDF2_Encrypt_Default-16                     3         446586011 ns/op            4469 B/op         18 allocs/op
BenchmarkPBKDF2_Decrypt_Default-16                     3         455325727 ns/op            3162 B/op         15 allocs/op
BenchmarkPBKDF2_Encrypt_HighIterations-16              2         733501845 ns/op            4472 B/op         18 allocs/op
BenchmarkGenerateKey256-16                       1741222               686.2 ns/op            32 B/op          1 allocs/op
BenchmarkAESGCM_Parallel-16                       328702              3344 ns/op         306.24 MB/s        4752 B/op          7 allocs/op
BenchmarkChaCha20_Parallel-16                     527884              2545 ns/op         402.28 MB/s        2256 B/op          5 allocs/op

Bench 2

BenchmarkAESGCM_Encrypt_1KB-16                    565636              2195 ns/op         466.52 MB/s        2432 B/op          3 allocs/op
BenchmarkAESGCM_Decrypt_1KB-16                    930876              1262 ns/op         811.72 MB/s        2304 B/op          3 allocs/op
BenchmarkAESGCM_Encrypt_1MB-16                      1694            857826 ns/op        1222.36 MB/s     1058049 B/op          3 allocs/op
BenchmarkChaCha20_Encrypt_1KB-16                  608810              1869 ns/op         547.94 MB/s        1184 B/op          2 allocs/op
BenchmarkChaCha20_Decrypt_1KB-16                  987297              1206 ns/op         848.94 MB/s        1056 B/op          2 allocs/op
BenchmarkChaCha20_Encrypt_1MB-16                     882           1493090 ns/op         702.29 MB/s     1056807 B/op          2 allocs/op
BenchmarkArgon2id_Encrypt_Default-16                  16          94655010 ns/op        67122092 B/op         95 allocs/op
BenchmarkArgon2id_Decrypt_Default-16                  18          63925862 ns/op        67121300 B/op         94 allocs/op
BenchmarkArgon2id_Encrypt_HighSecurity-16              5         210695180 ns/op        134232507 B/op       135 allocs/op
BenchmarkPBKDF2_Encrypt_Default-16                     3         398225141 ns/op            4453 B/op         17 allocs/op
BenchmarkPBKDF2_Decrypt_Default-16                     3         390489881 ns/op            3157 B/op         15 allocs/op
BenchmarkPBKDF2_Encrypt_HighIterations-16              2         654102038 ns/op            4456 B/op         17 allocs/op
BenchmarkGenerateKey256-16                       1932200               619.7 ns/op            32 B/op          1 allocs/op
BenchmarkAESGCM_Parallel-16                       434350              2920 ns/op         350.63 MB/s        4736 B/op          6 allocs/op
BenchmarkChaCha20_Parallel-16                     529720              2241 ns/op         456.91 MB/s        2240 B/op          4 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidCiphertext = errors.New("invalid ciphertext")
	ErrInvalidKey        = errors.New("invalid key")
	ErrInvalidNonce      = errors.New("invalid nonce")
)

Common errors

Functions

func GenerateKey

func GenerateKey(size int) ([]byte, error)

GenerateKey generates a cryptographically secure random key

func GenerateKey256

func GenerateKey256() ([]byte, error)

GenerateKey256 generates a 256-bit key suitable for AES-256 or ChaCha20

Types

type AESGCMCipher

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

AESGCMCipher implements AES-256-GCM encryption

func NewAESGCM

func NewAESGCM(key []byte, opts ...AESGCMOption) (*AESGCMCipher, error)

NewAESGCM creates a new AES-256-GCM cipher with secure defaults

func (*AESGCMCipher) Decrypt

func (c *AESGCMCipher) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt decrypts ciphertext encrypted with AES-256-GCM

func (*AESGCMCipher) Encrypt

func (c *AESGCMCipher) Encrypt(plaintext []byte) ([]byte, error)

Encrypt encrypts plaintext using AES-256-GCM Returns: nonce || ciphertext || tag

type AESGCMOption

type AESGCMOption func(*AESGCMCipher)

AESGCMOption configures AES-GCM parameters

func WithAESGCMNonceSize

func WithAESGCMNonceSize(size int) AESGCMOption

WithAESGCMNonceSize sets custom nonce size (default: 12 bytes)

func WithAESGCMTagSize

func WithAESGCMTagSize(size int) AESGCMOption

WithAESGCMTagSize sets custom tag size (default: 16 bytes)

type Argon2idCipher

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

Argon2idCipher implements password-based encryption using Argon2id + AES-256-GCM

func NewArgon2id

func NewArgon2id(password []byte, opts ...Argon2idOption) (*Argon2idCipher, error)

NewArgon2id creates a new Argon2id-based cipher with secure 2025 defaults

func (*Argon2idCipher) Decrypt

func (c *Argon2idCipher) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt decrypts ciphertext encrypted with Argon2id + AES-256-GCM

func (*Argon2idCipher) Encrypt

func (c *Argon2idCipher) Encrypt(plaintext []byte) ([]byte, error)

Encrypt encrypts plaintext using Argon2id + AES-256-GCM Returns: salt || aes_gcm_ciphertext

type Argon2idOption

type Argon2idOption func(*Argon2idCipher)

Argon2idOption configures Argon2id parameters

func WithArgon2idMemory

func WithArgon2idMemory(memory uint32) Argon2idOption

WithArgon2idMemory sets the memory parameter in KiB

func WithArgon2idSaltSize

func WithArgon2idSaltSize(size int) Argon2idOption

WithArgon2idSaltSize sets the salt size in bytes

func WithArgon2idThreads

func WithArgon2idThreads(threads uint8) Argon2idOption

WithArgon2idThreads sets the parallelism parameter

func WithArgon2idTime

func WithArgon2idTime(time uint32) Argon2idOption

WithArgon2idTime sets the time parameter (iterations)

type ChaCha20Cipher

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

ChaCha20Cipher implements ChaCha20-Poly1305 AEAD encryption

func NewChaCha20

func NewChaCha20(key []byte, opts ...ChaCha20Option) (*ChaCha20Cipher, error)

NewChaCha20 creates a new ChaCha20-Poly1305 cipher

func (*ChaCha20Cipher) Decrypt

func (c *ChaCha20Cipher) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt decrypts ciphertext encrypted with ChaCha20-Poly1305

func (*ChaCha20Cipher) Encrypt

func (c *ChaCha20Cipher) Encrypt(plaintext []byte) ([]byte, error)

Encrypt encrypts plaintext using ChaCha20-Poly1305 Returns: nonce || ciphertext || tag

type ChaCha20Option

type ChaCha20Option func(*ChaCha20Cipher)

ChaCha20Option configures ChaCha20-Poly1305 parameters

type Cipher

type Cipher interface {
	Encrypt(plaintext []byte) ([]byte, error)
	Decrypt(ciphertext []byte) ([]byte, error)
}

Cipher defines the interface for encryption/decryption operations

type PBKDF2Cipher

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

PBKDF2Cipher implements password-based encryption using PBKDF2 + AES-256-GCM

func NewPBKDF2

func NewPBKDF2(password []byte, opts ...PBKDF2Option) (*PBKDF2Cipher, error)

NewPBKDF2 creates a new PBKDF2-based cipher with secure 2025 defaults

func (*PBKDF2Cipher) Decrypt

func (c *PBKDF2Cipher) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt decrypts ciphertext encrypted with PBKDF2 + AES-256-GCM

func (*PBKDF2Cipher) Encrypt

func (c *PBKDF2Cipher) Encrypt(plaintext []byte) ([]byte, error)

Encrypt encrypts plaintext using PBKDF2 + AES-256-GCM Returns: salt || aes_gcm_ciphertext

type PBKDF2Option

type PBKDF2Option func(*PBKDF2Cipher)

PBKDF2Option configures PBKDF2 parameters

func WithPBKDF2Hash

func WithPBKDF2Hash(hashFunc func() hash.Hash) PBKDF2Option

WithPBKDF2Hash sets the hash function (default: SHA-256)

func WithPBKDF2Iterations

func WithPBKDF2Iterations(iterations int) PBKDF2Option

WithPBKDF2Iterations sets the iteration count

func WithPBKDF2SaltSize

func WithPBKDF2SaltSize(size int) PBKDF2Option

WithPBKDF2SaltSize sets the salt size in bytes

Jump to

Keyboard shortcuts

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