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
|
RSA_PKCS1_PADDING
|
||||||
} = process.binding('constants').crypto;
|
} = process.binding('constants').crypto;
|
||||||
const {
|
const {
|
||||||
|
checkIsArrayBufferView,
|
||||||
getDefaultEncoding,
|
getDefaultEncoding,
|
||||||
toBuf
|
toBuf
|
||||||
} = require('internal/crypto/util');
|
} = require('internal/crypto/util');
|
||||||
const { isArrayBufferView } = require('internal/util/types');
|
|
||||||
const { Writable } = require('stream');
|
const { Writable } = require('stream');
|
||||||
const { inherits } = require('util');
|
const { inherits } = require('util');
|
||||||
|
|
||||||
@ -41,14 +41,7 @@ Sign.prototype._write = function _write(chunk, encoding, callback) {
|
|||||||
|
|
||||||
Sign.prototype.update = function update(data, encoding) {
|
Sign.prototype.update = function update(data, encoding) {
|
||||||
encoding = encoding || getDefaultEncoding();
|
encoding = encoding || getDefaultEncoding();
|
||||||
data = toBuf(data, encoding);
|
data = checkIsArrayBufferView('data', toBuf(data, encoding));
|
||||||
if (!isArrayBufferView(data)) {
|
|
||||||
throw new ERR_INVALID_ARG_TYPE(
|
|
||||||
'data',
|
|
||||||
['string', 'Buffer', 'TypedArray', 'DataView'],
|
|
||||||
data
|
|
||||||
);
|
|
||||||
}
|
|
||||||
this._handle.update(data);
|
this._handle.update(data);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
@ -84,14 +77,7 @@ Sign.prototype.sign = function sign(options, encoding) {
|
|||||||
|
|
||||||
var pssSaltLength = getSaltLength(options);
|
var pssSaltLength = getSaltLength(options);
|
||||||
|
|
||||||
key = toBuf(key);
|
key = checkIsArrayBufferView('key', toBuf(key));
|
||||||
if (!isArrayBufferView(key)) {
|
|
||||||
throw new ERR_INVALID_ARG_TYPE(
|
|
||||||
'key',
|
|
||||||
['string', 'Buffer', 'TypedArray', 'DataView'],
|
|
||||||
key
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
var ret = this._handle.sign(key, passphrase, rsaPadding, pssSaltLength);
|
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);
|
var pssSaltLength = getSaltLength(options);
|
||||||
|
|
||||||
key = toBuf(key);
|
key = checkIsArrayBufferView('key', toBuf(key));
|
||||||
if (!isArrayBufferView(key)) {
|
|
||||||
throw new ERR_INVALID_ARG_TYPE(
|
|
||||||
'key',
|
|
||||||
['string', 'Buffer', 'TypedArray', 'DataView'],
|
|
||||||
key
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
signature = toBuf(signature, sigEncoding);
|
signature = checkIsArrayBufferView('signature',
|
||||||
if (!isArrayBufferView(signature)) {
|
toBuf(signature, sigEncoding));
|
||||||
throw new ERR_INVALID_ARG_TYPE(
|
|
||||||
'signature',
|
|
||||||
['string', 'Buffer', 'TypedArray', 'DataView'],
|
|
||||||
signature
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this._handle.verify(key, signature, rsaPadding, pssSaltLength);
|
return this._handle.verify(key, signature, rsaPadding, pssSaltLength);
|
||||||
};
|
};
|
||||||
|
@ -83,7 +83,19 @@ function timingSafeEqual(buf1, buf2) {
|
|||||||
return _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 = {
|
module.exports = {
|
||||||
|
checkIsArrayBufferView,
|
||||||
getCiphers,
|
getCiphers,
|
||||||
getCurves,
|
getCurves,
|
||||||
getDefaultEncoding,
|
getDefaultEncoding,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user