UNPKG

5.54 kBJavaScriptView Raw
1;(function (root, factory) {
2 if (typeof exports === "object") {
3 // CommonJS
4 module.exports = exports = factory(require("./core"));
5 }
6 else if (typeof define === "function" && define.amd) {
7 // AMD
8 define(["./core"], factory);
9 }
10 else {
11 // Global (browser)
12 factory(root.CryptoJS);
13 }
14}(this, function (CryptoJS) {
15
16 (function (Math) {
17 // Shortcuts
18 var C = CryptoJS;
19 var C_lib = C.lib;
20 var WordArray = C_lib.WordArray;
21 var Hasher = C_lib.Hasher;
22 var C_algo = C.algo;
23
24 // Initialization and round constants tables
25 var H = [];
26 var K = [];
27
28 // Compute constants
29 (function () {
30 function isPrime(n) {
31 var sqrtN = Math.sqrt(n);
32 for (var factor = 2; factor <= sqrtN; factor++) {
33 if (!(n % factor)) {
34 return false;
35 }
36 }
37
38 return true;
39 }
40
41 function getFractionalBits(n) {
42 return ((n - (n | 0)) * 0x100000000) | 0;
43 }
44
45 var n = 2;
46 var nPrime = 0;
47 while (nPrime < 64) {
48 if (isPrime(n)) {
49 if (nPrime < 8) {
50 H[nPrime] = getFractionalBits(Math.pow(n, 1 / 2));
51 }
52 K[nPrime] = getFractionalBits(Math.pow(n, 1 / 3));
53
54 nPrime++;
55 }
56
57 n++;
58 }
59 }());
60
61 // Reusable object
62 var W = [];
63
64 /**
65 * SHA-256 hash algorithm.
66 */
67 var SHA256 = C_algo.SHA256 = Hasher.extend({
68 _doReset: function () {
69 this._hash = new WordArray.init(H.slice(0));
70 },
71
72 _doProcessBlock: function (M, offset) {
73 // Shortcut
74 var H = this._hash.words;
75
76 // Working variables
77 var a = H[0];
78 var b = H[1];
79 var c = H[2];
80 var d = H[3];
81 var e = H[4];
82 var f = H[5];
83 var g = H[6];
84 var h = H[7];
85
86 // Computation
87 for (var i = 0; i < 64; i++) {
88 if (i < 16) {
89 W[i] = M[offset + i] | 0;
90 } else {
91 var gamma0x = W[i - 15];
92 var gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^
93 ((gamma0x << 14) | (gamma0x >>> 18)) ^
94 (gamma0x >>> 3);
95
96 var gamma1x = W[i - 2];
97 var gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^
98 ((gamma1x << 13) | (gamma1x >>> 19)) ^
99 (gamma1x >>> 10);
100
101 W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16];
102 }
103
104 var ch = (e & f) ^ (~e & g);
105 var maj = (a & b) ^ (a & c) ^ (b & c);
106
107 var sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22));
108 var sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7) | (e >>> 25));
109
110 var t1 = h + sigma1 + ch + K[i] + W[i];
111 var t2 = sigma0 + maj;
112
113 h = g;
114 g = f;
115 f = e;
116 e = (d + t1) | 0;
117 d = c;
118 c = b;
119 b = a;
120 a = (t1 + t2) | 0;
121 }
122
123 // Intermediate hash value
124 H[0] = (H[0] + a) | 0;
125 H[1] = (H[1] + b) | 0;
126 H[2] = (H[2] + c) | 0;
127 H[3] = (H[3] + d) | 0;
128 H[4] = (H[4] + e) | 0;
129 H[5] = (H[5] + f) | 0;
130 H[6] = (H[6] + g) | 0;
131 H[7] = (H[7] + h) | 0;
132 },
133
134 _doFinalize: function () {
135 // Shortcuts
136 var data = this._data;
137 var dataWords = data.words;
138
139 var nBitsTotal = this._nDataBytes * 8;
140 var nBitsLeft = data.sigBytes * 8;
141
142 // Add padding
143 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
144 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
145 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
146 data.sigBytes = dataWords.length * 4;
147
148 // Hash final blocks
149 this._process();
150
151 // Return final computed hash
152 return this._hash;
153 },
154
155 clone: function () {
156 var clone = Hasher.clone.call(this);
157 clone._hash = this._hash.clone();
158
159 return clone;
160 }
161 });
162
163 /**
164 * Shortcut function to the hasher's object interface.
165 *
166 * @param {WordArray|string} message The message to hash.
167 *
168 * @return {WordArray} The hash.
169 *
170 * @static
171 *
172 * @example
173 *
174 * var hash = CryptoJS.SHA256('message');
175 * var hash = CryptoJS.SHA256(wordArray);
176 */
177 C.SHA256 = Hasher._createHelper(SHA256);
178
179 /**
180 * Shortcut function to the HMAC's object interface.
181 *
182 * @param {WordArray|string} message The message to hash.
183 * @param {WordArray|string} key The secret key.
184 *
185 * @return {WordArray} The HMAC.
186 *
187 * @static
188 *
189 * @example
190 *
191 * var hmac = CryptoJS.HmacSHA256(message, key);
192 */
193 C.HmacSHA256 = Hasher._createHmacHelper(SHA256);
194 }(Math));
195
196
197 return CryptoJS.SHA256;
198
199}));
\No newline at end of file