Skip to content

Commit a230423

Browse files
committed
Issue 374: Allow an option to choose the hashing algorithm
- hashAlgorithm key added to DEFAULT_OPTIONS - hashAlgorithm key can be overriden by value given in options argument - buildOptions function will ensure a default a hashAlgorithm exists, and throw an error if the provided option is not supported by the system. - throws early error during app setup, rather than waiting until an upload is attempted - memHandler and tempFileHandler use the hashAlgorithm option now - Updated tests for buildOptions function in utilities.spec.js ref: #374
1 parent 3325e62 commit a230423

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const DEFAULT_OPTIONS = {
2020
createParentPath: false,
2121
parseNested: false,
2222
useTempFiles: false,
23-
tempFileDir: path.join(process.cwd(), 'tmp')
23+
tempFileDir: path.join(process.cwd(), 'tmp'),
24+
hashAlgorithm: 'md5'
2425
};
2526

2627
/**

lib/memHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const { debugLog } = require('./utilities');
1010
*/
1111
module.exports = (options, fieldname, filename) => {
1212
const buffers = [];
13-
const hash = crypto.createHash('md5');
13+
const hash = crypto.createHash(options.hashAlgorithm);
1414
let fileSize = 0;
1515
let completed = false;
1616

lib/tempFileHandler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ module.exports = (options, fieldname, filename) => {
1414
checkAndMakeDir({ createParentPath: true }, tempFilePath);
1515

1616
debugLog(options, `Temporary file path is ${tempFilePath}`);
17-
18-
const hash = crypto.createHash('md5');
17+
18+
const hash = crypto.createHash(options.hashAlgorithm);
1919
let fileSize = 0;
2020
let completed = false;
2121

lib/utilities.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fs = require('fs');
44
const path = require('path');
5+
const crypto = require('crypto');
56
const { Readable } = require('stream');
67

78
// Parameters for safe file name parsing.
@@ -75,6 +76,20 @@ const buildOptions = function() {
7576
if (!options || typeof options !== 'object') return;
7677
Object.keys(options).forEach(i => result[i] = options[i]);
7778
});
79+
80+
// Ensure a hashAlgorithm option exists.
81+
if (!result.hashAlgorithm) {
82+
result.hashAlgorithm = 'md5';
83+
}
84+
85+
// Ensure the configured hashAlgorithm is available on the system
86+
if (crypto.getHashes().find(h => result.hashAlgorithm === h) === undefined) {
87+
throw Error(
88+
`Hashing algorithm '${result.hashAlgorithm}' is not supported by this system's OpenSSL ` +
89+
`version`
90+
);
91+
}
92+
7893
return result;
7994
};
8095

test/utilities.spec.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,25 +199,32 @@ describe('utilities: Test of the utilities functions', function() {
199199
describe('Test buildOptions function', () => {
200200

201201
const source = { option1: '1', option2: '2' };
202-
const sourceAddon = { option3: '3'};
203-
const expected = { option1: '1', option2: '2' };
204-
const expectedAddon = { option1: '1', option2: '2', option3: '3'};
202+
const sourceAddon = { option3: '3', hashAlgorithm: 'sha256'};
203+
const expected = { option1: '1', option2: '2', hashAlgorithm: 'md5' };
204+
const expectedAddon = { option1: '1', option2: '2', option3: '3', hashAlgorithm: 'sha256'};
205205

206-
it('buildOptions returns and equal object to the object which was paased', () => {
207-
let result = buildOptions(source);
208-
assert.deepStrictEqual(result, source);
209-
});
206+
it(
207+
'buildOptions returns an equal object to the object which was passed + hashAlgorithm '
208+
+ 'property',
209+
() => {
210+
let result = buildOptions(source);
211+
assert.deepStrictEqual(result, expected);
212+
}
213+
);
210214

211215
it('buildOptions doesnt add non object or null arguments to the result', () => {
212216
let result = buildOptions(source, 2, '3', null);
213217
assert.deepStrictEqual(result, expected);
214218
});
215219

216-
it('buildOptions adds value to the result from the several source argumets', () => {
220+
it('buildOptions adds value to the result from the several source arguments', () => {
217221
let result = buildOptions(source, sourceAddon);
218222
assert.deepStrictEqual(result, expectedAddon);
219223
});
220224

225+
it('buildOptions throws an error when given an unsupported hashAlgorithm', () => {
226+
assert.throws(() => buildOptions({ hashAlgorithm: 'not-actual-algo' }));
227+
});
221228
});
222229
//buildFields tests
223230
describe('Test buildFields function', () => {

0 commit comments

Comments
 (0)