test_hash_algorithms.js (4363B)
1 "use strict"; 2 3 // This file tests various aspects of the nsICryptoHash implementation for all 4 // of the supported algorithms. 5 6 const messages = ["The quick brown fox jumps over the lazy dog", ""]; 7 const ALGORITHMS = [ 8 { 9 initString: "md5", 10 initConstant: Ci.nsICryptoHash.MD5, 11 hexHashes: [ 12 "9e107d9d372bb6826bd81d3542a419d6", 13 "d41d8cd98f00b204e9800998ecf8427e", 14 ], 15 b64Hashes: ["nhB9nTcrtoJr2B01QqQZ1g==", "1B2M2Y8AsgTpgAmY7PhCfg=="], 16 }, 17 { 18 initString: "sha1", 19 initConstant: Ci.nsICryptoHash.SHA1, 20 hexHashes: [ 21 "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", 22 "da39a3ee5e6b4b0d3255bfef95601890afd80709", 23 ], 24 b64Hashes: ["L9ThxnotKPzthJ7hu3bnORuT6xI=", "2jmj7l5rSw0yVb/vlWAYkK/YBwk="], 25 }, 26 { 27 initString: "sha256", 28 initConstant: Ci.nsICryptoHash.SHA256, 29 hexHashes: [ 30 "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", 31 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 32 ], 33 b64Hashes: [ 34 "16j7swfXgJRpypq8sAguT41WUeRtPNt2LQLQvzfJ5ZI=", 35 "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", 36 ], 37 }, 38 { 39 initString: "sha384", 40 initConstant: Ci.nsICryptoHash.SHA384, 41 hexHashes: [ 42 "ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1", 43 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", 44 ], 45 b64Hashes: [ 46 "ynN/EBSkj0wLbdQ8sXewr9nlFpNnVExJQBHjMX2/mlCcseXcHoWpQbvuPX8q+8mx", 47 "OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb", 48 ], 49 }, 50 { 51 initString: "sha512", 52 initConstant: Ci.nsICryptoHash.SHA512, 53 hexHashes: [ 54 "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6", 55 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", 56 ], 57 b64Hashes: [ 58 "B+VH2VhvanP3P7rAQ17XaVEhj7fQyNeIownXhUNru2Quk6JSqVTyORJUfR6KO17W4b/XCXghIz+gU489uFT+5g==", 59 "z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==", 60 ], 61 }, 62 ]; 63 64 function doHash(algo, value, cmp) { 65 let hash = Cc["@mozilla.org/security/hash;1"].createInstance( 66 Ci.nsICryptoHash 67 ); 68 hash.initWithString(algo); 69 70 value = new TextEncoder().encode(value); 71 hash.update(value, value.length); 72 equal( 73 hexify(hash.finish(false)), 74 cmp, 75 `Actual and expected hash for ${algo} should match` 76 ); 77 78 hash.initWithString(algo); 79 hash.update(value, value.length); 80 equal( 81 hexify(hash.finish(false)), 82 cmp, 83 `Actual and expected hash for ${algo} should match after re-init` 84 ); 85 } 86 87 function doHashStream(algo, value, cmp) { 88 // TODO(Bug 459835): Make updateFromStream() accept zero length streams. 89 if (!value.length) { 90 return; 91 } 92 93 let hash = Cc["@mozilla.org/security/hash;1"].createInstance( 94 Ci.nsICryptoHash 95 ); 96 hash.initWithString(algo); 97 98 let stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance( 99 Ci.nsIStringInputStream 100 ); 101 stream.setUTF8Data(value); 102 hash.updateFromStream(stream, stream.available()); 103 equal( 104 hexify(hash.finish(false)), 105 cmp, 106 `Actual and expected hash for ${algo} should match updating from stream` 107 ); 108 } 109 110 function testInitConstantAndBase64( 111 initConstant, 112 algoName, 113 message, 114 expectedOutput 115 ) { 116 let value = new TextEncoder().encode(message); 117 118 let hash = Cc["@mozilla.org/security/hash;1"].createInstance( 119 Ci.nsICryptoHash 120 ); 121 hash.init(initConstant); 122 hash.update(value, value.length); 123 equal( 124 hash.finish(true), 125 expectedOutput, 126 `Actual and expected base64 hash for ${algoName} should match` 127 ); 128 } 129 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); 135 }); 136 algo.b64Hashes.forEach((hash, i) => { 137 testInitConstantAndBase64( 138 algo.initConstant, 139 algo.initString, 140 messages[i], 141 hash 142 ); 143 }); 144 } 145 146 // Our buffer size for working with streams is 4096 bytes. This tests we 147 // handle larger inputs. 148 doHashStream("md5", " ".repeat(4100), "59f337d82f9ef5c9571bec4d78d66641"); 149 }