source: node_modules/sha.js/sha256.js@ e48199a

main
Last change on this file since e48199a was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 3.2 KB
Line 
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
9var inherits = require('inherits')
10var Hash = require('./hash')
11var Buffer = require('safe-buffer').Buffer
12
13var K = [
14 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
15 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
16 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
17 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
18 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
19 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
20 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
21 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
22 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
23 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
24 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
25 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
26 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
27 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
28 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
29 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
30]
31
32var W = new Array(64)
33
34function Sha256 () {
35 this.init()
36
37 this._w = W // new Array(64)
38
39 Hash.call(this, 64, 56)
40}
41
42inherits(Sha256, Hash)
43
44Sha256.prototype.init = function () {
45 this._a = 0x6a09e667
46 this._b = 0xbb67ae85
47 this._c = 0x3c6ef372
48 this._d = 0xa54ff53a
49 this._e = 0x510e527f
50 this._f = 0x9b05688c
51 this._g = 0x1f83d9ab
52 this._h = 0x5be0cd19
53
54 return this
55}
56
57function ch (x, y, z) {
58 return z ^ (x & (y ^ z))
59}
60
61function maj (x, y, z) {
62 return (x & y) | (z & (x | y))
63}
64
65function sigma0 (x) {
66 return (x >>> 2 | x << 30) ^ (x >>> 13 | x << 19) ^ (x >>> 22 | x << 10)
67}
68
69function sigma1 (x) {
70 return (x >>> 6 | x << 26) ^ (x >>> 11 | x << 21) ^ (x >>> 25 | x << 7)
71}
72
73function gamma0 (x) {
74 return (x >>> 7 | x << 25) ^ (x >>> 18 | x << 14) ^ (x >>> 3)
75}
76
77function gamma1 (x) {
78 return (x >>> 17 | x << 15) ^ (x >>> 19 | x << 13) ^ (x >>> 10)
79}
80
81Sha256.prototype._update = function (M) {
82 var W = this._w
83
84 var a = this._a | 0
85 var b = this._b | 0
86 var c = this._c | 0
87 var d = this._d | 0
88 var e = this._e | 0
89 var f = this._f | 0
90 var g = this._g | 0
91 var h = this._h | 0
92
93 for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
94 for (; i < 64; ++i) W[i] = (gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16]) | 0
95
96 for (var j = 0; j < 64; ++j) {
97 var T1 = (h + sigma1(e) + ch(e, f, g) + K[j] + W[j]) | 0
98 var T2 = (sigma0(a) + maj(a, b, c)) | 0
99
100 h = g
101 g = f
102 f = e
103 e = (d + T1) | 0
104 d = c
105 c = b
106 b = a
107 a = (T1 + T2) | 0
108 }
109
110 this._a = (a + this._a) | 0
111 this._b = (b + this._b) | 0
112 this._c = (c + this._c) | 0
113 this._d = (d + this._d) | 0
114 this._e = (e + this._e) | 0
115 this._f = (f + this._f) | 0
116 this._g = (g + this._g) | 0
117 this._h = (h + this._h) | 0
118}
119
120Sha256.prototype._hash = function () {
121 var H = Buffer.allocUnsafe(32)
122
123 H.writeInt32BE(this._a, 0)
124 H.writeInt32BE(this._b, 4)
125 H.writeInt32BE(this._c, 8)
126 H.writeInt32BE(this._d, 12)
127 H.writeInt32BE(this._e, 16)
128 H.writeInt32BE(this._f, 20)
129 H.writeInt32BE(this._g, 24)
130 H.writeInt32BE(this._h, 28)
131
132 return H
133}
134
135module.exports = Sha256
Note: See TracBrowser for help on using the repository browser.