@peculiar/webcrypto
Version:
A WebCrypto Polyfill for NodeJS
40 lines (39 loc) • 1.14 kB
JavaScript
/**
* Copyright (c) 2019-2026, Peculiar Ventures
* SPDX-License-Identifier: MIT
*/
import { Buffer } from "node:buffer";
import crypto from "node:crypto";
// src/mechs/sha/crypto.ts
var ShaCrypto = class {
static size(algorithm) {
switch (algorithm.name.toUpperCase()) {
case "SHA-1": return 160;
case "SHA-256":
case "SHA3-256": return 256;
case "SHA-384":
case "SHA3-384": return 384;
case "SHA-512":
case "SHA3-512": return 512;
default: throw new Error("Unrecognized name");
}
}
static getAlgorithmName(algorithm) {
switch (algorithm.name.toUpperCase()) {
case "SHA-1": return "sha1";
case "SHA-256": return "sha256";
case "SHA-384": return "sha384";
case "SHA-512": return "sha512";
case "SHA3-256": return "sha3-256";
case "SHA3-384": return "sha3-384";
case "SHA3-512": return "sha3-512";
default: throw new Error("Unrecognized name");
}
}
static digest(algorithm, data) {
const hashAlg = this.getAlgorithmName(algorithm);
const hash = crypto.createHash(hashAlg).update(Buffer.from(data)).digest();
return new Uint8Array(hash).buffer;
}
};
export { ShaCrypto };