-
-
Save bxem44/f8f7c685a826178aaf9c3fff96697a67 to your computer and use it in GitHub Desktop.
A Lambda function for AES encryption/decryption, from a Gist!
This file contains hidden or 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
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 |
This file contains hidden or 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
'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); | |
}; |
This file contains hidden or 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
{ | |
"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