devexpress-richedit
Version:
DevExpress Rich Text Editor is an advanced word-processing tool designed for working with rich text documents.
78 lines (77 loc) • 3.55 kB
JavaScript
export class SHA256 {
constructor() {
this._constants = [1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993, -1841331548, -1424204075, -670586216,
310598401, 607225278, 1426881987, 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522, 264347078, 604807628,
770255983, 1249150122, 1555081692, 1996064986, -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585, 113926993,
338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885,
-1035236496, -949202525, -778901479, -694614492, -200395387, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571,
1322822218, 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872, -1866530822, -1538233109, -1090935817,
-965641998];
}
computeHashCore(words) {
const constants = this._constants;
const hash = this._hash;
const temp = [];
const offset = 0;
let a = hash[0];
let b = hash[1];
let c = hash[2];
let d = hash[3];
let e = hash[4];
let f = hash[5];
let g = hash[6];
let h = hash[7];
for (let i = 0; i < 64; i++) {
if (i < 16) {
temp[i] = words[offset + i] | 0;
}
else {
const gamma0x = temp[i - 15];
const gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^
((gamma0x << 14) | (gamma0x >>> 18)) ^
(gamma0x >>> 3);
const gamma1x = temp[i - 2];
const gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^
((gamma1x << 13) | (gamma1x >>> 19)) ^
(gamma1x >>> 10);
temp[i] = gamma0 + temp[i - 7] + gamma1 + temp[i - 16];
}
const ch = (e & f) ^ (~e & g);
const maj = (a & b) ^ (a & c) ^ (b & c);
const sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22));
const sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7) | (e >>> 25));
const t1 = h + sigma1 + ch + constants[i] + temp[i];
const t2 = sigma0 + maj;
h = g;
g = f;
f = e;
e = (d + t1) | 0;
d = c;
c = b;
b = a;
a = (t1 + t2) | 0;
}
hash[0] = (hash[0] + a) | 0;
hash[1] = (hash[1] + b) | 0;
hash[2] = (hash[2] + c) | 0;
hash[3] = (hash[3] + d) | 0;
hash[4] = (hash[4] + e) | 0;
hash[5] = (hash[5] + f) | 0;
hash[6] = (hash[6] + g) | 0;
hash[7] = (hash[7] + h) | 0;
}
resetCache() {
this._hash = [1779033703, -1150833019, 1013904242, -1521486534, 1359893119, -1694144372, 528734635, 1541459225];
}
computeHash(source) {
this.resetCache();
const dataWords = source;
const nBitsTotal = source.length * 4 * 8;
const nBitsLeft = source.length * 4 * 8;
dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
this.computeHashCore(dataWords);
return this._hash;
}
}