fltrdr
/
src
/
ob
/
crypto.cc
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
#include "ob/crypto.hh"
#include <openssl/sha.h>
#include <array>
#include <string>
#include <string_view>
#include <sstream>
#include <iomanip>
#include <optional>
namespace OB::Crypto
{
std::optional<std::string> sha256(std::string_view const str)
{
SHA256_CTX ctx;
if (! SHA256_Init(&ctx)) return {};
if (! SHA256_Update(&ctx, str.data(), str.size())) return {};
std::array<unsigned char, SHA256_DIGEST_LENGTH> digest;
if (! SHA256_Final(digest.data(), &ctx)) return {};
std::ostringstream res;
res << std::hex << std::setfill('0');
for (auto const& e : digest)
{
res << std::setw(2) << static_cast<int>(e);
}
return res.str();
}
} // namespace OB::Crypto