This repository has been archived on 2024-09-11. You can view files and clone it, but cannot push or open issues or pull requests.
svrjs-blog-newsletter/cronjob/node_modules/@smithy/hash-node/dist-es/index.js

35 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-05-26 22:54:55 +02:00
import { fromArrayBuffer, fromString } from "@smithy/util-buffer-from";
import { toUint8Array } from "@smithy/util-utf8";
import { Buffer } from "buffer";
import { createHash, createHmac } from "crypto";
export class Hash {
constructor(algorithmIdentifier, secret) {
this.algorithmIdentifier = algorithmIdentifier;
this.secret = secret;
this.reset();
}
update(toHash, encoding) {
this.hash.update(toUint8Array(castSourceData(toHash, encoding)));
}
digest() {
return Promise.resolve(this.hash.digest());
}
reset() {
this.hash = this.secret
? createHmac(this.algorithmIdentifier, castSourceData(this.secret))
: createHash(this.algorithmIdentifier);
}
}
function castSourceData(toCast, encoding) {
if (Buffer.isBuffer(toCast)) {
return toCast;
}
if (typeof toCast === "string") {
return fromString(toCast, encoding);
}
if (ArrayBuffer.isView(toCast)) {
return fromArrayBuffer(toCast.buffer, toCast.byteOffset, toCast.byteLength);
}
return fromArrayBuffer(toCast);
}