-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.cpp
130 lines (102 loc) · 3.43 KB
/
hash.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "infra/hash.h"
#include "infra/string.h"
#ifdef INFRA_HASH_SUPPORT
#include <cryptopp/base64.h>
#include <cryptopp/cryptlib.h>
#include <cryptopp/files.h>
#include <cryptopp/hex.h>
#include <cryptopp/sha.h>
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
#endif
#include <array>
#include <cassert>
#include <sstream>
namespace inf::hash {
using namespace CryptoPP;
#ifdef INFRA_HASH_SUPPORT
std::string hex_encode(std::span<const uint8_t> data)
{
std::string hexString;
ArraySource src(data.data(), data.size(), true, new HexEncoder(new StringSink(hexString), false /*lowercase*/));
return hexString;
}
std::vector<uint8_t> hex_decode(std::string_view hexStringData)
{
std::vector<uint8_t> data;
StringSource ss(reinterpret_cast<const uint8_t*>(hexStringData.data()), hexStringData.size(), true, new HexDecoder(new VectorSink(data)));
return data;
}
std::string base64_encode(std::span<const uint8_t> data)
{
std::string hexString;
ArraySource src(data.data(), data.size(), true, new Base64Encoder(new StringSink(hexString), false /*lowercase*/));
return hexString;
}
std::vector<uint8_t> base64_decode(std::string_view hexStringData)
{
std::vector<uint8_t> data;
StringSource ss(reinterpret_cast<const uint8_t*>(hexStringData.data()), hexStringData.size(), true, new Base64Decoder(new VectorSink(data)));
return data;
}
std::array<uint8_t, 16> md5(std::span<const uint8_t> data)
{
std::array<uint8_t, 16> digest;
Weak::MD5 hash;
assert(digest.size() == hash.DigestSize());
auto sink = std::make_unique<HashFilter>(hash, new ArraySink(digest.data(), digest.size()));
ArraySource src(data.data(), data.size(), true /*pump all*/, sink.release());
return digest;
}
std::array<uint8_t, 16> md5(std::string_view stringData)
{
return md5(std::span<const uint8_t>(reinterpret_cast<const uint8_t*>(stringData.data()), stringData.size()));
}
std::array<uint8_t, 16> md5(const fs::path& filePath)
{
std::array<uint8_t, 16> digest;
Weak::MD5 hash;
auto sink = std::make_unique<HashFilter>(hash, new ArraySink(digest.data(), digest.size()));
FileSource src(str::from_u8(filePath.u8string()).c_str(), true /*pump all*/, sink.release());
return digest;
}
std::array<uint8_t, 64> sha512(std::span<const uint8_t> data)
{
std::array<uint8_t, 64> digest;
SHA512 hash;
assert(digest.size() == hash.DigestSize());
auto sink = std::make_unique<HashFilter>(hash, new ArraySink(digest.data(), digest.size()));
ArraySource src(data.data(), data.size(), true /*pump all*/, sink.release());
return digest;
}
std::array<uint8_t, 64> sha512(const fs::path& filePath)
{
std::array<uint8_t, 64> digest;
SHA512 hash;
assert(digest.size() == hash.DigestSize());
auto sink = std::make_unique<HashFilter>(hash, new ArraySink(digest.data(), digest.size()));
FileSource src(str::from_u8(filePath.u8string()).c_str(), true /*pump all*/, sink.release());
return digest;
}
#endif
std::string md5_string(const fs::path& filePath)
{
return hex_encode(md5(filePath));
}
std::string md5_string(std::string_view stringData)
{
return hex_encode(md5(stringData));
}
std::string md5_string(std::span<const uint8_t> data)
{
return hex_encode(md5(data));
}
std::string sha512_string(const fs::path& filePath)
{
return hex_encode(sha512(filePath));
}
std::string sha512_string(std::span<const uint8_t> data)
{
return hex_encode(sha512(data));
}
}