Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Rename hashSize to outputLength
  • Loading branch information
tniessen committed Jul 24, 2019
commit bb96e120f73526d2a22227dd51abfdbcf0cd8df0
4 changes: 2 additions & 2 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -1788,15 +1788,15 @@ added: v0.1.92
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/28805
description: The `hashSize` option was added for XOF hash functions.
description: The `outputLength` option was added for XOF hash functions.
-->
* `algorithm` {string}
* `options` {Object} [`stream.transform` options][]
* Returns: {Hash}

Creates and returns a `Hash` object that can be used to generate hash digests
using the given `algorithm`. Optional `options` argument controls stream
behavior. For XOF hash functions such as `'shake256'`, the `hashSize` option
behavior. For XOF hash functions such as `'shake256'`, the `outputLength` option
can be used to specify the desired output length in bytes.

The `algorithm` is dependent on the available algorithms supported by the
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ function Hash(algorithm, options) {
if (!(this instanceof Hash))
return new Hash(algorithm, options);
validateString(algorithm, 'algorithm');
const xofLen = typeof options === 'object' ? options.hashSize : undefined;
const xofLen = typeof options === 'object' ? options.outputLength : undefined;
if (xofLen !== undefined)
validateUint32(xofLen, 'options.hashSize');
validateUint32(xofLen, 'options.outputLength');
this[kHandle] = new _Hash(algorithm, xofLen);
this[kState] = {
[kFinalized]: false
Expand Down
39 changes: 20 additions & 19 deletions test/parallel/test-crypto-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,31 +186,31 @@ common.expectsError(
' when called without `new`');
}

// Test XOF hash functions and the hashSize option.
// Test XOF hash functions and the outputLength option.
{
// Default hashSizes.
// Default outputLengths.
assert.strictEqual(crypto.createHash('shake128').digest('hex'),
'7f9c2ba4e88f827d616045507605853e');
assert.strictEqual(crypto.createHash('shake256').digest('hex'),
'46b9dd2b0ba88d13233b3feb743eeb24' +
'3fcd52ea62b81b82b50c27646ed5762f');

// Short hashSizes.
assert.strictEqual(crypto.createHash('shake128', { hashSize: 0 })
// Short outputLengths.
assert.strictEqual(crypto.createHash('shake128', { outputLength: 0 })
.digest('hex'),
'');
assert.strictEqual(crypto.createHash('shake128', { hashSize: 5 })
assert.strictEqual(crypto.createHash('shake128', { outputLength: 5 })
.digest('hex'),
'7f9c2ba4e8');
assert.strictEqual(crypto.createHash('shake128', { hashSize: 15 })
assert.strictEqual(crypto.createHash('shake128', { outputLength: 15 })
.digest('hex'),
'7f9c2ba4e88f827d61604550760585');
assert.strictEqual(crypto.createHash('shake256', { hashSize: 16 })
assert.strictEqual(crypto.createHash('shake256', { outputLength: 16 })
.digest('hex'),
'46b9dd2b0ba88d13233b3feb743eeb24');

// Large hashSizes.
assert.strictEqual(crypto.createHash('shake128', { hashSize: 128 })
// Large outputLengths.
assert.strictEqual(crypto.createHash('shake128', { outputLength: 128 })
.digest('hex'),
'7f9c2ba4e88f827d616045507605853e' +
'd73b8093f6efbc88eb1a6eacfa66ef26' +
Expand All @@ -220,33 +220,34 @@ common.expectsError(
'ab916a58d974918835d25e6a435085b2' +
'badfd6dfaac359a5efbb7bcc4b59d538' +
'df9a04302e10c8bc1cbf1a0b3a5120ea');
const superLongHash = crypto.createHash('shake256', { hashSize: 1024 * 1024 })
.update('The message is shorter than the hash!')
.digest('hex');
const superLongHash = crypto.createHash('shake256', {
outputLength: 1024 * 1024
}).update('The message is shorter than the hash!')
.digest('hex');
assert.strictEqual(superLongHash.length, 2 * 1024 * 1024);
assert.ok(superLongHash.endsWith('193414035ddba77bf7bba97981e656ec'));
assert.ok(superLongHash.startsWith('a2a28dbc49cfd6e5d6ceea3d03e77748'));

// Non-XOF hash functions should accept valid hashSize options as well.
assert.strictEqual(crypto.createHash('sha224', { hashSize: 28 })
// Non-XOF hash functions should accept valid outputLength options as well.
assert.strictEqual(crypto.createHash('sha224', { outputLength: 28 })
.digest('hex'),
'd14a028c2a3a2bc9476102bb288234c4' +
'15a2b01f828ea62ac5b3e42f');

// Passing invalid sizes should throw during creation.
common.expectsError(() => {
crypto.createHash('sha256', { hashSize: 28 });
crypto.createHash('sha256', { outputLength: 28 });
}, {
code: 'ERR_OSSL_EVP_NOT_XOF_OR_INVALID_LENGTH'
});

for (const hashSize of [null, {}, 'foo', false]) {
common.expectsError(() => crypto.createHash('sha256', { hashSize }),
for (const outputLength of [null, {}, 'foo', false]) {
common.expectsError(() => crypto.createHash('sha256', { outputLength }),
{ code: 'ERR_INVALID_ARG_TYPE' });
}

for (const hashSize of [-1, .5, Infinity, 2 ** 90]) {
common.expectsError(() => crypto.createHash('sha256', { hashSize }),
for (const outputLength of [-1, .5, Infinity, 2 ** 90]) {
common.expectsError(() => crypto.createHash('sha256', { outputLength }),
{ code: 'ERR_OUT_OF_RANGE' });
}
}