3 // This file tests various aspects of the nsICryptoHash implementation for all
4 // of the supported algorithms.
6 const messages = ["The quick brown fox jumps over the lazy dog", ""];
10 initConstant: Ci.nsICryptoHash.MD5,
12 "9e107d9d372bb6826bd81d3542a419d6",
13 "d41d8cd98f00b204e9800998ecf8427e",
15 b64Hashes: ["nhB9nTcrtoJr2B01QqQZ1g==", "1B2M2Y8AsgTpgAmY7PhCfg=="],
19 initConstant: Ci.nsICryptoHash.SHA1,
21 "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
22 "da39a3ee5e6b4b0d3255bfef95601890afd80709",
24 b64Hashes: ["L9ThxnotKPzthJ7hu3bnORuT6xI=", "2jmj7l5rSw0yVb/vlWAYkK/YBwk="],
28 initConstant: Ci.nsICryptoHash.SHA256,
30 "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592",
31 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
34 "16j7swfXgJRpypq8sAguT41WUeRtPNt2LQLQvzfJ5ZI=",
35 "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
40 initConstant: Ci.nsICryptoHash.SHA384,
42 "ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1",
43 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b",
46 "ynN/EBSkj0wLbdQ8sXewr9nlFpNnVExJQBHjMX2/mlCcseXcHoWpQbvuPX8q+8mx",
47 "OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb",
52 initConstant: Ci.nsICryptoHash.SHA512,
54 "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6",
55 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
58 "B+VH2VhvanP3P7rAQ17XaVEhj7fQyNeIownXhUNru2Quk6JSqVTyORJUfR6KO17W4b/XCXghIz+gU489uFT+5g==",
59 "z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==",
64 function doHash(algo, value, cmp) {
65 let hash = Cc["@mozilla.org/security/hash;1"].createInstance(
68 hash.initWithString(algo);
70 value = new TextEncoder().encode(value);
71 hash.update(value, value.length);
73 hexify(hash.finish(false)),
75 `Actual and expected hash for ${algo} should match`
78 hash.initWithString(algo);
79 hash.update(value, value.length);
81 hexify(hash.finish(false)),
83 `Actual and expected hash for ${algo} should match after re-init`
87 function doHashStream(algo, value, cmp) {
88 // TODO(Bug 459835): Make updateFromStream() accept zero length streams.
93 let hash = Cc["@mozilla.org/security/hash;1"].createInstance(
96 hash.initWithString(algo);
98 let stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
99 Ci.nsIStringInputStream
101 stream.setUTF8Data(value);
102 hash.updateFromStream(stream, stream.available());
104 hexify(hash.finish(false)),
106 `Actual and expected hash for ${algo} should match updating from stream`
110 function testInitConstantAndBase64(
116 let value = new TextEncoder().encode(message);
118 let hash = Cc["@mozilla.org/security/hash;1"].createInstance(
121 hash.init(initConstant);
122 hash.update(value, value.length);
126 `Actual and expected base64 hash for ${algoName} should match`
130 function run_test() {
131 for (let algo of ALGORITHMS) {
132 algo.hexHashes.forEach((hash, i) => {
133 doHash(algo.initString, messages[i], hash);
134 doHashStream(algo.initString, messages[i], hash);
136 algo.b64Hashes.forEach((hash, i) => {
137 testInitConstantAndBase64(
146 // Our buffer size for working with streams is 4096 bytes. This tests we
147 // handle larger inputs.
148 doHashStream("md5", " ".repeat(4100), "59f337d82f9ef5c9571bec4d78d66641");