Skip to content

Commit

Permalink
Support Node.js 18 (#1542)
Browse files Browse the repository at this point in the history
Recent Node.js seems to have dropped support for ripemd160.
Thus, properly check the availability of hashes before using them.

Also, add Node.js 18 to CI.
  • Loading branch information
twiss committed Jun 29, 2022
1 parent e69d8b2 commit 04e806e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Expand Up @@ -17,7 +17,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [12.x, 14.x, 16.x, 18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
35 changes: 13 additions & 22 deletions src/crypto/hash/index.js
Expand Up @@ -20,8 +20,12 @@ import enums from '../../enums';

const webCrypto = util.getWebCrypto();
const nodeCrypto = util.getNodeCrypto();
const nodeCryptoHashes = nodeCrypto && nodeCrypto.getHashes();

function nodeHash(type) {
if (!nodeCrypto || !nodeCryptoHashes.includes(type)) {
return;
}
return async function (data) {
const shasum = nodeCrypto.createHash(type);
return stream.transform(data, value => {
Expand Down Expand Up @@ -63,28 +67,15 @@ function asmcryptoHash(hash, webCryptoHash) {
};
}

let hashFunctions;
if (nodeCrypto) { // Use Node native crypto for all hash functions
hashFunctions = {
md5: nodeHash('md5'),
sha1: nodeHash('sha1'),
sha224: nodeHash('sha224'),
sha256: nodeHash('sha256'),
sha384: nodeHash('sha384'),
sha512: nodeHash('sha512'),
ripemd: nodeHash('ripemd160')
};
} else { // Use JS fallbacks
hashFunctions = {
md5: md5,
sha1: asmcryptoHash(Sha1, 'SHA-1'),
sha224: hashjsHash(sha224),
sha256: asmcryptoHash(Sha256, 'SHA-256'),
sha384: hashjsHash(sha384, 'SHA-384'),
sha512: hashjsHash(sha512, 'SHA-512'), // asmcrypto sha512 is huge.
ripemd: hashjsHash(ripemd160)
};
}
const hashFunctions = {
md5: nodeHash('md5') || md5,
sha1: nodeHash('sha1') || asmcryptoHash(Sha1, 'SHA-1'),
sha224: nodeHash('sha224') || hashjsHash(sha224),
sha256: nodeHash('sha256') || asmcryptoHash(Sha256, 'SHA-256'),
sha384: nodeHash('sha384') || hashjsHash(sha384, 'SHA-384'),
sha512: nodeHash('sha512') || hashjsHash(sha512, 'SHA-512'), // asmcrypto sha512 is huge.
ripemd: nodeHash('ripemd160') || hashjsHash(ripemd160)
};

export default {

Expand Down

0 comments on commit 04e806e

Please sign in to comment.