stop leaking FILE* objects when hashing
[libi2ncommon] / src / crypto.cpp
index c7ad98b..2aeb614 100644 (file)
@@ -91,7 +91,11 @@ string I2n::hash_data_raw(string data, algorithm algo)
 
     const EVP_MD *md;
 
+#   if (OPENSSL_VERSION_NUMBER < 0x10100000L)
     EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
+#   else
+    EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_new(), EVP_MD_CTX_free);
+#   endif
 
     uchar_arr ret(new unsigned char[EVP_MAX_MD_SIZE]);
 
@@ -113,7 +117,11 @@ string I2n::hash_file(string filename, algorithm algo)
 {
     const EVP_MD *md;
 
+#   if (OPENSSL_VERSION_NUMBER < 0x10100000L)
     EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
+#   else
+    EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_new(), EVP_MD_CTX_free);
+#   endif
 
     uchar_arr ret(new unsigned char[EVP_MAX_MD_SIZE]);
 
@@ -160,9 +168,10 @@ string I2n::hash_file(string filename, algorithm algo)
         }
     }
 
+    fclose(file);
+
     if (!EVP_DigestFinal_ex(ctx.get(), ret.get(), &olen))
     {
-        fclose(file);
         throw runtime_error(ERROR_MESSAGE);
     }