Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const defaultOptions = {
algo: 'sha1', // see crypto.getHashes() for options
algoOptions: {},
encoding: 'base64', // 'base64', 'base64url', 'hex' or 'binary'
files: {
exclude: [],
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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),
Expand All @@ -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 ||
Expand Down
20 changes: 20 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
Loading