Skip to content

Instantly share code, notes, and snippets.

@willyg302
Last active August 11, 2023 06:39
Show Gist options
  • Save willyg302/560ab5d328b37d2cd4cc to your computer and use it in GitHub Desktop.
Save willyg302/560ab5d328b37d2cd4cc to your computer and use it in GitHub Desktop.
A Lambda function for AES encryption/decryption, from a Gist!
config:
FunctionName: aws-lambda-aes-gist
Handler: index.handler
Runtime: nodejs
Description: A Lambda function for AES encryption/decryption, from a Gist!
install: npm install --production
'use strict';
var crypto = require('crypto');
var extend = require('extend');
var revalidator = require('revalidator');
exports.handler = function(event, context) {
console.log((event.enc ? 'En' : 'De') + "crypting: " + event.message);
var opts = extend({
length: 256,
mode: 'ecb',
encoding: 'base64'
}, event.opts);
var valid = revalidator.validate(opts, {
properties: {
length: {
type: 'integer',
conform: function(v) {
return [128, 192, 256].indexOf(v) !== -1;
}
},
mode: {
type: 'string',
conform: function(v) {
return ['cbc', 'cfb', 'cfb1', 'cfb8', 'ctr', 'ecb', 'gcm', 'ofb'].indexOf(v) !== -1;
}
},
encoding: {
type: 'string',
conform: function(v) {
return ['base64', 'hex'].indexOf(v) !== -1;
}
}
}
});
if (!valid.valid) {
var errors = valid.errors.map(function(error) {
return error.property;
});
context.fail("The following options are malformed: " + errors.join(', '));
return;
}
var format = "aes-" + opts.length + "-" + opts.mode;
var result;
if (event.enc) {
var ct = crypto.createCipher(format, event.pass);
result = ct.update(event.message, 'utf8', opts.encoding) + ct.final(opts.encoding);
} else {
var pt = crypto.createDecipher(format, event.pass);
result = pt.update(event.message, opts.encoding, 'utf8') + pt.final('utf8');
}
console.log("Result: " + result);
context.succeed(result);
};
{
"name": "aws-lambda-aes-gist",
"version": "1.0.0",
"description": "A Lambda function for AES encryption/decryption, from a Gist!",
"main": "index.js",
"keywords": [
"aws",
"lambda",
"aws-lambda"
],
"author": "William Gaul",
"license": "MIT",
"dependencies": {
"extend": "^2.0.0",
"revalidator": "^0.3.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment