mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 10:15:07 +08:00
Reject unknown Hash and Hmac digest encodings before finalizing the operation instead of silently returning a Buffer. Preserve buffer output aliases and existing encoding coercion. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: https://github.com/nodejs/node/pull/66247 Fixes: https://github.com/nodejs/node/issues/45189 Refs: https://github.com/nodejs/node/pull/45990 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
|
|
const assert = require('assert');
|
|
const crypto = require('crypto');
|
|
|
|
for (const create of [
|
|
() => crypto.createHash('sha256'),
|
|
() => crypto.createHmac('sha256', 'key'),
|
|
]) {
|
|
const expected = create().update('test').digest();
|
|
for (const encoding of ['bad', { toString: () => 'bad' }]) {
|
|
const instance = create().update('test');
|
|
assert.throws(() => instance.digest(encoding), {
|
|
code: 'ERR_UNKNOWN_ENCODING',
|
|
message: 'Unknown encoding: bad',
|
|
});
|
|
// An invalid encoding must not finalize the operation.
|
|
assert.deepStrictEqual(instance.digest(), expected);
|
|
}
|
|
for (const encoding of [undefined, null, '', false, 0, 'buffer', 'BUFFER']) {
|
|
assert.deepStrictEqual(create().update('test').digest(encoding), expected);
|
|
}
|
|
for (const encoding of ['hex', 'HEX', 'base64', 'base64url', 'latin1', 'utf-8']) {
|
|
assert.strictEqual(create().update('test').digest(encoding),
|
|
expected.toString(encoding));
|
|
}
|
|
assert.strictEqual(
|
|
create().update('test').digest({ toString: () => 'hex' }),
|
|
expected.toString('hex'));
|
|
}
|