Created
January 3, 2019 09:19
-
-
Save kgalli/bf9c30492907949356ad99eb1ba65a28 to your computer and use it in GitHub Desktop.
AWS KMS example Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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