Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ Hash.prototype.copy = function copy(options) {
};

Hash.prototype._transform = function _transform(chunk, encoding, callback) {
this[kHandle].update(chunk, encoding);
if (!this[kHandle].update(chunk, encoding))
return callback(new ERR_CRYPTO_HASH_UPDATE_FAILED());
callback();
};

Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-crypto-hash-stream-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

// Flags: --expose-internals

const common = require('../common');
if (!common.hasCrypto) common.skip('missing crypto');

const assert = require('assert');
const crypto = require('crypto');
const { kHandle } = require('internal/crypto/util');

/**
* This test verifies that the Hash stream properly surfaces an error
* when the underlying native update fails.
*/

const h = crypto.createHash('sha256');

// Simulate native update failure by replacing the internal handle
const fakeHandle = {
update: () => false,
digest: () => Buffer.from('')
};

h[kHandle] = fakeHandle;

h.on('error', common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_CRYPTO_HASH_UPDATE_FAILED');
}));

h.write('test data');
h.end();
Loading