UNPKG

@cumulus/aws-client

Version:
46 lines 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decryptBase64String = exports.encrypt = exports.createKey = void 0; const services_1 = require("./services"); /** * Create a KMS key * * See https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/KMS.html#createKey-property * for allowed params and return value. * * @param {Object} params * @returns {Promise<Object>} */ const createKey = (params = {}) => (0, services_1.kms)().createKey(params); exports.createKey = createKey; /** * Encrypt a string using KMS * * @param {string} KeyId - the KMS key to use for encryption * @param {string} Plaintext - the string to be encrypted * @returns {Promise<string>} the Base 64 encoding of the encrypted value */ const encrypt = async (KeyId, Plaintext) => { const { CiphertextBlob } = await (0, services_1.kms)().encrypt({ KeyId, Plaintext: new TextEncoder().encode(Plaintext) }); if (CiphertextBlob === undefined) throw new Error('Returned CiphertextBlob is undefined'); return Buffer.from(CiphertextBlob).toString('base64'); }; exports.encrypt = encrypt; /** * Decrypt a KMS-encrypted string, Base 64 encoded * * @param {string} ciphertext - a KMS-encrypted value, Base 64 encoded * @returns {string} the plaintext */ const decryptBase64String = async (ciphertext) => { const { Plaintext } = await (0, services_1.kms)().decrypt({ CiphertextBlob: Buffer.from(ciphertext, 'base64'), }); if (Plaintext === undefined) return undefined; return Buffer.from(Plaintext).toString(); }; exports.decryptBase64String = decryptBase64String; //# sourceMappingURL=KMS.js.map