Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/product_registry/sha1.js

Issue 2766823002: [Devtools] Add SHA-1 hash function in prep for URL hashing (Closed)
Patch Set: make public Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/devtools/front_end/product_registry/module.json ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
caseq 2017/03/22 23:56:42 Please just keep the original copyright.
allada 2017/03/23 01:26:16 Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 /*
5 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
6 * in FIPS PUB 180-1
7 * Version 2.1a Copyright Paul Johnston 2000 - 2002.
8 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
9 * Distributed under the BSD License
10 * See http://pajhome.org.uk/crypt/md5 for details.
11 */
12 /**
13 * @param {string} str
14 * @return {string}
15 */
16 ProductRegistry.sha1 = function(str) {
17 return binb2hex(core_sha1(str2binb(str), str.length * 8));
18
19 /**
20 * Calculate the SHA-1 of an array of big-endian words, and a bit length
21 * @param {!Array<number>} x
22 * @param {number} len
23 * @return {!Array<number>}
24 */
25 function core_sha1(x, len) {
26 /* append padding */
27 x[len >> 5] |= 0x80 << (24 - len % 32);
28 x[((len + 64 >> 9) << 4) + 15] = len;
29
30 var w = Array(80);
31 var a = 1732584193;
32 var b = -271733879;
33 var c = -1732584194;
34 var d = 271733878;
35 var e = -1009589776;
36
37 for (var i = 0; i < x.length; i += 16) {
38 var olda = a;
39 var oldb = b;
40 var oldc = c;
41 var oldd = d;
42 var olde = e;
43
44 for (var j = 0; j < 80; j++) {
45 if (j < 16)
46 w[j] = x[i + j];
47 else
48 w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
49 var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe _add(e, w[j]), sha1_kt(j)));
50 e = d;
51 d = c;
52 c = rol(b, 30);
53 b = a;
54 a = t;
55 }
56
57 a = safe_add(a, olda);
58 b = safe_add(b, oldb);
59 c = safe_add(c, oldc);
60 d = safe_add(d, oldd);
61 e = safe_add(e, olde);
62 }
63 return Array(a, b, c, d, e);
64 }
65
66 /**
67 * Perform the appropriate triplet combination function for the current
68 * iteration
69 * @param {number} t
70 * @param {number} b
71 * @param {number} c
72 * @param {number} d
73 * @return {number}
74 */
75 function sha1_ft(t, b, c, d) {
76 if (t < 20)
77 return (b & c) | ((~b) & d);
78 if (t < 40)
79 return b ^ c ^ d;
80 if (t < 60)
81 return (b & c) | (b & d) | (c & d);
82 return b ^ c ^ d;
83 }
84
85 /**
86 * Determine the appropriate additive constant for the current iteration
87 * @param {number} t
88 * @return {number}
89 */
90 function sha1_kt(t) {
91 return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -189400758 8 : -899497514;
92 }
93
94 /**
95 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
96 * to work around bugs in some JS interpreters.
97 * @param {number} x
98 * @param {number} y
99 * @return {number}
100 */
101 function safe_add(x, y) {
102 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
103 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
104 return (msw << 16) | (lsw & 0xFFFF);
105 }
106
107 /**
108 * Bitwise rotate a 32-bit number to the left.
109 * @param {number} num
110 * @param {number} cnt
111 */
112 function rol(num, cnt) {
113 return (num << cnt) | (num >>> (32 - cnt));
114 }
115
116 /**
117 * Convert an 8-bit or 16-bit string to an array of big-endian words
118 * In 8-bit function, characters >255 have their hi-byte silently ignored.
119 * @param {string} str
120 * @return {!Array<number>}
121 */
122 function str2binb(str) {
123 var bin = Array();
124 var mask = 255;
125 for (var i = 0; i < str.length * 8; i += 8)
126 bin[i >> 5] |= (str.charCodeAt(i / 8) & mask) << (32 - 8 - i % 32);
127 return bin;
128 }
129
130 /**
131 * Convert an array of big-endian words to a hex string.
132 * @param {!Array<number>} binarray
133 * @return {string}
134 */
135 function binb2hex(binarray) {
136 const hex_tab = '0123456789abcdef';
137 var str = '';
138 for (var i = 0; i < binarray.length * 4; i++) {
139 str += hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) +
140 hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);
141 }
142 return str;
143 }
144 };
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/devtools/front_end/product_registry/module.json ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698