Skip to content

Commit

Permalink
feat: require Node 20.12+
Browse files Browse the repository at this point in the history
always use crypto.hash which is 20.12+
  • Loading branch information
kirillgroshkov committed Jul 11, 2024
1 parent 42c4db1 commit a050e73
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"url": "https://github.com/NaturalCycles/nodejs-lib"
},
"engines": {
"node": ">=18.12.0"
"node": ">=20.12.0"
},
"description": "Standard library for Node.js",
"author": "Natural Cycles Team",
Expand Down
18 changes: 4 additions & 14 deletions src/security/hash.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,14 @@ export function hash(
algorithm: string,
outputEncoding: BinaryToTextEncoding = 'hex',
): string {
// todo: cleanup after @types/node is updated
// https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V20.md#crypto-implement-cryptohash
// Node 20.12+
if ((crypto as any).hash) {
return (crypto as any).hash(algorithm, s, outputEncoding)
}

return crypto.createHash(algorithm).update(s).digest(outputEncoding)
// Note: crypto.hash is Node 20.12+
return crypto.hash(algorithm, s, outputEncoding)
}

export function hashAsBuffer(s: string | Buffer, algorithm: string): Buffer {
// todo: cleanup after @types/node is updated
// Node 20.12+
if ((crypto as any).hash) {
return (crypto as any).hash(algorithm, s, 'buffer')
}

return crypto.createHash(algorithm).update(s).digest()
// Note: crypto.hash is Node 20.12+
return crypto.hash(algorithm, s, 'buffer')
}

export function base64(s: string | Buffer): Base64String {
Expand Down

0 comments on commit a050e73

Please sign in to comment.