Skip to content

Commit d4dc7c7

Browse files
authored
Merge pull request #387 from node-oauth/fix/create-hash
fix: pass proper arguments to createHash
2 parents 9f3e982 + 6f04e2e commit d4dc7c7

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
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.
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 };

test/unit/utils/crypto-util_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require('chai').should();
44
describe(cryptoUtil.createHash.name, function () {
55
it('creates a hash by given algorithm', function () {
66
const data = 'client-credentials-grant';
7-
const hash = cryptoUtil.createHash({ data, encoding: 'hex' });
7+
const hash = cryptoUtil.createHash({ data, output: 'hex' });
88
hash.should.equal('072726830f0aadd2d91f86f53e3a7ef40018c2626438152dd576e272bf2b8e60');
99
});
1010
it('should throw if data is missing', function () {

0 commit comments

Comments
 (0)