@ecash/lib
Version:
Library for eCash transaction building
63 lines • 1.92 kB
JavaScript
;
// Copyright (c) 2025 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
Object.defineProperty(exports, "__esModule", { value: true });
exports.hmacSha512 = exports.hmacSha256 = exports.Hmac = void 0;
const hash_1 = require("./hash");
class Hmac {
constructor(hashFactory, blockLength, key) {
this.oHash = hashFactory();
this.iHash = hashFactory();
const pad = new Uint8Array(blockLength);
if (key.length > blockLength) {
const hasher = hashFactory();
hasher.update(key);
key = hasher.finalize();
}
pad.set(key, 0);
for (let i = 0; i < pad.length; i++) {
pad[i] ^= 0x36;
}
this.iHash.update(pad);
for (let i = 0; i < pad.length; i++) {
pad[i] ^= 0x36 ^ 0x5c;
}
this.oHash.update(pad);
pad.fill(0);
}
update(data) {
this.iHash.update(data);
}
digest() {
this.oHash.update(this.iHash.finalize());
const hash = this.oHash.finalize();
this.iHash.free();
this.oHash.free();
return hash;
}
clone() {
const clone = Object.create(Object.getPrototypeOf(this), {});
clone.oHash = this.oHash.clone();
clone.iHash = this.iHash.clone();
return clone;
}
free() {
this.iHash.free();
this.oHash.free();
}
}
exports.Hmac = Hmac;
function hmacSha256(key, data) {
const hmac = new Hmac(hash_1.sha256Hasher, 64, key);
hmac.update(data);
return hmac.digest();
}
exports.hmacSha256 = hmacSha256;
function hmacSha512(key, data) {
const hmac = new Hmac(hash_1.sha512Hasher, 128, key);
hmac.update(data);
return hmac.digest();
}
exports.hmacSha512 = hmacSha512;
//# sourceMappingURL=hmac.js.map