| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 17efd93 commit 990feaf
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4634,16 +4634,20 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) { | |||
| 4634 | 4634 | encoding = ParseEncoding(env->isolate(), args[0], BUFFER); | |
| 4635 | 4635 | } | |
| 4636 | 4636 | ||
| 4637 | - unsigned char md_value[EVP_MAX_MD_SIZE]; | ||
| 4638 | - unsigned int md_len; | ||
| 4639 | - | ||
| 4640 | - EVP_DigestFinal_ex(hash->mdctx_.get(), md_value, &md_len); | ||
| 4637 | + if (hash->md_len_ == 0) { | ||
| 4638 | + // Some hash algorithms such as SHA3 do not support calling | ||
| 4639 | + // EVP_DigestFinal_ex more than once, however, Hash._flush | ||
| 4640 | + // and Hash.digest can both be used to retrieve the digest, | ||
| 4641 | + // so we need to cache it. | ||
| 4642 | + // See https://github.com/nodejs/node/issues/28245. | ||
| 4643 | + EVP_DigestFinal_ex(hash->mdctx_.get(), hash->md_value_, &hash->md_len_); | ||
| 4644 | + } | ||
| 4641 | 4645 | ||
| 4642 | 4646 | Local<Value> error; | |
| 4643 | 4647 | MaybeLocal<Value> rc = | |
| 4644 | 4648 | StringBytes::Encode(env->isolate(), | |
| 4645 | - reinterpret_cast<const char*>(md_value), | ||
| 4646 | - md_len, | ||
| 4649 | + reinterpret_cast<const char*>(hash->md_value_), | ||
| 4650 | + hash->md_len_, | ||
| 4647 | 4651 | encoding, | |
| 4648 | 4652 | &error); | |
| 4649 | 4653 | if (rc.IsEmpty()) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -595,12 +595,19 @@ class Hash : public BaseObject { | |||
| 595 | 595 | ||
| 596 | 596 | Hash(Environment* env, v8::Local<v8::Object> wrap) | |
| 597 | 597 | : BaseObject(env, wrap), | |
| 598 | - mdctx_(nullptr) { | ||
| 598 | + mdctx_(nullptr), | ||
| 599 | + md_len_(0) { | ||
| 599 | 600 | MakeWeak(); | |
| 600 | 601 | } | |
| 601 | 602 | ||
| 603 | + ~Hash() override { | ||
| 604 | + OPENSSL_cleanse(md_value_, md_len_); | ||
| 605 | + } | ||
| 606 | + | ||
| 602 | 607 | private: | |
| 603 | 608 | EVPMDPointer mdctx_; | |
| 609 | + unsigned char md_value_[EVP_MAX_MD_SIZE]; | ||
| 610 | + unsigned int md_len_; | ||
| 604 | 611 | }; | |
| 605 | 612 | ||
| 606 | 613 | class SignBase : public BaseObject { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,11 +30,17 @@ const crypto = require('crypto'); | |||
| 30 | 30 | ||
| 31 | 31 | const stream = require('stream'); | |
| 32 | 32 | const s = new stream.PassThrough(); | |
| 33 | - const h = crypto.createHash('sha1'); | ||
| 34 | - const expect = '15987e60950cf22655b9323bc1e281f9c4aff47e'; | ||
| 33 | + const h = crypto.createHash('sha3-512'); | ||
| 34 | + const expect = '36a38a2a35e698974d4e5791a3f05b05' + | ||
| 35 | + '198235381e864f91a0e8cd6a26b677ec' + | ||
| 36 | + 'dcde8e2b069bd7355fabd68abd6fc801' + | ||
| 37 | + '19659f25e92f8efc961ee3a7c815c758'; | ||
| 35 | 38 | ||
| 36 | 39 | s.pipe(h).on('data', common.mustCall(function(c) { | |
| 37 | 40 | assert.strictEqual(c, expect); | |
| 41 | + // Calling digest() after piping into a stream with SHA3 should not cause | ||
| 42 | + // a segmentation fault, see https://github.com/nodejs/node/issues/28245. | ||
| 43 | + assert.strictEqual(h.digest('hex'), expect); | ||
| 38 | 44 | })).setEncoding('hex'); | |
| 39 | 45 | ||
| 40 | 46 | s.end('aoeu'); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments