| OLD | NEW |
| 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); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 SHA256.prototype.reset = function() { | 44 SHA256.prototype.reset = function() { |
| 45 this._chain = [ | 45 this._chain = [ |
| 46 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, | 46 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, |
| 47 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]; | 47 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]; |
| 48 | 48 |
| 49 this._inbuf = 0; | 49 this._inbuf = 0; |
| 50 this._total = 0; | 50 this._total = 0; |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 /** Hash the next block of 64 bytes | 53 /** Hash the next block of 64 bytes |
| 54 * @param {Array.<number>} buf A 64 byte buffer | 54 * @param {Array<number>} buf A 64 byte buffer |
| 55 */ | 55 */ |
| 56 SHA256.prototype._compress = function(buf) { | 56 SHA256.prototype._compress = function(buf) { |
| 57 var W = this._W; | 57 var W = this._W; |
| 58 var k = this._k; | 58 var k = this._k; |
| 59 | 59 |
| 60 function _rotr(w, r) { return ((w << (32 - r)) | (w >>> r)); }; | 60 function _rotr(w, r) { return ((w << (32 - r)) | (w >>> r)); }; |
| 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 = (buf[i] << 24) | |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 this._chain[1] += B; | 106 this._chain[1] += B; |
| 107 this._chain[2] += C; | 107 this._chain[2] += C; |
| 108 this._chain[3] += D; | 108 this._chain[3] += D; |
| 109 this._chain[4] += E; | 109 this._chain[4] += E; |
| 110 this._chain[5] += F; | 110 this._chain[5] += F; |
| 111 this._chain[6] += G; | 111 this._chain[6] += G; |
| 112 this._chain[7] += H; | 112 this._chain[7] += H; |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 /** Update the hash with additional data | 115 /** Update the hash with additional data |
| 116 * @param {Array.<number>|Uint8Array} bytes The data | 116 * @param {Array<number>|Uint8Array} bytes The data |
| 117 * @param {number=} opt_length How many bytes to hash, if not all */ | 117 * @param {number=} opt_length How many bytes to hash, if not all */ |
| 118 SHA256.prototype.update = function(bytes, opt_length) { | 118 SHA256.prototype.update = function(bytes, opt_length) { |
| 119 if (!opt_length) opt_length = bytes.length; | 119 if (!opt_length) opt_length = bytes.length; |
| 120 | 120 |
| 121 this._total += opt_length; | 121 this._total += opt_length; |
| 122 for (var n = 0; n < opt_length; ++n) { | 122 for (var n = 0; n < opt_length; ++n) { |
| 123 this._buf[this._inbuf++] = bytes[n]; | 123 this._buf[this._inbuf++] = bytes[n]; |
| 124 if (this._inbuf == 64) { | 124 if (this._inbuf == 64) { |
| 125 this._compress(this._buf); | 125 this._compress(this._buf); |
| 126 this._inbuf = 0; | 126 this._inbuf = 0; |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 }; | 129 }; |
| 130 | 130 |
| 131 /** Update the hash with a specified range from a data buffer | 131 /** Update the hash with a specified range from a data buffer |
| 132 * @param {Array.<number>} bytes The data buffer | 132 * @param {Array<number>} bytes The data buffer |
| 133 * @param {number} start Starting index of the range in bytes | 133 * @param {number} start Starting index of the range in bytes |
| 134 * @param {number} end End index, will not be included in range | 134 * @param {number} end End index, will not be included in range |
| 135 */ | 135 */ |
| 136 SHA256.prototype.updateRange = function(bytes, start, end) { | 136 SHA256.prototype.updateRange = function(bytes, start, end) { |
| 137 this._total += (end - start); | 137 this._total += (end - start); |
| 138 for (var n = start; n < end; ++n) { | 138 for (var n = start; n < end; ++n) { |
| 139 this._buf[this._inbuf++] = bytes[n]; | 139 this._buf[this._inbuf++] = bytes[n]; |
| 140 if (this._inbuf == 64) { | 140 if (this._inbuf == 64) { |
| 141 this._compress(this._buf); | 141 this._compress(this._buf); |
| 142 this._inbuf = 0; | 142 this._inbuf = 0; |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 }; | 145 }; |
| 146 | 146 |
| 147 /** | 147 /** |
| 148 * Optionally update the hash with additional arguments, and return the | 148 * Optionally update the hash with additional arguments, and return the |
| 149 * resulting hash value. | 149 * resulting hash value. |
| 150 * @param {...*} var_args Data buffers to hash | 150 * @param {...*} var_args Data buffers to hash |
| 151 * @return {Array.<number>} the SHA256 hash value. | 151 * @return {Array<number>} the SHA256 hash value. |
| 152 */ | 152 */ |
| 153 SHA256.prototype.digest = function(var_args) { | 153 SHA256.prototype.digest = function(var_args) { |
| 154 for (var i = 0; i < arguments.length; ++i) | 154 for (var i = 0; i < arguments.length; ++i) |
| 155 this.update(arguments[i]); | 155 this.update(arguments[i]); |
| 156 | 156 |
| 157 var digest = new Array(32); | 157 var digest = new Array(32); |
| 158 var totalBits = this._total * 8; | 158 var totalBits = this._total * 8; |
| 159 | 159 |
| 160 // add pad 0x80 0x00* | 160 // add pad 0x80 0x00* |
| 161 if (this._inbuf < 56) | 161 if (this._inbuf < 56) |
| 162 this.update(this._pad, 56 - this._inbuf); | 162 this.update(this._pad, 56 - this._inbuf); |
| 163 else | 163 else |
| 164 this.update(this._pad, 64 - (this._inbuf - 56)); | 164 this.update(this._pad, 64 - (this._inbuf - 56)); |
| 165 | 165 |
| 166 // add # bits, big endian | 166 // add # bits, big endian |
| 167 for (var i = 63; i >= 56; --i) { | 167 for (var i = 63; i >= 56; --i) { |
| 168 this._buf[i] = totalBits & 255; | 168 this._buf[i] = totalBits & 255; |
| 169 totalBits >>>= 8; | 169 totalBits >>>= 8; |
| 170 } | 170 } |
| 171 | 171 |
| 172 this._compress(this._buf); | 172 this._compress(this._buf); |
| 173 | 173 |
| 174 var n = 0; | 174 var n = 0; |
| 175 for (var i = 0; i < 8; ++i) | 175 for (var i = 0; i < 8; ++i) |
| 176 for (var j = 24; j >= 0; j -= 8) | 176 for (var j = 24; j >= 0; j -= 8) |
| 177 digest[n++] = (this._chain[i] >> j) & 255; | 177 digest[n++] = (this._chain[i] >> j) & 255; |
| 178 | 178 |
| 179 return digest; | 179 return digest; |
| 180 }; | 180 }; |
| OLD | NEW |