-
Notifications
You must be signed in to change notification settings - Fork 474
Expand file tree
/
Copy pathhash.cpp
More file actions
35 lines (28 loc) · 770 Bytes
/
hash.cpp
File metadata and controls
35 lines (28 loc) · 770 Bytes
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
#include "core/hash.h"
#include "absl/strings/escaping.h"
#include "openssl/bio.h"
#include "openssl/digest.h"
namespace jsonnet {
namespace hash {
namespace {
std::string digest(const std::string& input, const EVP_MD* alg)
{
uint8_t digest[EVP_MAX_MD_SIZE];
unsigned int digest_length = 0;
if (EVP_Digest(input.data(), input.size(), digest, &digest_length, alg, nullptr) != 1) {
return "";
}
return absl::BytesToHexString(
std::string(reinterpret_cast<const char*>(digest), digest_length));
}
} // namespace
std::string Sha256(const std::string& input)
{
return digest(input, EVP_sha256());
}
std::string Md5(const std::string& input)
{
return digest(input, EVP_md5());
}
} // namespace hash
} // namespace jsonnet