Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | var btoa; |
michael@0 | 2 | |
michael@0 | 3 | function test_derive(cryptoSvc) { |
michael@0 | 4 | // Extracted from test_utils_deriveKey. |
michael@0 | 5 | let pp = "secret phrase"; |
michael@0 | 6 | let salt = "RE5YUHpQcGl3bg=="; // btoa("DNXPzPpiwn") |
michael@0 | 7 | |
michael@0 | 8 | // 16-byte, extract key data. |
michael@0 | 9 | let k = cryptoSvc.deriveKeyFromPassphrase(pp, salt, 16); |
michael@0 | 10 | do_check_eq(16, k.length); |
michael@0 | 11 | do_check_eq(btoa(k), "d2zG0d2cBfXnRwMUGyMwyg=="); |
michael@0 | 12 | |
michael@0 | 13 | // Test different key lengths. |
michael@0 | 14 | k = cryptoSvc.deriveKeyFromPassphrase(pp, salt, 32); |
michael@0 | 15 | do_check_eq(32, k.length); |
michael@0 | 16 | let encKey = btoa(k); |
michael@0 | 17 | |
michael@0 | 18 | // Test via encryption. |
michael@0 | 19 | let iv = cryptoSvc.generateRandomIV(); |
michael@0 | 20 | do_check_eq(cryptoSvc.decrypt(cryptoSvc.encrypt("bacon", encKey, iv), encKey, iv), "bacon"); |
michael@0 | 21 | |
michael@0 | 22 | // Test default length (32). |
michael@0 | 23 | k = cryptoSvc.deriveKeyFromPassphrase(pp, salt, null); |
michael@0 | 24 | do_check_eq(32, k.length); |
michael@0 | 25 | do_check_eq(encKey, btoa(k)); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | function run_test() { |
michael@0 | 29 | let cryptoSvc; |
michael@0 | 30 | try { |
michael@0 | 31 | let backstagePass = Components.utils.import("resource://services-crypto/WeaveCrypto.js"); |
michael@0 | 32 | btoa = backstagePass.btoa; |
michael@0 | 33 | } catch (ex) { |
michael@0 | 34 | _("Aborting test: no WeaveCrypto.js."); |
michael@0 | 35 | return; |
michael@0 | 36 | } |
michael@0 | 37 | test_derive(new WeaveCrypto()); |
michael@0 | 38 | } |