Register singleton's destruction using std::atexit
[folly.git] / folly / ssl / OpenSSLHash.h
index baf9ec7f56681c85b71a675c474b36fe038a8b1a..0804c221858bf6442ee210a6c7d4b0ca7f995aa5 100644 (file)
@@ -18,8 +18,8 @@
 
 #include <folly/Range.h>
 #include <folly/io/IOBuf.h>
-#include <folly/io/async/ssl/OpenSSLPtrTypes.h>
 #include <folly/portability/OpenSSL.h>
+#include <folly/ssl/OpenSSLPtrTypes.h>
 
 namespace folly {
 namespace ssl {
@@ -28,11 +28,24 @@ namespace ssl {
 /// These functions are not thread-safe unless you initialize OpenSSL.
 class OpenSSLHash {
  public:
-
   class Digest {
    public:
     Digest() : ctx_(EVP_MD_CTX_new()) {}
 
+    Digest(const Digest& other) {
+      ctx_ = EvpMdCtxUniquePtr(EVP_MD_CTX_new());
+      if (other.md_ != nullptr) {
+        hash_init(other.md_);
+        check_libssl_result(
+            1, EVP_MD_CTX_copy_ex(ctx_.get(), other.ctx_.get()));
+      }
+    }
+
+    Digest& operator=(const Digest& other) {
+      this->~Digest();
+      return *new (this) Digest(other);
+    }
+
     void hash_init(const EVP_MD* md) {
       md_ = md;
       check_libssl_result(1, EVP_DigestInit_ex(ctx_.get(), md, nullptr));
@@ -54,24 +67,19 @@ class OpenSSLHash {
       check_libssl_result(size, int(len));
       md_ = nullptr;
     }
+
    private:
     const EVP_MD* md_ = nullptr;
     EvpMdCtxUniquePtr ctx_{nullptr};
   };
 
-  static void hash(
-      MutableByteRange out,
-      const EVP_MD* md,
-      ByteRange data) {
+  static void hash(MutableByteRange out, const EVP_MD* md, ByteRange data) {
     Digest hash;
     hash.hash_init(md);
     hash.hash_update(data);
     hash.hash_final(out);
   }
-  static void hash(
-      MutableByteRange out,
-      const EVP_MD* md,
-      const IOBuf& data) {
+  static void hash(MutableByteRange out, const EVP_MD* md, const IOBuf& data) {
     Digest hash;
     hash.hash_init(md);
     hash.hash_update(data);
@@ -116,16 +124,14 @@ class OpenSSLHash {
       check_libssl_result(size, int(len));
       md_ = nullptr;
     }
+
    private:
     const EVP_MD* md_ = nullptr;
     HmacCtxUniquePtr ctx_{nullptr};
   };
 
-  static void hmac(
-      MutableByteRange out,
-      const EVP_MD* md,
-      ByteRange key,
-      ByteRange data) {
+  static void
+  hmac(MutableByteRange out, const EVP_MD* md, ByteRange key, ByteRange data) {
     Hmac hmac;
     hmac.hash_init(md, key);
     hmac.hash_update(data);
@@ -141,20 +147,18 @@ class OpenSSLHash {
     hmac.hash_update(data);
     hmac.hash_final(out);
   }
-  static void hmac_sha1(
-      MutableByteRange out, ByteRange key, ByteRange data) {
+  static void hmac_sha1(MutableByteRange out, ByteRange key, ByteRange data) {
     hmac(out, EVP_sha1(), key, data);
   }
-  static void hmac_sha1(
-      MutableByteRange out, ByteRange key, const IOBuf& data) {
+  static void
+  hmac_sha1(MutableByteRange out, ByteRange key, const IOBuf& data) {
     hmac(out, EVP_sha1(), key, data);
   }
-  static void hmac_sha256(
-      MutableByteRange out, ByteRange key, ByteRange data) {
+  static void hmac_sha256(MutableByteRange out, ByteRange key, ByteRange data) {
     hmac(out, EVP_sha256(), key, data);
   }
-  static void hmac_sha256(
-      MutableByteRange out, ByteRange key, const IOBuf& data) {
+  static void
+  hmac_sha256(MutableByteRange out, ByteRange key, const IOBuf& data) {
     hmac(out, EVP_sha256(), key, data);
   }
 
@@ -177,6 +181,5 @@ class OpenSSLHash {
   }
   [[noreturn]] static void check_libssl_result_throw();
 };
-
-}
-}
+} // namespace ssl
+} // namespace folly