From 04e806e0b4a6872652e48d7df2d443e9e0d81597 Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Wed, 29 Jun 2022 20:59:38 +0200 Subject: [PATCH] Support Node.js 18 (#1542) 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. --- .github/workflows/node.js.yml | 2 +- src/crypto/hash/index.js | 35 +++++++++++++---------------------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 07ee301d5..11a050294 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -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: diff --git a/src/crypto/hash/index.js b/src/crypto/hash/index.js index 27f42c6b5..0765881ed 100644 --- a/src/crypto/hash/index.js +++ b/src/crypto/hash/index.js @@ -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 => { @@ -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 {