Crypto
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.
Overview
The Crypto module enables:
- Hashing data with MD5, SHA-1, SHA-2 family (SHA-224, SHA-256, SHA-384, SHA-512)
- Creating HMAC digests using a secret key
- Generating random symmetric encryption keys
- Encrypting and decrypting data with AES-GCM (authenticated encryption)
All operations are performed on the Data type, which encapsulates binary input/output.
Functions
Crypto.generateSymmetricKey(size?: number): Data
Generates a new random symmetric key.
-
Parameters:
size(optional): Key size in bits. Defaults to256.
-
Returns: A
Dataobject representing the key. -
Example:
Hashing Functions
These functions return a cryptographic hash (digest) of the input data.
Crypto.md5(data: Data): Data
Hashes the input data using the MD5 algorithm (128-bit output).
-
Returns: A
Dataobject containing the MD5 digest. -
Example:
Crypto.sha1(data: Data): Data
Uses the SHA-1 algorithm (160-bit output).
- Returns: A
Dataobject.
Crypto.sha256(data: Data): Data
Uses the SHA-256 algorithm (256-bit output).
-
Returns: A
Dataobject. -
Example:
Crypto.sha384(data: Data): Data
Uses the SHA-384 algorithm (384-bit output).
- Returns: A
Dataobject.
Crypto.sha512(data: Data): Data
Uses the SHA-512 algorithm (512-bit output).
- Returns: A
Dataobject.
HMAC Functions
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): Data
Computes HMAC using MD5.
Crypto.hmacSHA1(data: Data, key: Data): Data
Computes HMAC using SHA-1.
Crypto.hmacSHA224(data: Data, key: Data): Data
Computes HMAC using SHA-224.
Crypto.hmacSHA256(data: Data, key: Data): Data
Computes HMAC using SHA-256.
Crypto.hmacSHA384(data: Data, key: Data): Data
Computes HMAC using SHA-384.
Crypto.hmacSHA512(data: Data, key: Data): Data
Computes HMAC using SHA-512.
AES-GCM Encryption
Crypto.encryptAESGCM(data: Data, key: Data, options?: { iv?: Data, aad?: Data }): Data | null
Encrypts the given data using AES-GCM with the provided key.
-
Parameters:
-
data: The plaintextDatato encrypt -
key: ADataobject 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
Dataobject containing the encrypted ciphertext, ornullon failure. -
Example:
Crypto.decryptAESGCM(data: Data, key: Data, aad?: Data): Data | null
Decrypts 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, ornullif decryption fails (e.g., tag mismatch or incorrect key). -
Example:
Summary of Algorithms
Full Example
Notes
- All functions require valid
Dataobjects. - For AES-GCM, if
ivis omitted, a secure random IV is automatically applied. - Encrypted
Datamay include the IV and authentication tag.
