@scaffoldly/serverless-util
Version:
Scaffoldly Serverless Helper Functionality
36 lines • 1.31 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.decryptValue = exports.encryptValue = void 0;
const constants_1 = require("./constants");
const exports_1 = require("./exports");
const encryptValue = async (value, keyId) => {
const kms = await exports_1.KMS();
const KeyId = keyId || `alias/${constants_1.STAGE}`;
const encryptResult = await kms.encrypt({ KeyId, Plaintext: Buffer.from(value, 'utf-8') }).promise();
if (!encryptResult.KeyId) {
throw new Error('Missing key id in encryption response');
}
if (!encryptResult.CiphertextBlob) {
throw new Error('Missing cyphertext blob in encryption response');
}
return {
keyId: KeyId,
value: encryptResult.CiphertextBlob.toString('base64'),
};
};
exports.encryptValue = encryptValue;
const decryptValue = async (value) => {
const kms = await exports_1.KMS();
const decryptResult = await kms
.decrypt({
KeyId: value.keyId,
CiphertextBlob: Buffer.from(value.value, 'base64'),
})
.promise();
if (!decryptResult.Plaintext) {
throw new Error('Missing plaintext result');
}
return decryptResult.Plaintext.toString('utf-8');
};
exports.decryptValue = decryptValue;
//# sourceMappingURL=encryption.js.map
;