Static
bytesConverts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
The parameters for the symmetric key conversion.
The raw symmetric key as a Uint8Array.
A Promise that resolves to the symmetric key in JWK format.
This method accepts a symmetric key represented as a byte array (Uint8Array) and converts it into a JWK object for use with AES-GCM (Advanced Encryption Standard - Galois/Counter Mode). The conversion process involves encoding the key into base64url format and setting the appropriate JWK parameters.
The resulting JWK object includes the following properties:
kty
: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).k
: The symmetric key, base64url-encoded.kid
: Key ID, generated based on the JWK thumbprint.const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
const privateKey = await AesGcm.bytesToPrivateKey({ privateKeyBytes });
Static
decryptDecrypts the provided data using AES-GCM.
The parameters for the decryption operation.
Optional
additionalOptional additional authenticated data. Optional.
The encrypted data to decrypt, represented as a Uint8Array.
The initialization vector, represented as a Uint8Array.
The key to use for decryption, represented in JWK format.
Optional
tagThe length of the authentication tag in bits. Optional.
A Promise that resolves to the decrypted data as a Uint8Array.
This method performs AES-GCM decryption on the given encrypted data using the specified key.
It requires an initialization vector (IV), the encrypted data along with the decryption key,
and optionally, additional authenticated data (AAD). The method returns the decrypted data as a
Uint8Array. The optional tagLength
parameter specifies the size in bits of the authentication
tag used when encrypting the data. If not specified, the default tag length of 128 bits is
used.
const encryptedData = new Uint8Array([...]); // Encrypted data
const iv = new Uint8Array([...]); // Initialization vector used during encryption
const additionalData = new Uint8Array([...]); // Optional additional authenticated data
const key = { ... }; // A Jwk object representing the AES key
const decryptedData = await AesGcm.decrypt({
data: encryptedData,
iv,
additionalData,
key,
tagLength: 128 // Optional tag length in bits
});
Static
encryptEncrypts the provided data using AES-GCM.
The parameters for the encryption operation.
Optional
additionalOptional additional authenticated data. Optional.
The data to encrypt, represented as a Uint8Array.
The initialization vector, represented as a Uint8Array.
The key to use for encryption, represented in JWK format.
Optional
tagThe length of the authentication tag in bits. Optional.
A Promise that resolves to the encrypted data as a Uint8Array.
This method performs AES-GCM encryption on the given data using the specified key.
It requires an initialization vector (IV), the encrypted data along with the decryption key,
and optionally, additional authenticated data (AAD). The method returns the encrypted data as a
Uint8Array. The optional tagLength
parameter specifies the size in bits of the authentication
tag generated in the encryption operation and used for authentication in the corresponding
decryption. If not specified, the default tag length of 128 bits is used.
const data = new TextEncoder().encode('Messsage');
const iv = new Uint8Array([...]); // Initialization vector
const additionalData = new Uint8Array([...]); // Optional additional authenticated data
const key = { ... }; // A Jwk object representing an AES key
const encryptedData = await AesGcm.encrypt({
data,
iv,
additionalData,
key,
tagLength: 128 // Optional tag length in bits
});
Static
generateGenerates a symmetric key for AES in Galois/Counter Mode (GCM) in JSON Web Key (JWK) format.
The parameters for the key generation.
The length of the key in bits. Common lengths are 128, 192, and 256 bits.
A Promise that resolves to the generated symmetric key in JWK format.
This method creates a new symmetric key of a specified length suitable for use with AES-GCM encryption. It leverages cryptographically secure random number generation to ensure the uniqueness and security of the key. The generated key adheres to the JWK format, facilitating compatibility with common cryptographic standards and ease of use in various cryptographic applications.
The generated key includes these components:
kty
: Key Type, set to 'oct' for Octet Sequence, indicating a symmetric key.k
: The symmetric key component, base64url-encoded.kid
: Key ID, generated based on the JWK thumbprint, providing a unique identifier.const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
const privateKey = await AesGcm.generateKey({ length });
Static
privateConverts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
A Promise that resolves to the symmetric key as a Uint8Array.
This method takes a symmetric key in JWK format and extracts its raw byte representation. It focuses on the 'k' parameter of the JWK, which represents the symmetric key component in base64url encoding. The method decodes this value into a byte array, providing the symmetric key in its raw binary form.
const privateKey = { ... }; // A symmetric key in JWK format
const privateKeyBytes = await AesGcm.privateKeyToBytes({ privateKey });
The
AesGcm
class provides a comprehensive set of utilities for cryptographic operations using the Advanced Encryption Standard (AES) in Galois/Counter Mode (GCM). This class includes methods for key generation, encryption, decryption, and conversions between raw byte arrays and JSON Web Key (JWK) formats. It is designed to support AES-GCM, a symmetric key algorithm that is widely used for its efficiency, security, and provision of authenticated encryption.AES-GCM is particularly favored for scenarios that require both confidentiality and integrity of data. It integrates the counter mode of encryption with the Galois mode of authentication, offering high performance and parallel processing capabilities.
Key Features:
The methods in this class are asynchronous, returning Promises to accommodate various JavaScript environments.
Example