Skip to content

Instantly share code, notes, and snippets.

@kgalli
Created January 3, 2019 09:19
Show Gist options
  • Save kgalli/bf9c30492907949356ad99eb1ba65a28 to your computer and use it in GitHub Desktop.
Save kgalli/bf9c30492907949356ad99eb1ba65a28 to your computer and use it in GitHub Desktop.
AWS KMS example Javascript
const AWS = require("aws-sdk");
const kms = new AWS.KMS({region: 'eu-central-1' });
const decrypt = async (secret) => {
const secretBuffer = new Buffer(secret.replace('aws:kms:', ''), 'base64');
const params = { CiphertextBlob: secretBuffer };
const response = await kms.decrypt(params).promise();
return response.Plaintext.toString()
};
const encrypt = async (keyId, value) => {
const params = { KeyId: keyId, Plaintext: value };
const result = await kms.encrypt(params).promise();
return result.CiphertextBlob.toString('base64');
};
const test = async () => {
const keyId = '529a06e5-2c4f-4dc5-9bb7-ac518be3b9f6';
const value = 'dave';
const encryptedValue = await encrypt(keyId, value);
const deryptedValue = await decrypt(encryptedValue);
console.log(`Value: ${value} \nEncryptedValue: ${encryptedValue}\nDecryptedValue: ${deryptedValue}\nSuccess: ${value === deryptedValue}`);
};
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment