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

Side by Side Diff: chrome/browser/resources/cryptotoken/sha256.js

Issue 2939273002: DO NOT SUBMIT: what chrome/browser/resources/ could eventually look like with clang-format (Closed)
Patch Set: Created 3 years, 6 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // SHA256 in javascript. 5 // SHA256 in javascript.
6 // 6 //
7 // SHA256 { 7 // SHA256 {
8 // SHA256(); 8 // SHA256();
9 // void reset(); 9 // void reset();
10 // void update(byte[] data, opt_length); 10 // void update(byte[] data, opt_length);
11 // byte[32] digest(); 11 // byte[32] digest();
12 // } 12 // }
13 13
14 /** @constructor */ 14 /** @constructor */
15 function SHA256() { 15 function SHA256() {
16 this._buf = new Array(64); 16 this._buf = new Array(64);
17 this._W = new Array(64); 17 this._W = new Array(64);
18 this._pad = new Array(64); 18 this._pad = new Array(64);
19 this._k = [ 19 this._k = [
20 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 20 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
21 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 21 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
22 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 22 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
23 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 23 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
24 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 24 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
25 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 25 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
26 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 26 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
27 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 27 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
28 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 28 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
29 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 29 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
30 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 30 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
31 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 31 ];
32 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
33 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
34 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
35 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2];
36 32
37 this._pad[0] = 0x80; 33 this._pad[0] = 0x80;
38 for (var i = 1; i < 64; ++i) this._pad[i] = 0; 34 for (var i = 1; i < 64; ++i)
35 this._pad[i] = 0;
39 36
40 this.reset(); 37 this.reset();
41 } 38 }
42 39
43 /** Reset the hasher */ 40 /** Reset the hasher */
44 SHA256.prototype.reset = function() { 41 SHA256.prototype.reset = function() {
45 this._chain = [ 42 this._chain = [
46 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 43 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c,
47 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]; 44 0x1f83d9ab, 0x5be0cd19
45 ];
48 46
49 this._inbuf = 0; 47 this._inbuf = 0;
50 this._total = 0; 48 this._total = 0;
51 }; 49 };
52 50
53 /** Hash the next block of 64 bytes 51 /** Hash the next block of 64 bytes
54 * @param {Array<number>} buf A 64 byte buffer 52 * @param {Array<number>} buf A 64 byte buffer
55 */ 53 */
56 SHA256.prototype._compress = function(buf) { 54 SHA256.prototype._compress = function(buf) {
57 var W = this._W; 55 var W = this._W;
58 var k = this._k; 56 var k = this._k;
59 57
60 function _rotr(w, r) { return ((w << (32 - r)) | (w >>> r)); } 58 function _rotr(w, r) {
59 return ((w << (32 - r)) | (w >>> r));
60 }
61 61
62 // get 16 big endian words 62 // get 16 big endian words
63 for (var i = 0; i < 64; i += 4) { 63 for (var i = 0; i < 64; i += 4) {
64 var w = (buf[i] << 24) | 64 var w =
65 (buf[i + 1] << 16) | 65 (buf[i] << 24) | (buf[i + 1] << 16) | (buf[i + 2] << 8) | (buf[i + 3]);
66 (buf[i + 2] << 8) |
67 (buf[i + 3]);
68 W[i / 4] = w; 66 W[i / 4] = w;
69 } 67 }
70 68
71 // expand to 64 words 69 // expand to 64 words
72 for (var i = 16; i < 64; ++i) { 70 for (var i = 16; i < 64; ++i) {
73 var s0 = _rotr(W[i - 15], 7) ^ _rotr(W[i - 15], 18) ^ (W[i - 15] >>> 3); 71 var s0 = _rotr(W[i - 15], 7) ^ _rotr(W[i - 15], 18) ^ (W[i - 15] >>> 3);
74 var s1 = _rotr(W[i - 2], 17) ^ _rotr(W[i - 2], 19) ^ (W[i - 2] >>> 10); 72 var s1 = _rotr(W[i - 2], 17) ^ _rotr(W[i - 2], 19) ^ (W[i - 2] >>> 10);
75 W[i] = (W[i - 16] + s0 + W[i - 7] + s1) & 0xffffffff; 73 W[i] = (W[i - 16] + s0 + W[i - 7] + s1) & 0xffffffff;
76 } 74 }
77 75
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 this._chain[4] += E; 107 this._chain[4] += E;
110 this._chain[5] += F; 108 this._chain[5] += F;
111 this._chain[6] += G; 109 this._chain[6] += G;
112 this._chain[7] += H; 110 this._chain[7] += H;
113 }; 111 };
114 112
115 /** Update the hash with additional data 113 /** Update the hash with additional data
116 * @param {Array<number>|Uint8Array} bytes The data 114 * @param {Array<number>|Uint8Array} bytes The data
117 * @param {number=} opt_length How many bytes to hash, if not all */ 115 * @param {number=} opt_length How many bytes to hash, if not all */
118 SHA256.prototype.update = function(bytes, opt_length) { 116 SHA256.prototype.update = function(bytes, opt_length) {
119 if (!opt_length) opt_length = bytes.length; 117 if (!opt_length)
118 opt_length = bytes.length;
120 119
121 this._total += opt_length; 120 this._total += opt_length;
122 for (var n = 0; n < opt_length; ++n) { 121 for (var n = 0; n < opt_length; ++n) {
123 this._buf[this._inbuf++] = bytes[n]; 122 this._buf[this._inbuf++] = bytes[n];
124 if (this._inbuf == 64) { 123 if (this._inbuf == 64) {
125 this._compress(this._buf); 124 this._compress(this._buf);
126 this._inbuf = 0; 125 this._inbuf = 0;
127 } 126 }
128 } 127 }
129 }; 128 };
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 170
172 this._compress(this._buf); 171 this._compress(this._buf);
173 172
174 var n = 0; 173 var n = 0;
175 for (var i = 0; i < 8; ++i) 174 for (var i = 0; i < 8; ++i)
176 for (var j = 24; j >= 0; j -= 8) 175 for (var j = 24; j >= 0; j -= 8)
177 digest[n++] = (this._chain[i] >> j) & 255; 176 digest[n++] = (this._chain[i] >> j) & 255;
178 177
179 return digest; 178 return digest;
180 }; 179 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698