The Crypto module provides a set of cryptographic utilities for hashing, HMAC authentication, symmetric key generation, and AES-GCM encryption/decryption. It is designed to securely process Data instances using industry-standard algorithms.
The Crypto module enables:
All operations are performed on the Data type, which encapsulates binary input/output.
Crypto.generateSymmetricKey(size?: number): DataGenerates a new random symmetric key.
Parameters:
size (optional): Key size in bits. Defaults to 256.Returns: A Data object representing the key.
Example:
These functions return a cryptographic hash (digest) of the input data.
Crypto.md5(data: Data): DataHashes the input data using the MD5 algorithm (128-bit output).
Returns: A Data object containing the MD5 digest.
Example:
Crypto.sha1(data: Data): DataUses the SHA-1 algorithm (160-bit output).
Data object.Crypto.sha256(data: Data): DataUses the SHA-256 algorithm (256-bit output).
Returns: A Data object.
Example:
Crypto.sha384(data: Data): DataUses the SHA-384 algorithm (384-bit output).
Data object.Crypto.sha512(data: Data): DataUses the SHA-512 algorithm (512-bit output).
Data object.These functions generate a hash-based message authentication code (HMAC) using a shared secret key.
All return a Data object representing the HMAC digest.
Parameters:
data: The message to authenticate (Data)key: The symmetric key (Data)Crypto.hmacMD5(data: Data, key: Data): DataComputes HMAC using MD5.
Crypto.hmacSHA1(data: Data, key: Data): DataComputes HMAC using SHA-1.
Crypto.hmacSHA224(data: Data, key: Data): DataComputes HMAC using SHA-224.
Crypto.hmacSHA256(data: Data, key: Data): DataComputes HMAC using SHA-256.
Crypto.hmacSHA384(data: Data, key: Data): DataComputes HMAC using SHA-384.
Crypto.hmacSHA512(data: Data, key: Data): DataComputes HMAC using SHA-512.
Crypto.encryptAESGCM(data: Data, key: Data, options?: { iv?: Data, aad?: Data }): Data | nullEncrypts the given data using AES-GCM with the provided key.
Parameters:
data: The plaintext Data to encrypt
key: A Data object representing the symmetric key
options (optional):
iv: Initialization vector (optional). If omitted, a random IV is used internally.aad: Additional authenticated data (optional). Used for authentication but not encrypted.Returns: A Data object containing the encrypted ciphertext, or null on failure.
Example:
Crypto.decryptAESGCM(data: Data, key: Data, aad?: Data): Data | nullDecrypts AES-GCM-encrypted Data using the provided key and optional AAD.
Parameters:
data: The encrypted data (Data)key: The symmetric key used to encrypt the dataaad (optional): The additional authenticated data used during encryption (must match exactly)Returns: The decrypted plaintext as Data, or null if decryption fails (e.g., tag mismatch or incorrect key).
Example:
| Function | Output Size | Use Case |
|---|---|---|
md5 |
128 bits | Legacy checksums |
sha1 |
160 bits | Compatibility |
sha256 |
256 bits | General-purpose security |
sha384 |
384 bits | Stronger hashing |
sha512 |
512 bits | High-security requirements |
hmacXXX |
Same as hash | Authentication |
AES-GCM |
variable | Authenticated encryption |
Data objects.iv is omitted, a secure random IV is automatically applied.Data may include the IV and authentication tag.