UNPKG

meshblu-encryption

Version:

Common encryption functions and patterns for Meshblu

175 lines (145 loc) 5.33 kB
// Generated by CoffeeScript 1.12.6 (function() { var Encryption, NodeRSA, _, bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; NodeRSA = require('node-rsa'); _ = require('lodash'); Encryption = (function() { function Encryption(arg) { var nodeRsa; nodeRsa = arg.nodeRsa; this.verify = bind(this.verify, this); this.sign = bind(this.sign, this); this.decrypt = bind(this.decrypt, this); this.encrypt = bind(this.encrypt, this); this.codeToAuth = bind(this.codeToAuth, this); this.authToCode = bind(this.authToCode, this); this.toPublicPem = bind(this.toPublicPem, this); this.toPem = bind(this.toPem, this); this.toPublicOldEnvironmentValue = bind(this.toPublicOldEnvironmentValue, this); this.toOldEnvironmentValue = bind(this.toOldEnvironmentValue, this); this.toPublicDer = bind(this.toPublicDer, this); this.toPublicEnvironmentValue = bind(this.toPublicEnvironmentValue, this); this.toDer = bind(this.toDer, this); this.toEnvironmentValue = bind(this.toEnvironmentValue, this); this.key = nodeRsa; } Encryption.prototype.toEnvironmentValue = function() { return this.toDer(); }; Encryption.prototype.toDer = function() { return this.key.exportKey('private-der').toString('base64'); }; Encryption.prototype.toPublicEnvironmentValue = function() { return this.toPublicDer(); }; Encryption.prototype.toPublicDer = function() { return this.key.exportKey('public-der').toString('base64'); }; Encryption.prototype.toOldEnvironmentValue = function() { var pem; pem = this.key.exportKey(); return new Buffer(pem).toString('base64'); }; Encryption.prototype.toPublicOldEnvironmentValue = function() { var pem; pem = this.key.exportKey('public'); return new Buffer(pem).toString('base64'); }; Encryption.prototype.toPem = function() { return this.key.exportKey(); }; Encryption.prototype.toPublicPem = function() { return this.key.exportKey('public'); }; Encryption.prototype.authToCode = function(arg) { var newAuth, token, uuid, verifier; uuid = arg.uuid, token = arg.token; newAuth = uuid + ":" + token; verifier = { auth: newAuth, signature: this.sign(newAuth) }; return new Buffer(JSON.stringify(verifier)).toString('base64'); }; Encryption.prototype.codeToAuth = function(code) { var auth, ref, ref1, signature, token, uuid, verified; ref = JSON.parse(new Buffer(code, 'base64').toString()), auth = ref.auth, signature = ref.signature; verified = this.verify(auth, signature); ref1 = auth.split(':'), uuid = ref1[0], token = ref1[1]; return { uuid: uuid, token: token, verified: verified }; }; Encryption.prototype.encrypt = function(options) { return this.key.encrypt(JSON.stringify(options)).toString('base64'); }; Encryption.prototype.decrypt = function(options) { var decryptedOptions; return decryptedOptions = JSON.parse(this.key.decrypt(options)); }; Encryption.prototype.sign = function(options) { var optionsString; optionsString = JSON.stringify(options); return this.key.sign(optionsString).toString('base64'); }; Encryption.prototype.verify = function(options, signature) { var optionsString, signatureBuffer; optionsString = JSON.stringify(options); signatureBuffer = new Buffer(signature, 'base64'); return this.key.verify(optionsString, signatureBuffer); }; Encryption.fromPem = function(pem) { var encryption, nodeRsa; nodeRsa = new NodeRSA(pem); return encryption = new Encryption({ nodeRsa: nodeRsa }); }; Encryption.fromDer = function(der) { var encryption, keyBinary, nodeRsa; keyBinary = new Buffer(der, 'base64'); nodeRsa = new NodeRSA(keyBinary, 'pkcs1-der'); return encryption = new Encryption({ nodeRsa: nodeRsa }); }; Encryption.fromOldEnvironmentValue = function(oldEnv) { var pem; pem = new Buffer(oldEnv, 'base64').toString(); return Encryption.fromPem(pem); }; Encryption.fromEnvironmentValue = function(env) { return Encryption.fromDer(env); }; Encryption.fromJustGuess = function(thing) { if (thing instanceof NodeRSA) { return new Encryption({ nodeRsa: thing }); } if (Encryption.isPem(thing)) { return Encryption.fromPem(thing); } if (Encryption.isOldEnvironmentValue(thing)) { return Encryption.fromOldEnvironmentValue(thing); } return Encryption.fromDer(thing); }; Encryption.isPem = function(thing) { thing = _.trim(thing); return _.startsWith(thing, '-----') && _.endsWith(thing, '-----'); }; Encryption.isOldEnvironmentValue = function(thing) { var decoded; thing = _.trim(thing); decoded = new Buffer(thing, 'base64').toString(); return Encryption.isPem(decoded); }; return Encryption; })(); module.exports = Encryption; }).call(this); //# sourceMappingURL=encryption.js.map