Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Node.js 18 #1542

Merged
merged 2 commits into from Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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