react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / create-hash / node_modules / sha.js / sha256.js
80556 views/**1* A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined2* in FIPS 180-23* Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.4* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet5*6*/78var inherits = require('inherits')9var Hash = require('./hash')1011var K = [120x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,130x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,140xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,150x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,160xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,170x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,180x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,190xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,200x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,210x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,220xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,230xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,240x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,250x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,260x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,270x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F228]2930var W = new Array(64)3132function Sha256() {33this.init()3435this._w = W // new Array(64)3637Hash.call(this, 64, 56)38}3940inherits(Sha256, Hash)4142Sha256.prototype.init = function () {43this._a = 0x6a09e667|044this._b = 0xbb67ae85|045this._c = 0x3c6ef372|046this._d = 0xa54ff53a|047this._e = 0x510e527f|048this._f = 0x9b05688c|049this._g = 0x1f83d9ab|050this._h = 0x5be0cd19|05152return this53}5455function S (X, n) {56return (X >>> n) | (X << (32 - n));57}5859function R (X, n) {60return (X >>> n);61}6263function Ch (x, y, z) {64return ((x & y) ^ ((~x) & z));65}6667function Maj (x, y, z) {68return ((x & y) ^ (x & z) ^ (y & z));69}7071function Sigma0256 (x) {72return (S(x, 2) ^ S(x, 13) ^ S(x, 22));73}7475function Sigma1256 (x) {76return (S(x, 6) ^ S(x, 11) ^ S(x, 25));77}7879function Gamma0256 (x) {80return (S(x, 7) ^ S(x, 18) ^ R(x, 3));81}8283function Gamma1256 (x) {84return (S(x, 17) ^ S(x, 19) ^ R(x, 10));85}8687Sha256.prototype._update = function(M) {88var W = this._w8990var a = this._a | 091var b = this._b | 092var c = this._c | 093var d = this._d | 094var e = this._e | 095var f = this._f | 096var g = this._g | 097var h = this._h | 09899var j = 0100101function calcW() { return Gamma1256(W[j - 2]) + W[j - 7] + Gamma0256(W[j - 15]) + W[j - 16] }102function loop(w) {103W[j] = w104105var T1 = h + Sigma1256(e) + Ch(e, f, g) + K[j] + w106var T2 = Sigma0256(a) + Maj(a, b, c);107108h = g;109g = f;110f = e;111e = d + T1;112d = c;113c = b;114b = a;115a = T1 + T2;116117j++118}119120while (j < 16) loop(M.readInt32BE(j * 4))121while (j < 64) loop(calcW())122123this._a = (a + this._a) | 0124this._b = (b + this._b) | 0125this._c = (c + this._c) | 0126this._d = (d + this._d) | 0127this._e = (e + this._e) | 0128this._f = (f + this._f) | 0129this._g = (g + this._g) | 0130this._h = (h + this._h) | 0131};132133Sha256.prototype._hash = function () {134var H = new Buffer(32)135136H.writeInt32BE(this._a, 0)137H.writeInt32BE(this._b, 4)138H.writeInt32BE(this._c, 8)139H.writeInt32BE(this._d, 12)140H.writeInt32BE(this._e, 16)141H.writeInt32BE(this._f, 20)142H.writeInt32BE(this._g, 24)143H.writeInt32BE(this._h, 28)144145return H146}147148module.exports = Sha256149150151