Skip to content

Commit 8e92776

Browse files
authored
fix: pass proper arguments to createHash
Refactor createHash function to include output parameter and update documentation.
1 parent 9f3e982 commit 8e92776

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

lib/utils/crypto-util.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@
33
const crypto = require('crypto');
44

55
/**
6-
* Export `StringUtil`.
6+
* @module CryptoUtil
77
*/
88

9-
module.exports = {
10-
/**
11-
*
12-
* @param algorithm {String} the hash algorithm, default is 'sha256'
13-
* @param data {Buffer|String|TypedArray|DataView} the data to hash
14-
* @param encoding {String|undefined} optional, the encoding to calculate the
15-
* digest
16-
* @return {Buffer|String} if {encoding} undefined a {Buffer} is returned, otherwise a {String}
17-
*/
18-
createHash: function({ algorithm = 'sha256', data = undefined, encoding = undefined }) {
19-
return crypto
20-
.createHash(algorithm)
21-
.update(data)
22-
.digest(encoding);
23-
}
9+
/**
10+
* Creates a new hash by given algorithm, data and digest encoding.
11+
* Defaults to sha256 as.
12+
*
13+
* @function
14+
* @param algorithm {string} the hash algorithm, default is 'sha256'
15+
* @param data {Buffer|string|TypedArray|DataView} the data to hash
16+
* @param encoding {string=} optional, the encoding of the input
17+
* @param output {'base64'|'base64url'|'binary'|'hex'|undefined} optional, the desired output type
18+
* @return {Buffer|string} if {output} is undefined, a {Buffer} is returned, otherwise a {String}
19+
*/
20+
const createHash = function({ algorithm = 'sha256', data, output, encoding }) {
21+
return crypto
22+
.createHash(algorithm)
23+
.update(data, encoding)
24+
.digest(output);
2425
};
26+
27+
module.exports = { createHash };

0 commit comments

Comments
 (0)