Created
December 9, 2013 03:27
-
-
Save swys/7866988 to your computer and use it in GitHub Desktop.
Testing encryption/decryption on all possible Algorithms in nodejs crypto module
This file contains 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
var crypto = require('crypto'), | |
pull = require('pull-stream'), | |
errors = [], | |
skipDecrypt = [], | |
encryptCount = 0, | |
decryptCount = 0 | |
var vals = 'node issue # 6477 told me to make input to xts algorithm more than 16bytes so that is what i am doing so this should work' | |
var ciphers = crypto.getCiphers() | |
ciphers.forEach(function(ciph, i) { | |
opts = { | |
encrypt : { | |
inputEncoding : 'utf8', | |
encoding : 'base64' | |
}, | |
decrypt : { | |
inputEncoding : 'base64', | |
encoding : 'utf8' | |
}, | |
password : 'secret', | |
algorithm : ciphers[i] | |
}; | |
//console.log("Testing Algorithm : ", opts.algorithm) | |
var encrypted = encrypt(vals, opts) | |
//console.log("Encrypted Text : ", encrypted) | |
if (skipDecrypt.indexOf(opts.algorithm) === -1) { | |
var decrypted = decrypt(encrypted, opts) | |
//console.log("Decrypted Text : ", decrypted) | |
if (vals !== decrypted) { | |
errors.push({ | |
op : 'decrypted text does not match orginal message', | |
algorithm : opts.algorithm, | |
error : decrypted + " doesnt equal " + vals | |
}) | |
} | |
} | |
}) | |
console.log("Total Algorithms available on this system %d", ciphers.length) | |
console.log("Total Algorithms tested for encryption %d", encryptCount) | |
console.log("Total Algorithms tested for decryption %d", decryptCount) | |
console.log("Received %d errors total", errors.length) | |
errors.forEach(function(err) { | |
console.dir(err) | |
}) | |
function encrypt(msg, opts) { | |
encryptCount += 1 | |
if (!msg) throw new Error("Must pass in msg to be encrypted") | |
if (!opts.password) throw new Error("Must supply password") | |
if (!opts.encrypt) opts.encrypt = {} | |
var alg = opts.algorithm || 'aes-256-cbc' | |
var ine = (opts.encrypt.inputEncoding === undefined ? 'utf8' : opts.encrypt.inputEncoding) | |
var enc = (opts.encrypt.encoding === undefined ? 'hex' : opts.encrypt.encoding) | |
var cipher = crypto.createCipher(alg, opts.password) | |
var cipherTxt = '' | |
try { | |
cipherTxt += cipher.update(msg, ine, enc) | |
} catch (e) { | |
skipDecrypt.push(opts.algorithm) | |
errors.push({ | |
op : 'encrypt update', | |
algorithm : opts.algorithm, | |
error : e | |
}) | |
} | |
try { | |
cipherTxt += cipher.final(enc) | |
} catch (e) { | |
skipDecrypt.push(opts.algorithm) | |
errors.push({ | |
op : 'encrypt final', | |
algorithm : opts.algorithm, | |
error : e | |
}) | |
} | |
return cipherTxt | |
} | |
function decrypt(msg, opts) { | |
decryptCount += 1 | |
if (!msg) throw new Error("Must pass in msg to be decrypted") | |
if (!opts.password) throw new Error("Must supply password") | |
if (!opts.decrypt) opts.decrypt = {} | |
var alg = opts.algorithm || 'aes-256-cbc' | |
var ine = (opts.decrypt.inputEncoding === undefined ? 'hex' : opts.decrypt.inputEncoding) | |
var enc = (opts.decrypt.encoding === undefined ? 'utf8' : opts.decrypt.encoding) | |
var cipher = crypto.createDecipher(alg, opts.password) | |
var plainTxt = '' | |
try { | |
plainTxt += cipher.update(msg, ine, enc) | |
} catch (e) { | |
errors.push({ | |
op : 'decrypt update', | |
algorithm : opts.algorithm, | |
error : e | |
}) | |
} | |
try { | |
plainTxt += cipher.final(enc) | |
} catch (e) { | |
errors.push({ | |
op : 'decrypt final', | |
algorithm : opts.algorithm, | |
error : e | |
}) | |
} | |
return plainTxt | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment