Skip to content

Commit 444fec1

Browse files
committed
fixup! crypto: implement crypto.digest()
1 parent 1131ee0 commit 444fec1

File tree

3 files changed

+15
-31
lines changed

3 files changed

+15
-31
lines changed

benchmark/crypto/node-digest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function main({ length, type, method, n }) {
2727
}
2828

2929
const hash = digest ?
30-
(method, input) => digest(method, input, { outputEncoding: 'hex' }) :
30+
(method, input) => digest(method, input, 'hex') :
3131
(method, input) => createHash(method).update(input).digest('hex');
3232
const array = [];
3333
for (let i = 0; i < n; i++) {

lib/internal/crypto/hash.js

+6-12
Original file line numberDiff line numberDiff line change
@@ -192,24 +192,18 @@ async function asyncDigest(algorithm, data) {
192192
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
193193
}
194194

195-
function digest(algorithm, input, options) {
195+
function digest(algorithm, input, outputEncoding = 'hex') {
196196
validateString(algorithm, 'algorithm');
197197
if (typeof input !== 'string') {
198198
validateBuffer(input, 'input');
199199
}
200-
let inputEncoding = 'utf8';
201-
let outputEncoding = 'buffer';
202-
if (typeof options === 'object') {
203-
if (typeof options.inputEncoding === 'string') {
204-
inputEncoding = normalizeEncoding(options.inputEncoding) || inputEncoding;
205-
}
206-
if (typeof options.outputEncoding === 'string') {
207-
outputEncoding = normalizeEncoding(options.outputEncoding) || outputEncoding;
208-
}
200+
// Fast case: if it's 'hex', we don't need to validate it further.
201+
if (outputEncoding !== 'hex') {
202+
validateString(outputEncoding);
203+
outputEncoding = normalizeEncoding(outputEncoding) || outputEncoding;
209204
}
210205
return oneShotDigest(algorithm, getCachedHashId(algorithm), getHashCache(),
211-
input, inputEncoding, encodingsMap[inputEncoding],
212-
outputEncoding, encodingsMap[outputEncoding]);
206+
input, outputEncoding, encodingsMap[outputEncoding]);
213207
}
214208

215209
module.exports = {

src/crypto/crypto_hash.cc

+8-18
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,17 @@ const EVP_MD* GetDigestImplementation(Environment* env,
204204
}
205205

206206
// crypto.digest(algorithm, algorithmId, algorithmCache,
207-
// input, inputEncoding, inputEncodingId,
208-
// outputEncoding, outputEncodingId)
207+
// input, outputEncoding, outputEncodingId)
209208
void Hash::OneShotDigest(const FunctionCallbackInfo<Value>& args) {
210209
Environment* env = Environment::GetCurrent(args);
211210
Isolate* isolate = env->isolate();
212-
CHECK_EQ(args.Length(), 8);
211+
CHECK_EQ(args.Length(), 6);
213212
CHECK(args[0]->IsString()); // algorithm
214213
CHECK(args[1]->IsInt32()); // algorithmId
215214
CHECK(args[2]->IsObject()); // algorithmCache
216215
CHECK(args[3]->IsString() || args[3]->IsArrayBufferView()); // input
217-
CHECK(args[4]->IsString()); // inputEncoding
218-
CHECK(args[5]->IsUint32() || args[5]->IsUndefined()); // inputEncodingId
219-
CHECK(args[6]->IsString()); // outputEncoding
220-
CHECK(args[7]->IsUint32() || args[7]->IsUndefined()); // outputEncodingId
216+
CHECK(args[4]->IsString()); // outputEncoding
217+
CHECK(args[5]->IsUint32() || args[5]->IsUndefined()); // outputEncodingId
221218

222219
const EVP_MD* md = GetDigestImplementation(env, args[0], args[1], args[2]);
223220
if (md == nullptr) {
@@ -227,8 +224,7 @@ void Hash::OneShotDigest(const FunctionCallbackInfo<Value>& args) {
227224
return ThrowCryptoError(env, ERR_get_error(), message.c_str());
228225
}
229226

230-
enum encoding input_enc = ParseEncoding(isolate, args[4], args[5], UTF8);
231-
enum encoding output_enc = ParseEncoding(isolate, args[6], args[7], BUFFER);
227+
enum encoding output_enc = ParseEncoding(isolate, args[4], args[5], HEX);
232228

233229
int md_len = EVP_MD_size(md);
234230
unsigned int result_size;
@@ -241,15 +237,9 @@ void Hash::OneShotDigest(const FunctionCallbackInfo<Value>& args) {
241237
// in the future.
242238
// https://github.com/openssl/openssl/issues/19612
243239
if (args[3]->IsString()) {
244-
StringBytes::InlineDecoder decoder;
245-
// Input is a string so it must be decodable.
246-
decoder.Decode(env, args[3].As<String>(), input_enc).Check();
247-
if (UNLIKELY(decoder.size() > INT_MAX)) {
248-
return THROW_ERR_OUT_OF_RANGE(env, "data is too long");
249-
}
250-
251-
success = EVP_Digest(decoder.out(),
252-
decoder.size(),
240+
Utf8Value utf8(isolate, args[3]);
241+
success = EVP_Digest(utf8.out(),
242+
utf8.length(),
253243
output.data<unsigned char>(),
254244
&result_size,
255245
md,

0 commit comments

Comments
 (0)