Add some more tests for the crypto module

This commit is contained in:
Martin Kleinschrodt 2013-11-10 23:21:28 +01:00
parent 6f098aacb8
commit 35a139fa1e
1 changed files with 28 additions and 2 deletions

View File

@ -11,6 +11,32 @@ test("key generation", function() {
var newKey = safe.crypto.genKey(pwd, key.salt, keyLength);
// Using the same password and salt should result in the same key
equal(key.key.join(), newKey.key.join());
equal(key.salt.join(), newKey.salt.join());
deepEqual(key.key, newKey.key);
deepEqual(key.salt, newKey.salt);
newKey = safe.crypto.genKey(pwd, null, keyLength);
// A key generated with new salt should turn out differently.
notDeepEqual(newKey.key, key.key);
});
test("encrypt/decrypt roundtrip", function() {
var pwd = "password", pt = "Hello World!";
var key = safe.crypto.genKey(pwd);
var c = safe.crypto.encrypt(key.key, pt);
// We should get back a _safe.crypto.container_ object
ok(safe.crypto.container.isPrototypeOf(c));
// Encrypting the same value twice with the same key should
// result in two different cipher texts, since a new iv is randomly
// generated each time
var newC = safe.crypto.encrypt(key.key, pt);
notEqual(newC.ct, c.ct);
// Decrypted value should be equal to the original value
var dec = safe.crypto.decrypt(key.key, c);
equal(dec, pt);
});