Commit 68a3d541 authored by Jude Taylor's avatar Jude Taylor Committed by Facebook Github Bot

Provide a copy ctor for Digest that copies the current hash context

Summary: opensource changes for t16702532

Reviewed By: yfeldblum

Differential Revision: D4846899

fbshipit-source-id: 67a610ff399e95c7cf1c9c8a5950f79bfc3aabb9
parent a9d90ea1
......@@ -33,6 +33,20 @@ class OpenSSLHash {
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,6 +68,7 @@ class OpenSSLHash {
check_libssl_result(size, int(len));
md_ = nullptr;
}
private:
const EVP_MD* md_ = nullptr;
EvpMdCtxUniquePtr ctx_{nullptr};
......
......@@ -45,6 +45,39 @@ TEST_F(OpenSSLHashTest, sha256) {
EXPECT_EQ(expected, out);
}
TEST_F(OpenSSLHashTest, sha256_hashcopy) {
std::array<uint8_t, 32> expected, actual;
OpenSSLHash::Digest digest;
digest.hash_init(EVP_sha256());
digest.hash_update(ByteRange(StringPiece("foobar")));
OpenSSLHash::Digest copy(digest);
digest.hash_final(range(expected));
copy.hash_final(range(actual));
EXPECT_EQ(expected, actual);
}
TEST_F(OpenSSLHashTest, sha256_hashcopy_intermediate) {
std::array<uint8_t, 32> expected, actual;
OpenSSLHash::Digest digest;
digest.hash_init(EVP_sha256());
digest.hash_update(ByteRange(StringPiece("foo")));
OpenSSLHash::Digest copy(digest);
digest.hash_update(ByteRange(StringPiece("bar")));
copy.hash_update(ByteRange(StringPiece("bar")));
digest.hash_final(range(expected));
copy.hash_final(range(actual));
EXPECT_EQ(expected, actual);
}
TEST_F(OpenSSLHashTest, hmac_sha256) {
auto key = ByteRange(StringPiece("qwerty"));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment