crypto: add checkIsArrayBufferView
This commit adds a checkIsArrayBufferView function to avoid some code duplication in sig.js PR-URL: https://github.com/nodejs/node/pull/20251 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
This commit is contained in:
parent
d4726d2f3f
commit
fe70af9072
@ -14,10 +14,10 @@ const {
|
||||
RSA_PKCS1_PADDING
|
||||
} = process.binding('constants').crypto;
|
||||
const {
|
||||
checkIsArrayBufferView,
|
||||
getDefaultEncoding,
|
||||
toBuf
|
||||
} = require('internal/crypto/util');
|
||||
const { isArrayBufferView } = require('internal/util/types');
|
||||
const { Writable } = require('stream');
|
||||
const { inherits } = require('util');
|
||||
|
||||
@ -41,14 +41,7 @@ Sign.prototype._write = function _write(chunk, encoding, callback) {
|
||||
|
||||
Sign.prototype.update = function update(data, encoding) {
|
||||
encoding = encoding || getDefaultEncoding();
|
||||
data = toBuf(data, encoding);
|
||||
if (!isArrayBufferView(data)) {
|
||||
throw new ERR_INVALID_ARG_TYPE(
|
||||
'data',
|
||||
['string', 'Buffer', 'TypedArray', 'DataView'],
|
||||
data
|
||||
);
|
||||
}
|
||||
data = checkIsArrayBufferView('data', toBuf(data, encoding));
|
||||
this._handle.update(data);
|
||||
return this;
|
||||
};
|
||||
@ -84,14 +77,7 @@ Sign.prototype.sign = function sign(options, encoding) {
|
||||
|
||||
var pssSaltLength = getSaltLength(options);
|
||||
|
||||
key = toBuf(key);
|
||||
if (!isArrayBufferView(key)) {
|
||||
throw new ERR_INVALID_ARG_TYPE(
|
||||
'key',
|
||||
['string', 'Buffer', 'TypedArray', 'DataView'],
|
||||
key
|
||||
);
|
||||
}
|
||||
key = checkIsArrayBufferView('key', toBuf(key));
|
||||
|
||||
var ret = this._handle.sign(key, passphrase, rsaPadding, pssSaltLength);
|
||||
|
||||
@ -128,23 +114,10 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {
|
||||
|
||||
var pssSaltLength = getSaltLength(options);
|
||||
|
||||
key = toBuf(key);
|
||||
if (!isArrayBufferView(key)) {
|
||||
throw new ERR_INVALID_ARG_TYPE(
|
||||
'key',
|
||||
['string', 'Buffer', 'TypedArray', 'DataView'],
|
||||
key
|
||||
);
|
||||
}
|
||||
key = checkIsArrayBufferView('key', toBuf(key));
|
||||
|
||||
signature = toBuf(signature, sigEncoding);
|
||||
if (!isArrayBufferView(signature)) {
|
||||
throw new ERR_INVALID_ARG_TYPE(
|
||||
'signature',
|
||||
['string', 'Buffer', 'TypedArray', 'DataView'],
|
||||
signature
|
||||
);
|
||||
}
|
||||
signature = checkIsArrayBufferView('signature',
|
||||
toBuf(signature, sigEncoding));
|
||||
|
||||
return this._handle.verify(key, signature, rsaPadding, pssSaltLength);
|
||||
};
|
||||
|
@ -83,7 +83,19 @@ function timingSafeEqual(buf1, buf2) {
|
||||
return _timingSafeEqual(buf1, buf2);
|
||||
}
|
||||
|
||||
function checkIsArrayBufferView(name, buffer) {
|
||||
if (!isArrayBufferView(buffer)) {
|
||||
throw new ERR_INVALID_ARG_TYPE(
|
||||
name,
|
||||
['string', 'Buffer', 'TypedArray', 'DataView'],
|
||||
buffer
|
||||
);
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
checkIsArrayBufferView,
|
||||
getCiphers,
|
||||
getCurves,
|
||||
getDefaultEncoding,
|
||||
|
Loading…
x
Reference in New Issue
Block a user