-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: use node crypto
hash
when available (#116)
- Loading branch information
Showing
1 changed file
with
10 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |