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