@@ -205,6 +205,9 @@ static int X509_up_ref(X509* cert) {
205205 CRYPTO_add (&cert->references , 1 , CRYPTO_LOCK_X509);
206206 return 1 ;
207207}
208+
209+ #define EVP_MD_CTX_new EVP_MD_CTX_create
210+ #define EVP_MD_CTX_free EVP_MD_CTX_destroy
208211#endif // OPENSSL_VERSION_NUMBER < 0x10100000L
209212
210213// Subject DER of CNNIC ROOT CA and CNNIC EV ROOT CA are taken from
@@ -3955,6 +3958,11 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
39553958}
39563959
39573960
3961+ Hash::~Hash () {
3962+ EVP_MD_CTX_free (mdctx_);
3963+ }
3964+
3965+
39583966void Hash::Initialize (Environment* env, v8::Local<v8::Object> target) {
39593967 Local<FunctionTemplate> t = env->NewFunctionTemplate (New);
39603968
@@ -3989,20 +3997,22 @@ bool Hash::HashInit(const char* hash_type) {
39893997 const EVP_MD* md = EVP_get_digestbyname (hash_type);
39903998 if (md == nullptr )
39913999 return false ;
3992- EVP_MD_CTX_init (&mdctx_);
3993- if (EVP_DigestInit_ex (&mdctx_, md, nullptr ) <= 0 ) {
4000+ mdctx_ = EVP_MD_CTX_new ();
4001+ if (mdctx_ == nullptr ||
4002+ EVP_DigestInit_ex (mdctx_, md, nullptr ) <= 0 ) {
4003+ EVP_MD_CTX_free (mdctx_);
4004+ mdctx_ = nullptr ;
39944005 return false ;
39954006 }
3996- initialised_ = true ;
39974007 finalized_ = false ;
39984008 return true ;
39994009}
40004010
40014011
40024012bool Hash::HashUpdate (const char * data, int len) {
4003- if (!initialised_ )
4013+ if (mdctx_ == nullptr )
40044014 return false ;
4005- EVP_DigestUpdate (& mdctx_, data, len);
4015+ EVP_DigestUpdate (mdctx_, data, len);
40064016 return true ;
40074017}
40084018
@@ -4067,8 +4077,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
40674077 unsigned char md_value[EVP_MAX_MD_SIZE];
40684078 unsigned int md_len;
40694079
4070- EVP_DigestFinal_ex (&hash->mdctx_ , md_value, &md_len);
4071- EVP_MD_CTX_cleanup (&hash->mdctx_ );
4080+ EVP_DigestFinal_ex (hash->mdctx_ , md_value, &md_len);
40724081 hash->finalized_ = true ;
40734082
40744083 Local<Value> error;
0 commit comments