126 lines
3.1 KiB
JavaScript
126 lines
3.1 KiB
JavaScript
import { Hasher, Hasher32, WordArray } from "./core.mjs";
|
|
|
|
//#region src/sha256.ts
|
|
const { H, K } = /* @__PURE__ */ (() => {
|
|
const _H = [];
|
|
const _K = [];
|
|
const isPrime = (n$1) => {
|
|
const sqrtN = Math.sqrt(n$1);
|
|
for (let factor = 2; factor <= sqrtN; factor += 1) if (!(n$1 % factor)) return false;
|
|
return true;
|
|
};
|
|
const getFractionalBits = (n$1) => (n$1 - (n$1 | 0)) * 4294967296 | 0;
|
|
let n = 2;
|
|
let nPrime = 0;
|
|
while (nPrime < 64) {
|
|
if (isPrime(n)) {
|
|
if (nPrime < 8) _H[nPrime] = getFractionalBits(n ** (1 / 2));
|
|
_K[nPrime] = getFractionalBits(n ** (1 / 3));
|
|
nPrime += 1;
|
|
}
|
|
n += 1;
|
|
}
|
|
return {
|
|
H: _H,
|
|
K: _K
|
|
};
|
|
})();
|
|
const W = [];
|
|
/**
|
|
* SHA-256 hash algorithm.
|
|
*/
|
|
var SHA256Algo = class extends Hasher32 {
|
|
_doReset() {
|
|
this._hash = new WordArray(H.slice(0));
|
|
}
|
|
_doProcessBlock(M, offset) {
|
|
const _H = this._hash.words;
|
|
let a = _H[0];
|
|
let b = _H[1];
|
|
let c = _H[2];
|
|
let d = _H[3];
|
|
let e = _H[4];
|
|
let f = _H[5];
|
|
let g = _H[6];
|
|
let h = _H[7];
|
|
for (let i = 0; i < 64; i += 1) {
|
|
if (i < 16) W[i] = M[offset + i] | 0;
|
|
else {
|
|
const gamma0x = W[i - 15];
|
|
const gamma0 = (gamma0x << 25 | gamma0x >>> 7) ^ (gamma0x << 14 | gamma0x >>> 18) ^ gamma0x >>> 3;
|
|
const gamma1x = W[i - 2];
|
|
const gamma1 = (gamma1x << 15 | gamma1x >>> 17) ^ (gamma1x << 13 | gamma1x >>> 19) ^ gamma1x >>> 10;
|
|
W[i] = gamma0 + W[i - 7] + gamma1 + W[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 + K[i] + W[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;
|
|
}
|
|
_H[0] = _H[0] + a | 0;
|
|
_H[1] = _H[1] + b | 0;
|
|
_H[2] = _H[2] + c | 0;
|
|
_H[3] = _H[3] + d | 0;
|
|
_H[4] = _H[4] + e | 0;
|
|
_H[5] = _H[5] + f | 0;
|
|
_H[6] = _H[6] + g | 0;
|
|
_H[7] = _H[7] + h | 0;
|
|
}
|
|
_doFinalize() {
|
|
const data = this._data;
|
|
const dataWords = data.words;
|
|
const nBitsTotal = this._nDataBytes * 8;
|
|
const nBitsLeft = data.sigBytes * 8;
|
|
dataWords[nBitsLeft >>> 5] |= 128 << 24 - nBitsLeft % 32;
|
|
dataWords[(nBitsLeft + 64 >>> 9 << 4) + 14] = Math.floor(nBitsTotal / 4294967296);
|
|
dataWords[(nBitsLeft + 64 >>> 9 << 4) + 15] = nBitsTotal;
|
|
data.sigBytes = dataWords.length * 4;
|
|
this._process();
|
|
return this._hash;
|
|
}
|
|
clone() {
|
|
const clone = super.clone.call(this);
|
|
clone._hash = this._hash.clone();
|
|
return clone;
|
|
}
|
|
};
|
|
/**
|
|
* Shortcut function to the hasher's object interface.
|
|
*
|
|
* @param message - The message to hash.
|
|
* @returns The hash.
|
|
*
|
|
* @example
|
|
* ```js
|
|
* const hash = SHA256('message');
|
|
* const hash = SHA256(wordArray);
|
|
* ```
|
|
*/
|
|
const SHA256 = Hasher._createHelper(SHA256Algo);
|
|
/**
|
|
* Shortcut function to the HMAC's object interface.
|
|
*
|
|
* @param message - The message to hash.
|
|
* @param key - The secret key.
|
|
* @returns The HMAC.
|
|
*
|
|
* @example
|
|
* ```js
|
|
* const hmac = HmacSHA256(message, key);
|
|
* ```
|
|
*/
|
|
const HmacSHA256 = Hasher._createHmacHelper(SHA256Algo);
|
|
|
|
//#endregion
|
|
export { HmacSHA256, SHA256, SHA256Algo };
|
|
//# sourceMappingURL=sha256.mjs.map
|