@swtc/common
Version:
swtc chain information
79 lines • 2.66 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlockHash = void 0;
const utils_1 = require("./utils");
const functions_1 = require("../functions");
class BlockHash {
constructor(blockSize = 512, outSize = 256, hmacStrength = 192, padLength = 64) {
this.pending = null;
this.pendingTotal = 0;
this.endian = "big";
this.blockSize = blockSize;
this.outSize = outSize;
this.hmacStrength = hmacStrength;
this.padLength = padLength / 8;
this._delta8 = this.blockSize / 8;
this._delta32 = this.blockSize / 32;
}
update(msg, enc = "") {
msg = utils_1.toArray(msg, enc);
if (!this.pending)
this.pending = msg;
else
this.pending = this.pending.concat(msg);
this.pendingTotal += msg.length;
if (this.pending.length >= this._delta8) {
msg = this.pending;
const r = msg.length % this._delta8;
this.pending = msg.slice(msg.length - r, msg.length);
if (this.pending.length === 0)
this.pending = null;
msg = utils_1.join32(msg, 0, msg.length - r, this.endian);
for (let i = 0; i < msg.length; i += this._delta32)
this._update(msg, i);
}
return this;
}
digest(enc = "") {
this.update(this.pad());
functions_1.funcAssert(this.pending === null);
return this._digest(enc);
}
pad() {
var len = this.pendingTotal;
var bytes = this._delta8;
var k = bytes - ((len + this.padLength) % bytes);
var res = new Array(k + this.padLength);
res[0] = 0x80;
for (var i = 1; i < k; i++)
res[i] = 0;
len <<= 3;
if (this.endian === "big") {
for (var t = 8; t < this.padLength; t++)
res[i++] = 0;
res[i++] = 0;
res[i++] = 0;
res[i++] = 0;
res[i++] = 0;
res[i++] = (len >>> 24) & 0xff;
res[i++] = (len >>> 16) & 0xff;
res[i++] = (len >>> 8) & 0xff;
res[i++] = len & 0xff;
}
else {
res[i++] = len & 0xff;
res[i++] = (len >>> 8) & 0xff;
res[i++] = (len >>> 16) & 0xff;
res[i++] = (len >>> 24) & 0xff;
res[i++] = 0;
res[i++] = 0;
res[i++] = 0;
res[i++] = 0;
for (t = 8; t < this.padLength; t++)
res[i++] = 0;
}
return res;
}
}
exports.BlockHash = BlockHash;
//# sourceMappingURL=common.js.map