@naturalcycles/nodejs-lib 13.12.0 → 13.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/security/hash.util.js +11 -0
- package/package.json +1 -1
- package/src/security/hash.util.ts +13 -0
|
@@ -20,10 +20,21 @@ function sha256AsBuffer(s) {
|
|
|
20
20
|
}
|
|
21
21
|
exports.sha256AsBuffer = sha256AsBuffer;
|
|
22
22
|
function hash(s, algorithm, outputEncoding = 'hex') {
|
|
23
|
+
// todo: cleanup after @types/node is updated
|
|
24
|
+
// https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V20.md#crypto-implement-cryptohash
|
|
25
|
+
// Node 20.12+
|
|
26
|
+
if (node_crypto_1.default.hash) {
|
|
27
|
+
return node_crypto_1.default.hash(algorithm, s, outputEncoding);
|
|
28
|
+
}
|
|
23
29
|
return node_crypto_1.default.createHash(algorithm).update(s).digest(outputEncoding);
|
|
24
30
|
}
|
|
25
31
|
exports.hash = hash;
|
|
26
32
|
function hashAsBuffer(s, algorithm) {
|
|
33
|
+
// todo: cleanup after @types/node is updated
|
|
34
|
+
// Node 20.12+
|
|
35
|
+
if (node_crypto_1.default.hash) {
|
|
36
|
+
return node_crypto_1.default.hash(algorithm, s, 'buffer');
|
|
37
|
+
}
|
|
27
38
|
return node_crypto_1.default.createHash(algorithm).update(s).digest();
|
|
28
39
|
}
|
|
29
40
|
exports.hashAsBuffer = hashAsBuffer;
|
package/package.json
CHANGED
|
@@ -22,10 +22,23 @@ export function hash(
|
|
|
22
22
|
algorithm: string,
|
|
23
23
|
outputEncoding: BinaryToTextEncoding = 'hex',
|
|
24
24
|
): string {
|
|
25
|
+
// todo: cleanup after @types/node is updated
|
|
26
|
+
// https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V20.md#crypto-implement-cryptohash
|
|
27
|
+
// Node 20.12+
|
|
28
|
+
if ((crypto as any).hash) {
|
|
29
|
+
return (crypto as any).hash(algorithm, s, outputEncoding)
|
|
30
|
+
}
|
|
31
|
+
|
|
25
32
|
return crypto.createHash(algorithm).update(s).digest(outputEncoding)
|
|
26
33
|
}
|
|
27
34
|
|
|
28
35
|
export function hashAsBuffer(s: string | Buffer, algorithm: string): Buffer {
|
|
36
|
+
// todo: cleanup after @types/node is updated
|
|
37
|
+
// Node 20.12+
|
|
38
|
+
if ((crypto as any).hash) {
|
|
39
|
+
return (crypto as any).hash(algorithm, s, 'buffer')
|
|
40
|
+
}
|
|
41
|
+
|
|
29
42
|
return crypto.createHash(algorithm).update(s).digest()
|
|
30
43
|
}
|
|
31
44
|
|