blob: 106dc984685ec183f421a9463386fafbb62a77fc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#include <array>
#include <cstdint>
#include "openssl/md5.h"
#include "src/buildtool/crypto/hash_impl.hpp"
/// \brief Hash implementation for MD5
class HashImplMd5 final : public IHashImpl {
public:
HashImplMd5() { initialized_ = MD5_Init(&ctx_) == 1; }
auto Update(std::string const& data) noexcept -> bool final {
return initialized_ and
MD5_Update(&ctx_, data.data(), data.size()) == 1;
}
auto Finalize() && noexcept -> std::optional<std::string> final {
if (initialized_) {
auto out = std::array<std::uint8_t, MD5_DIGEST_LENGTH>{};
if (MD5_Final(out.data(), &ctx_) == 1) {
return std::string{out.begin(), out.end()};
}
}
return std::nullopt;
}
auto Compute(std::string const& data) && noexcept -> std::string final {
if (Update(data)) {
auto digest = std::move(*this).Finalize();
if (digest) {
return *digest;
}
}
FatalError();
return {};
}
[[nodiscard]] auto DigestLength() const noexcept -> std::size_t final {
return MD5_DIGEST_LENGTH;
}
private:
MD5_CTX ctx_{};
bool initialized_{};
};
/// \brief Factory for MD5 implementation
auto CreateHashImplMd5() -> std::unique_ptr<IHashImpl> {
return std::make_unique<HashImplMd5>();
}
|