Skip to content

Commit

Permalink
perf: use node crypto hash when available (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Feb 20, 2025
1 parent e12dd18 commit fbca1ca
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/crypto/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { createHash } from "node:crypto";
import { createHash, hash } from "node:crypto";

/**
* Hashes a string using the SHA-256 algorithm and encodes it in Base64URL format.
*
* @param {string} message - The message to hash.
* @param {string} data - data message to hash.
*
* @returns {string} The hash of the message.
* @returns {string} The hash of the data.
*/
export function digest(date: string): string {
return createHash("sha256").update(date).digest("base64url");
export function digest(data: string): string {
if (hash) {
// Available in Node.js v21.7.0+, v20.12.0+
// https://nodejs.org/api/crypto.html#cryptohashalgorithm-data-outputencoding
return hash("sha256", data, "base64url");
}
return createHash("sha256").update(data).digest("base64url");
}

0 comments on commit fbca1ca

Please sign in to comment.