react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / browserify-sign / node_modules / parse-asn1 / fixProc.js
80556 views// adapted from https://github.com/apatil/pemstrip1var findProc = /Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: AES-((?:128)|(?:192)|(?:256))-CBC,([0-9A-H]+)\r?\n\r?\n([0-9A-z\n\r\+\/\=]+)\r?\n/m;2var startRegex =/^-----BEGIN (.*) KEY-----\r?\n/m;3var fullRegex = /^-----BEGIN (.*) KEY-----\r?\n([0-9A-z\n\r\+\/\=]+)\r?\n-----END \1 KEY-----$/m;4var evp = require('./EVP_BytesToKey');5var ciphers = require('browserify-aes');6module.exports = function (okey, password) {7var key = okey.toString();8var match = key.match(findProc);9var decrypted;10if (!match) {11var match2 = key.match(fullRegex);12decrypted = new Buffer(match2[2].replace(/\r?\n/g, ''), 'base64');13} else {14var suite = 'aes' + match[1];15var iv = new Buffer(match[2], 'hex');16var cipherText = new Buffer(match[3].replace(/\r?\n/g, ''), 'base64');17var cipherKey = evp(password, iv.slice(0,8), parseInt(match[1]));18var out = [];19var cipher = ciphers.createDecipheriv(suite, cipherKey, iv);20out.push(cipher.update(cipherText));21out.push(cipher.final());22decrypted = Buffer.concat(out);23}24var tag = key.match(startRegex)[1] + ' KEY';25return {26tag: tag,27data: decrypted28};29};3031// http://stackoverflow.com/a/703370532function wrap (str) {33var chunks = []3435for (var i = 0; i < str.length; i += 64) {36chunks.push(str.slice(i, i + 64))37}38return chunks.join("\n")39}404142