Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80556 views
1
/**
2
* A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
3
* in FIPS 180-2
4
* Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.
5
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
6
*
7
*/
8
9
var inherits = require('inherits')
10
var Hash = require('./hash')
11
12
var K = [
13
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
14
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
15
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
16
0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
17
0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
18
0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
19
0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
20
0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
21
0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
22
0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
23
0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
24
0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
25
0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
26
0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
27
0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
28
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
29
]
30
31
var W = new Array(64)
32
33
function Sha256() {
34
this.init()
35
36
this._w = W // new Array(64)
37
38
Hash.call(this, 64, 56)
39
}
40
41
inherits(Sha256, Hash)
42
43
Sha256.prototype.init = function () {
44
this._a = 0x6a09e667|0
45
this._b = 0xbb67ae85|0
46
this._c = 0x3c6ef372|0
47
this._d = 0xa54ff53a|0
48
this._e = 0x510e527f|0
49
this._f = 0x9b05688c|0
50
this._g = 0x1f83d9ab|0
51
this._h = 0x5be0cd19|0
52
53
return this
54
}
55
56
function S (X, n) {
57
return (X >>> n) | (X << (32 - n));
58
}
59
60
function R (X, n) {
61
return (X >>> n);
62
}
63
64
function Ch (x, y, z) {
65
return ((x & y) ^ ((~x) & z));
66
}
67
68
function Maj (x, y, z) {
69
return ((x & y) ^ (x & z) ^ (y & z));
70
}
71
72
function Sigma0256 (x) {
73
return (S(x, 2) ^ S(x, 13) ^ S(x, 22));
74
}
75
76
function Sigma1256 (x) {
77
return (S(x, 6) ^ S(x, 11) ^ S(x, 25));
78
}
79
80
function Gamma0256 (x) {
81
return (S(x, 7) ^ S(x, 18) ^ R(x, 3));
82
}
83
84
function Gamma1256 (x) {
85
return (S(x, 17) ^ S(x, 19) ^ R(x, 10));
86
}
87
88
Sha256.prototype._update = function(M) {
89
var W = this._w
90
91
var a = this._a | 0
92
var b = this._b | 0
93
var c = this._c | 0
94
var d = this._d | 0
95
var e = this._e | 0
96
var f = this._f | 0
97
var g = this._g | 0
98
var h = this._h | 0
99
100
var j = 0
101
102
function calcW() { return Gamma1256(W[j - 2]) + W[j - 7] + Gamma0256(W[j - 15]) + W[j - 16] }
103
function loop(w) {
104
W[j] = w
105
106
var T1 = h + Sigma1256(e) + Ch(e, f, g) + K[j] + w
107
var T2 = Sigma0256(a) + Maj(a, b, c);
108
109
h = g;
110
g = f;
111
f = e;
112
e = d + T1;
113
d = c;
114
c = b;
115
b = a;
116
a = T1 + T2;
117
118
j++
119
}
120
121
while (j < 16) loop(M.readInt32BE(j * 4))
122
while (j < 64) loop(calcW())
123
124
this._a = (a + this._a) | 0
125
this._b = (b + this._b) | 0
126
this._c = (c + this._c) | 0
127
this._d = (d + this._d) | 0
128
this._e = (e + this._e) | 0
129
this._f = (f + this._f) | 0
130
this._g = (g + this._g) | 0
131
this._h = (h + this._h) | 0
132
};
133
134
Sha256.prototype._hash = function () {
135
var H = new Buffer(32)
136
137
H.writeInt32BE(this._a, 0)
138
H.writeInt32BE(this._b, 4)
139
H.writeInt32BE(this._c, 8)
140
H.writeInt32BE(this._d, 12)
141
H.writeInt32BE(this._e, 16)
142
H.writeInt32BE(this._f, 20)
143
H.writeInt32BE(this._g, 24)
144
H.writeInt32BE(this._h, 28)
145
146
return H
147
}
148
149
module.exports = Sha256
150
151