|
1 | 1 | # AWS Encryption SDK for JavaScript client for Node.js
|
2 | 2 |
|
3 |
| -This module is for encryption and decryption in Node.js. |
4 |
| -It is a composition of underlying modules. |
5 |
| -It is intended to have every component of the AWS Encryption SDK. |
| 3 | +# @aws-crypto/client-node |
| 4 | + |
| 5 | +The *client-node* module includes all of the modules you need to use the AWS Encryption SDK for |
| 6 | +JavaScript with Node.js. |
| 7 | + |
| 8 | +* decrypt-node |
| 9 | +* encrypt-node |
| 10 | +* kms-keyring-node |
| 11 | +* material-management-node |
| 12 | +* caching-materials-manager-node |
| 13 | +* raw-aes-keyring-node |
| 14 | +* raw-rsa-keyring-node |
| 15 | + |
| 16 | +For code examples that show you how to these modules to create keyrings and encrypt and decrypt data, install the [example-node](https://github.com/awslabs/aws-encryption-sdk-javascript/tree/master/modules/example-node) module. |
| 17 | +## install |
| 18 | + |
| 19 | +To install this module, use the npm package manager. For help with installation, see |
| 20 | +[https://www.npmjs.com/get-npm](https://www.npmjs.com/get-npm). |
| 21 | + |
| 22 | +```sh |
| 23 | +npm install @aws-crypto/client-node |
| 24 | +``` |
| 25 | + |
| 26 | +## use |
| 27 | + |
| 28 | +```javascript |
| 29 | + |
| 30 | +const { KmsKeyringNode, encrypt, decrypt } = require('@aws-crypto/client-node') |
| 31 | +/* Start by constructing a keyring. We'll create a KMS keyring. |
| 32 | + * Specify an AWS Key Management Service (AWS KMS) customer master key (CMK) to be the |
| 33 | + * generator key in the keyring. This CMK generates a data key and encrypts it. |
| 34 | + * To use the keyring to encrypt data, you need kms:GenerateDataKey permission |
| 35 | + * on this CMK. To decrypt, you need kms:Decrypt permission. |
| 36 | + */ |
| 37 | +const generatorKeyId = 'arn:aws:kms:us-west-2:658956600833:alias/EncryptDecrypt' |
| 38 | + |
| 39 | +/* You can specify additional CMKs for the keyring. The data key that the generator key |
| 40 | + * creates is also encrypted by the additional CMKs you specify. To encrypt data, |
| 41 | + * you need kms:Encrypt permission on this CMK. To decrypt, you need kms:Decrypt permission. |
| 42 | + */ |
| 43 | +const keyIds = ['arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f'] |
| 44 | + |
| 45 | +/* Create the KMS keyring */ |
| 46 | +const keyring = new KmsKeyringNode({ generatorKeyId, keyIds }) |
| 47 | + |
| 48 | +/* Set an encryption context For more information: |
| 49 | + * https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context |
| 50 | + */ |
| 51 | +const context = { |
| 52 | + stage: 'demo', |
| 53 | + purpose: 'simple demonstration app', |
| 54 | + origin: 'us-west-2' |
| 55 | + } |
| 56 | + |
| 57 | +/* Create a string to encrypt */ |
| 58 | +const cleartext = 'asdf' |
| 59 | + |
| 60 | +/* Encrypt the string using the keyring and the encryption context |
| 61 | + * The SDK returns an "encrypted message" that includes the ciphertext, |
| 62 | + * the encryption context, and the encrypted data keys. |
| 63 | + */ |
| 64 | +const { ciphertext } = await encrypt(keyring, cleartext, { context }) |
| 65 | + |
| 66 | +/* Decrypt the ciphertext using the same keyring */ |
| 67 | +const { plaintext, messageHeader } = await decrypt(keyring, ciphertext) |
| 68 | + |
| 69 | +/* Get the encryption context */ |
| 70 | +const { encryptionContext } = messageHeader |
| 71 | + |
| 72 | +/* Verify that all values in the original encryption context are in the |
| 73 | + * current one. (The SDK adds extra values for signing.) |
| 74 | + */ |
| 75 | + Object |
| 76 | + .entries(context) |
| 77 | + .forEach(([key, value]) => { |
| 78 | + if (encryptionContext[key] !== value) throw new Error('Encryption Context does not match expected values') |
| 79 | + }) |
| 80 | + |
| 81 | +/* If the encryption context is verified, return the plaintext. */ |
| 82 | + |
| 83 | +``` |
| 84 | + |
| 85 | +## test |
| 86 | + |
| 87 | +```sh |
| 88 | +npm test |
| 89 | +``` |
| 90 | + |
| 91 | +## license |
| 92 | + |
| 93 | +This SDK is distributed under the |
| 94 | +[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0), |
| 95 | +see LICENSE.txt and NOTICE.txt for more information. |
6 | 96 |
|
0 commit comments