summaryrefslogtreecommitdiffstats
path: root/services/crypto/tests/unit/test_crypto_deriveKey.js
blob: 00af474cb0f32633e4dc8f75fa715bf6ad427460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Components.utils.import("resource://services-crypto/WeaveCrypto.js");

function run_test() {
  let cryptoSvc = new WeaveCrypto();
  // Extracted from test_utils_deriveKey.
  let pp = "secret phrase";
  let salt = "RE5YUHpQcGl3bg==";   // btoa("DNXPzPpiwn")

  // 16-byte, extract key data.
  let k = cryptoSvc.deriveKeyFromPassphrase(pp, salt, 16);
  do_check_eq(16, k.length);
  do_check_eq(btoa(k), "d2zG0d2cBfXnRwMUGyMwyg==");

  // Test different key lengths.
  k = cryptoSvc.deriveKeyFromPassphrase(pp, salt, 32);
  do_check_eq(32, k.length);
  do_check_eq(btoa(k), "d2zG0d2cBfXnRwMUGyMwyroRXtnrSIeLwSDvReSfcyA=");
  let encKey = btoa(k);

  // Test via encryption.
  let iv = cryptoSvc.generateRandomIV();
  do_check_eq(cryptoSvc.decrypt(cryptoSvc.encrypt("bacon", encKey, iv), encKey, iv), "bacon");

  // Test default length (32).
  k = cryptoSvc.deriveKeyFromPassphrase(pp, salt);
  do_check_eq(32, k.length);
  do_check_eq(encKey, btoa(k));
}