From ddd5c4961a5cb3f6d6548a66c107946625c527f4 Mon Sep 17 00:00:00 2001 From: EyePulp Date: Thu, 26 Oct 2023 16:15:59 -0500 Subject: [PATCH 1/3] add support to pass algorithm options --- index.js | 12 +++++++----- test/base.js | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 59d9e0c..2a81b53 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const defaultOptions = { algo: 'sha1', // see crypto.getHashes() for options + algoOptions: {}, encoding: 'base64', // 'base64', 'base64url', 'hex' or 'binary' files: { exclude: [], @@ -167,7 +168,7 @@ function prep(fs) { return new Promise((resolve, reject) => { try { - const hash = crypto.createHash(options.algo); + const hash = crypto.createHash(options.algo, options.algoOptions); if ( options.files.ignoreBasename || options.ignoreBasenameOnce || @@ -213,7 +214,7 @@ function prep(fs) { function symLinkIgnoreTargetContent(name, target, options, isRootElement) { delete options.skipMatching; // only used for the root level log.symlink('ignoring symbolic link target content'); - const hash = crypto.createHash(options.algo); + const hash = crypto.createHash(options.algo, options.algoOptions); if (!options.symbolicLinks.ignoreBasename && !(isRootElement && options.files.ignoreRootName)) { log.symlink('hash basename'); hash.update(name); @@ -237,7 +238,7 @@ function prep(fs) { const temp = await hashElementPromise(stats, dir, options, isRootElement); if (!options.symbolicLinks.ignoreTargetPath) { - const hash = crypto.createHash(options.algo); + const hash = crypto.createHash(options.algo, options.algoOptions); hash.update(temp.hash); log.symlink('hash targetpath'); hash.update(target); @@ -247,7 +248,7 @@ function prep(fs) { } catch (err) { if (options.symbolicLinks.ignoreTargetContentAfterError) { log.symlink(`Ignoring error "${err.code}" when hashing symbolic link ${name}`, err); - const hash = crypto.createHash(options.algo); + const hash = crypto.createHash(options.algo, options.algoOptions); if ( !options.symbolicLinks.ignoreBasename && !(isRootElement && options.files.ignoreRootName) @@ -314,6 +315,7 @@ function parseParameters(args) { if (!isObject(options_)) options_ = {}; const options = { algo: options_.algo || defaultOptions.algo, + algoOptions: options_.algoOptions || defaultOptions.algoOptions, encoding: options_.encoding || defaultOptions.encoding, files: Object.assign({}, defaultOptions.files, options_.files), folders: Object.assign({}, defaultOptions.folders, options_.folders), @@ -334,7 +336,7 @@ const HashedFolder = function HashedFolder(name, children, options, isRootElemen this.name = name; this.children = children; - const hash = crypto.createHash(options.algo); + const hash = crypto.createHash(options.algo, options.algoOptions); if ( options.folders.ignoreBasename || options.ignoreBasenameOnce || diff --git a/test/base.js b/test/base.js index ceaccb6..3792c4e 100644 --- a/test/base.js +++ b/test/base.js @@ -35,6 +35,26 @@ describe('Should generate hashes', function () { }; return hashElement(basename, dir, options).then(checkHash); }); + + it('with algoOptions passed', function () { + const checkAlgoOptionHash = result => { + should.exist(result); + should.exist(result.hash); + result.hash.should.equal('d89f885449'); + }; + + var options = { + algo: 'shake256', + algoOptions: { outputLength: 5 }, + encoding: 'hex', + excludes: [], + match: { + basename: false, + path: false, + }, + }; + return hashElement(basename, dir, options).then(checkAlgoOptionHash); + }); }); describe('when executed with an error-first callback', function () { From d094ebaffa510261d731c724e765ffc1d20510d3 Mon Sep 17 00:00:00 2001 From: Marc Walter Date: Thu, 2 Jan 2025 22:14:01 +0100 Subject: [PATCH 2/3] Add `algoOptions` to README From https://github.com/marc136/node-folder-hash/pull/217 --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index e9642bc..1a3295d 100644 --- a/README.md +++ b/README.md @@ -509,6 +509,19 @@ hashElement(__dirname, options, (error, hash) => { console.log(hash.toString()); } }); + +// pass algoOptions (example: shake256) +// see https://nodejs.org/api/crypto.html#cryptocreatehashalgorithm-options +// only supported in node v12.8 and higher +const options = { algo: 'shake256', algoOptions:{ outputLength: 5 }, files: { exclude: ['.*'], matchBasename: true } }; +hashElement(__dirname, options, (error, hash) => { + if (error) { + return console.error('hashing failed:', error); + } else { + console.log('Result for folder "' + __dirname + '":'); + console.log(hash.toString()); + } +}); ``` ## Behavior From 6ba9b9c82fa5d34a4228ce42ca5c5ad6abe57e31 Mon Sep 17 00:00:00 2001 From: Marc Walter Date: Thu, 2 Jan 2025 22:18:35 +0100 Subject: [PATCH 3/3] Fix #217 in CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca4490c..da4e2b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog -## Unreleased (2025) +## 4.1.0 (2025-01-02) +- Pass options to crypto.createHash from #217 - Update dependencies ## 4.0.4 (2023-01-11)