This module provides cryptographic operations for the Zephyrus CLI, including AES-GCM encryption, PBKDF2 key derivation, and secure random name generation.
- SaltSize: 16 bytes - Size of the salt used in key derivation
- Iterations: 100000 - Number of PBKDF2 iterations for key derivation
- KeySize: 32 bytes - AES-256 key size
- NonceSize: 12 bytes - Standard GCM nonce size
crypto/aes: Advanced Encryption Standardcrypto/cipher: Cipher modes (GCM)crypto/rand: Cryptographically secure random number generationcrypto/sha256: SHA-256 hashingencoding/hex: Hexadecimal encoding/decodingerrors: Error handlingio: I/O utilitiesgolang.org/x/crypto/pbkdf2: PBKDF2 key derivation
func Encrypt(plaintext []byte, password string) ([]byte, error)Encrypts plaintext using AES-256-GCM with PBKDF2 key derivation.
Parameters:
plaintext: The data to encryptpassword: The password used to derive the encryption key
Return:
[]byte: Encrypted data formatted as[Salt][Nonce][Ciphertext]error: Returns an error if random generation fails
Process:
- Generates a random salt (16 bytes)
- Derives a 256-bit key from the password using PBKDF2 with 100,000 iterations
- Creates an AES-256 cipher and GCM mode
- Generates a random nonce (12 bytes)
- Encrypts the plaintext using AES-GCM
- Returns salt + nonce + ciphertext concatenated
Security Features:
- Uses PBKDF2 with 100,000 iterations to protect against brute force
- Uses AES-256 for strong encryption
- GCM mode provides authenticated encryption
- Random salt and nonce for each encryption
func Decrypt(data []byte, password string) ([]byte, error)Decrypts data encrypted by the Encrypt function using the same password.
Parameters:
data: The encrypted data in format[Salt][Nonce][Ciphertext]password: The password used to derive the decryption key
Return:
[]byte: The decrypted plaintexterror: Returns an error if decryption fails or data is malformed
Process:
- Extracts salt from the first 16 bytes
- Extracts nonce from the next 12 bytes
- Remaining bytes are the ciphertext
- Re-derives the key using the same password and salt
- Decrypts and verifies authenticity using AES-GCM
Error Handling:
- Returns error if data is shorter than salt + nonce size
- Returns error if key derivation fails
- Returns error if decryption/authentication fails
func GenerateRandomName() stringCreates a 16-character hexadecimal string from 8 random bytes.
Return:
string: A 16-character hex string (e.g., "a3f2e1c9d4b6f8e2")
Usage:
- Used to generate obfuscated storage IDs for files in the vault
- Files are stored with these random names instead of their original names
Notes:
- Panics if random byte generation fails (should never happen on healthy systems)
// Encrypt data
plaintext := []byte("sensitive data")
password := "myVaultPassword"
encrypted, err := Encrypt(plaintext, password)
if err != nil {
log.Fatal(err)
}
// Later, decrypt with the same password
decrypted, err := Decrypt(encrypted, password)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(decrypted)) // "sensitive data"
// Generate a random storage name for a file
storageName := GenerateRandomName() // "a3f2e1c9d4b6f8e2"