security/manager/ssl/tests/unit/test_hash_algorithms.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 var ScriptableUnicodeConverter =
michael@0 2 Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter",
michael@0 3 "nsIScriptableUnicodeConverter");
michael@0 4 var CryptoHash =
michael@0 5 Components.Constructor("@mozilla.org/security/hash;1",
michael@0 6 "nsICryptoHash",
michael@0 7 "initWithString");
michael@0 8
michael@0 9 var messages = [
michael@0 10 "The quick brown fox jumps over the lazy dog",
michael@0 11 ""
michael@0 12 ];
michael@0 13 var hashes = {
michael@0 14 md2: [
michael@0 15 "03d85a0d629d2c442e987525319fc471",
michael@0 16 "8350e5a3e24c153df2275c9f80692773"
michael@0 17 ],
michael@0 18 md5: [
michael@0 19 "9e107d9d372bb6826bd81d3542a419d6",
michael@0 20 "d41d8cd98f00b204e9800998ecf8427e"
michael@0 21 ],
michael@0 22 sha1: [
michael@0 23 "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
michael@0 24 "da39a3ee5e6b4b0d3255bfef95601890afd80709"
michael@0 25 ],
michael@0 26 sha256: [
michael@0 27 "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592",
michael@0 28 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
michael@0 29 ],
michael@0 30 sha384: [
michael@0 31 "ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1",
michael@0 32 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"
michael@0 33 ],
michael@0 34 sha512: [
michael@0 35 "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6",
michael@0 36 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
michael@0 37 ]
michael@0 38 };
michael@0 39
michael@0 40 function hexdigest(data) {
michael@0 41 /*
michael@0 42 * Coment taken from bug 383390:
michael@0 43 *
michael@0 44 * First, |data| is the final string value produced by the cryptohash. |for (i in
michael@0 45 * data)| uses the Mozilla JS extension that iterating over a string iterates over
michael@0 46 * its character indexes, so that's 0..length-1 over the hash string.
michael@0 47 *
michael@0 48 * Returning to the left, the |charCodeAt| gets the value of the character at that
michael@0 49 * index in the string.
michael@0 50 *
michael@0 51 * |slice(-2)| is equivalent to |slice(length of this string - 2)| as a convenient
michael@0 52 * way of wrapping around to the other end of a string without doing the actual
michael@0 53 * calculation. When provided with only one argument, slice selects to the end of
michael@0 54 * the string, so this chomps off the last two characters of the string.
michael@0 55 *
michael@0 56 * The last-two-characters part clarifies the |"0" +| -- if the Unicode value is
michael@0 57 * <10, we have a single-character hex string when we want one that's two
michael@0 58 * characters, and unconditionally prepending a "0" solves the problem.
michael@0 59 *
michael@0 60 * The array comprehension just creates an array whose elements are these
michael@0 61 * two-character strings.
michael@0 62 */
michael@0 63 return [("0" + data.charCodeAt(i).toString(16)).slice(-2) for (i in data)].join("");
michael@0 64 }
michael@0 65
michael@0 66 function doHash(algo, value, cmp) {
michael@0 67 var converter = new ScriptableUnicodeConverter();
michael@0 68 var hash = new CryptoHash(algo);
michael@0 69
michael@0 70 converter.charset = 'utf8';
michael@0 71 value = converter.convertToByteArray(value);
michael@0 72 hash.update(value, value.length);
michael@0 73 var hash1 = hexdigest(hash.finish(false));
michael@0 74 if (cmp != hash1) {
michael@0 75 do_throw("Hash mismatch!\n" +
michael@0 76 " Expected: " + cmp + "\n" +
michael@0 77 " Actual: " + hash1 + "\n" +
michael@0 78 " Algo: " + algo);
michael@0 79 }
michael@0 80
michael@0 81 hash.initWithString(algo);
michael@0 82 hash.update(value, value.length);
michael@0 83 var hash2 = hexdigest(hash.finish(false));
michael@0 84 if (cmp != hash2) {
michael@0 85 do_throw("Hash mismatch after crypto hash re-init!\n" +
michael@0 86 " Expected: " + cmp + "\n" +
michael@0 87 " Actual: " + hash2 + "\n" +
michael@0 88 " Algo: " + algo);
michael@0 89 }
michael@0 90 }
michael@0 91
michael@0 92 function doHashStream(algo, value, cmp) {
michael@0 93 var converter = new ScriptableUnicodeConverter();
michael@0 94 var hash = new CryptoHash(algo);
michael@0 95
michael@0 96 converter.charset = 'utf8';
michael@0 97 var stream = converter.convertToInputStream(value);
michael@0 98 hash.updateFromStream(stream, stream.available());
michael@0 99 hash = hexdigest(hash.finish(false));
michael@0 100 if (cmp != hash) {
michael@0 101 do_throw("Hash mismatch!\n" +
michael@0 102 " Expected: " + cmp + "\n" +
michael@0 103 " Actual: " + hash + "\n" +
michael@0 104 " Algo: " + algo);
michael@0 105 }
michael@0 106 }
michael@0 107
michael@0 108 function run_test() {
michael@0 109 for (algo in hashes) {
michael@0 110 hashes[algo].forEach(
michael@0 111 function(e, i) {
michael@0 112 doHash(algo, messages[i], e);
michael@0 113
michael@0 114 if (messages[i].length) {
michael@0 115 // this test doesn't work for empty string/stream
michael@0 116 doHashStream(algo, messages[i], e);
michael@0 117 }
michael@0 118 }
michael@0 119 );
michael@0 120 }
michael@0 121 }

mercurial