crypto: make update(buf, enc) ignore encoding · nodejs/node@a727b13 · GitHub
Skip to content

Commit a727b13

Browse files
bnoordhuisMylesBorins
authored andcommitted
crypto: make update(buf, enc) ignore encoding
Make the cipher/decipher/hash/hmac update() methods ignore the input encoding when the input is a buffer. This is the documented behavior but some inputs were rejected, notably when the specified encoding is 'hex' and the buffer has an odd length (because a _string_ with an odd length is never a valid hex string.) The sign/verify update() methods work okay because they use different validation logic. Fixes: #31751 PR-URL: #31766 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 83e9a3e commit a727b13

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

lib/internal/crypto/cipher.js

Lines changed: 3 additions & 3 deletions

lib/internal/crypto/hash.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,13 @@ Hash.prototype.update = function update(data, encoding) {
7878
if (state[kFinalized])
7979
throw new ERR_CRYPTO_HASH_FINALIZED();
8080

81-
if (typeof data !== 'string' && !isArrayBufferView(data)) {
82-
throw new ERR_INVALID_ARG_TYPE('data',
83-
['string',
84-
'Buffer',
85-
'TypedArray',
86-
'DataView'],
87-
data);
81+
if (typeof data === 'string') {
82+
validateEncoding(data, encoding);
83+
} else if (!isArrayBufferView(data)) {
84+
throw new ERR_INVALID_ARG_TYPE(
85+
'data', ['string', 'Buffer', 'TypedArray', 'DataView'], data);
8886
}
8987

90-
validateEncoding(data, encoding);
91-
9288
if (!this[kHandle].update(data, encoding))
9389
throw new ERR_CRYPTO_HASH_UPDATE_FAILED();
9490
return this;
Lines changed: 22 additions & 0 deletions

0 commit comments

Comments
 (0)