cryptoutil

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package cryptoutil provides secure cryptographic operations.

This package uses only secure, modern cryptographic primitives. Specifically, it uses AES-GCM for authenticated encryption, which provides both confidentiality and integrity protection.

Key Points

  • Uses AES-GCM (Galois/Counter Mode) - authenticated encryption
  • Keys must be 16, 24, or 32 bytes (AES-128, AES-192, AES-256)
  • Nonces are generated automatically using crypto/rand
  • Output is base64-encoded for safe transport

Why AES-GCM?

The original library used AES-ECB mode which has serious weaknesses:

  • Identical plaintext blocks produce identical ciphertext
  • No authentication (ciphertext can be modified undetected)

AES-GCM solves both problems:

  • Each message is unique due to random nonce
  • Authentication tag prevents tampering

Usage

key := make([]byte, 32) // 256-bit key
rand.Read(key)

ciphertext, err := cryptoutil.Encrypt(plaintext, key)
if err != nil { ... }

plaintext, err := cryptoutil.Decrypt(ciphertext, key)
if err != nil { ... }

Key Management

This package does not handle key management. It is the caller's responsibility to:

  • Generate keys securely (use crypto/rand)
  • Store keys securely (use secret management systems)
  • Rotate keys periodically

Security Considerations

  • Never reuse a key with the same nonce
  • Use at least 128-bit keys (32 bytes for AES-256)
  • Do not use this for password hashing (use bcrypt/argon2)

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidKeySize    = errors.New("key must be 16, 24, or 32 bytes")
	ErrInvalidCiphertext = errors.New("invalid ciphertext")
	ErrDecryptFailed     = errors.New("decryption failed: authentication error")
)

Common errors.

Functions

func CompareHash

func CompareHash(data, expectedHash []byte) bool

CompareHash compares a hash with a computed hash of data in constant time. This prevents timing attacks.

func Decrypt

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

Decrypt decrypts base64-encoded ciphertext using AES-GCM with the given key. Returns ErrDecryptFailed if authentication fails (tampered or wrong key).

Example:

plaintext, err := Decrypt(ciphertext, key)
if errors.Is(err, ErrDecryptFailed) {
    // Wrong key or tampered data
}

func DecryptString

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

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

func DeriveKey

func DeriveKey(password, salt string) []byte

DeriveKey derives a key from a password using SHA-256. This is a simple key derivation for non-security-critical applications. For password storage or high-security applications, use argon2 or bcrypt.

Example:

key := DeriveKey("mypassword", "mysalt")
ciphertext, err := Encrypt(data, key)

func Encrypt

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

Encrypt encrypts plaintext using AES-GCM with the given key. The key must be 16, 24, or 32 bytes (AES-128, AES-192, AES-256). Returns base64-encoded ciphertext containing nonce + encrypted data + auth tag.

Example:

key := make([]byte, 32) // Generate a proper key
io.ReadFull(rand.Reader, key)
ciphertext, err := Encrypt([]byte("secret message"), key)

func EncryptString

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

EncryptString encrypts a string and returns base64-encoded ciphertext.

func GenerateKey

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

GenerateKey generates a cryptographically secure random key of the specified size. Size must be 16, 24, or 32 bytes.

Example:

key, err := GenerateKey(32) // AES-256
if err != nil { ... }

func GenerateNonce

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

GenerateNonce generates a random nonce of the specified size. For AES-GCM, the standard nonce size is 12 bytes.

func Hash

func Hash(data []byte) []byte

Hash returns the SHA-256 hash of data as a byte slice.

func HashString

func HashString(s string) string

HashString returns the SHA-256 hash of a string as a hex-encoded string.

func RandomBytes

func RandomBytes(n int) ([]byte, error)

RandomBytes returns n cryptographically secure random bytes.

Types

This section is empty.

Jump to

Keyboard shortcuts

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