What is @aws-crypto/kms-keyring?
@aws-crypto/kms-keyring is an npm package that provides key management and cryptographic operations using AWS Key Management Service (KMS). It allows developers to encrypt and decrypt data using AWS KMS keys, making it easier to manage encryption keys and secure data in AWS environments.
What are @aws-crypto/kms-keyring's main functionalities?
Encrypt Data
This feature allows you to encrypt data using a specified AWS KMS key. The code sample demonstrates how to create a KMS keyring and use it to encrypt a plaintext string.
const { KmsKeyringNode } = require('@aws-crypto/kms-keyring');
const { encrypt } = require('@aws-crypto/client-node');
const keyring = new KmsKeyringNode({ generatorKeyId: 'arn:aws:kms:us-west-2:123456789012:key/abcd-1234-efgh-5678' });
const plaintext = 'Hello, World!';
async function encryptData() {
const { result } = await encrypt(keyring, plaintext);
console.log('Encrypted data:', result);
}
encryptData();
Decrypt Data
This feature allows you to decrypt data that was previously encrypted using an AWS KMS key. The code sample demonstrates how to create a KMS keyring and use it to decrypt an encrypted string.
const { KmsKeyringNode } = require('@aws-crypto/kms-keyring');
const { decrypt } = require('@aws-crypto/client-node');
const keyring = new KmsKeyringNode({ generatorKeyId: 'arn:aws:kms:us-west-2:123456789012:key/abcd-1234-efgh-5678' });
const encryptedData = '...'; // Encrypted data from previous example
async function decryptData() {
const { plaintext } = await decrypt(keyring, encryptedData);
console.log('Decrypted data:', plaintext.toString());
}
decryptData();
Generate Data Key
This feature allows you to generate a data key using an AWS KMS key. The code sample demonstrates how to create a KMS keyring and use it to generate a data key, returning both the plaintext and encrypted versions of the key.
const { KmsKeyringNode } = require('@aws-crypto/kms-keyring');
const { generateDataKey } = require('@aws-crypto/client-node');
const keyring = new KmsKeyringNode({ generatorKeyId: 'arn:aws:kms:us-west-2:123456789012:key/abcd-1234-efgh-5678' });
async function generateKey() {
const { plaintext, ciphertext } = await generateDataKey(keyring, { length: 32 });
console.log('Generated data key:', plaintext);
console.log('Encrypted data key:', ciphertext);
}
generateKey();
Other packages similar to @aws-crypto/kms-keyring
aws-sdk
The aws-sdk package is the official AWS SDK for JavaScript, which includes support for AWS KMS among many other AWS services. It provides more comprehensive functionality for interacting with AWS services, but requires more manual setup for cryptographic operations compared to @aws-crypto/kms-keyring.
node-forge
node-forge is a JavaScript library for implementing various cryptographic operations, including encryption, decryption, and key management. Unlike @aws-crypto/kms-keyring, it does not integrate directly with AWS KMS, so it requires more effort to manage keys and secure data in AWS environments.
crypto-js
crypto-js is a popular JavaScript library for cryptographic operations such as encryption, decryption, and hashing. It does not provide direct integration with AWS KMS, making it less suitable for applications that require seamless integration with AWS key management services.
aws-encryption-sdk-javascript
The AWS Encryption SDK for JavaScript is a client-side encryption library designed to make it easy for everyone to encrypt and decrypt data using industry standards and best practices. It uses a data format compatible with the AWS Encryption SDKs in other languages. For more information on the AWS Encryption SDKs in all languages, see the Developer Guide.
About @aws-crypto/kms-keyring
This packages is not intended for direct use by clients. To get started with the AWS Encryption SDK for JavaScript, follow the instructions in the README.
License
This SDK is distributed under the
Apache License, Version 2.0,
see LICENSE.txt and NOTICE.txt for more information.