blob: 8fa80bbcf57bd54c75ed067fe87eaffd2f9f9ff5 (
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
|
#include "src/buildtool/crypto/hash_impl_sha256.hpp"
#include <array>
#include <cstdint>
#include "openssl/sha.h"
/// \brief Hash implementation for SHA-256
class HashImplSha256 final : public Hasher::IHashImpl {
public:
HashImplSha256() { initialized_ = SHA256_Init(&ctx_) == 1; }
auto Update(std::string const& data) noexcept -> bool final {
return initialized_ and
SHA256_Update(&ctx_, data.data(), data.size()) == 1;
}
auto Finalize() && noexcept -> std::optional<std::string> final {
if (initialized_) {
auto out = std::array<std::uint8_t, SHA256_DIGEST_LENGTH>{};
if (SHA256_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 {};
}
private:
SHA256_CTX ctx_{};
bool initialized_{};
};
/// \brief Factory for SHA-256 implementation
auto CreateHashImplSha256() -> std::unique_ptr<Hasher::IHashImpl> {
return std::make_unique<HashImplSha256>();
}
|