Skip to content

Instantly share code, notes, and snippets.

@bluesmoon
Created July 26, 2012 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluesmoon/3184113 to your computer and use it in GitHub Desktop.
Save bluesmoon/3184113 to your computer and use it in GitHub Desktop.
Test Node.js Cipher implementation.
crypto=require("crypto");
assert=require("assert");
data = [
// 0. Short key, short data, ascii data, hex output
{ k: "abcde", d: "foobar", enc_in: "ascii", enc_out: "hex" },
// 1. Long key, short data, ascii data, hex output
{ k: "abcdefabcdefabcdeabcdefabcdefabcdeabcdefabcdefabcdeabcdefabcdefabcdef", d: "foobar", enc_in: "ascii", enc_out: "hex" },
// 2. Long key, long data, ascii data, hex output
{ k: "abcdefabcdefabcdeabcdefabcdefabcdeabcdefabcdefabcdeabcdefabcdefabcdef", d: "foobarfoobarfoobarfoobarfoobar", enc_in: "ascii", enc_out: "hex" },
// 3. Short key, long data, ascii data, hex output
{ k: "abcdef", d: "foobarfoobarfoobarfoobarfoobar", enc_in: "ascii", enc_out: "hex" },
// 4. Short key, medium data, ascii data, hex output
{ k: "abcdef", d: "foobarfo", enc_in: "ascii", enc_out: "hex" },
// 5. Short key, medium data, ascii data, hex output
{ k: "abcdef", d: "foobarf", enc_in: "ascii", enc_out: "hex" }
];
data.forEach(function(d, i) {
console.log("Test %d", i);
try {
var cipher = crypto.createCipher("bf", d.k);
cipher.update(d.d, d.enc_in);
var c_out=cipher.final(d.enc_out);
console.log(c_out);
var decipher = crypto.createDecipher("bf", d.k);
decipher.update(c_out, d.enc_out);
var d_out=decipher.final(d.enc_in);
assert(d_out === d.d, "" + i + " did not match: " + d_out + "!==" + d.d)
console.log(" ...passed");
}
catch(err) {
console.log("Error with %d: %s", i, err);
}
});
$ node test-cipher.js
Test 0
296fe0fae8365fa7
...passed
Test 1
b927e1b0b71d719b
...passed
Test 2
c2a79df5699b88b8
Error with 2: AssertionError: 2 did not match: !==foobarfoobarfoobarfoobarfoobar
Test 3
b1b9029a3d468b22
Error with 3: AssertionError: 3 did not match: !==foobarfoobarfoobarfoobarfoobar
Test 4
b62b214920532221
Error with 4: AssertionError: 4 did not match: !==foobarfo
Test 5
4314ac415df15428
...passed
$ node test-cipher.js
Test 0
296fe0fae8365fa7
...passed
Test 1
b927e1b0b71d719b
...passed
Test 2
c2a79df5699b88b8
Error with 2: TypeError: DecipherFinal fail
Test 3
b1b9029a3d468b22
Error with 3: TypeError: DecipherFinal fail
Test 4
b62b214920532221
Error with 4: TypeError: DecipherFinal fail
Test 5
4314ac415df15428
...passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment