1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#pragma once
8

9
#include "Data.h"
10

11
#include <functional>
12

13
namespace TW::Hash {
14

15
/// Hashing function.
16
typedef TW::Data (*HasherSimpleType)(const TW::byte*, size_t);
17
using Hasher = std::function<Data(const byte*, size_t)>;
18

19
// Digest size constants, duplicating constants from underlying lib 
20
/// Number of bytes in a SHA1 hash.
21
static const size_t sha1Size = 20;
22

23
/// Number of bytes in a SHA256 hash.
24
static const size_t sha256Size = 32;
25

26
/// Number of bytes in a SHA512 hash.
27
static const size_t sha512Size = 64;
28

29
/// Number of bytes in a RIPEMD160 hash.
30
static const size_t ripemdSize = 20;
31

32
/// Computes the SHA1 hash.
33
Data sha1(const byte* data, size_t size);
34

35
/// Computes the SHA256 hash.
36
Data sha256(const byte* data, size_t size);
37

38
/// Computes the SHA512 hash.
39
Data sha512(const byte* data, size_t size);
40

41
/// Computes the SHA512/256 hash.
42
Data sha512_256(const byte* data, size_t size);
43

44
/// Computes the Keccak SHA256 hash.
45
Data keccak256(const byte* data, size_t size);
46

47
/// Computes the Keccak SHA512 hash.
48
Data keccak512(const byte* data, size_t size);
49

50
/// Computes the version 3 SHA256 hash.
51
Data sha3_256(const byte* data, size_t size);
52

53
/// Computes the version 3 SHA512 hash.
54
Data sha3_512(const byte* data, size_t size);
55

56
/// Computes the RIPEMD160 hash.
57
Data ripemd(const byte* data, size_t size);
58

59
/// Computes the Blake256 hash.
60
Data blake256(const byte* data, size_t size);
61

62
/// Computes the Blake2b hash.
63
Data blake2b(const byte* data, size_t dataSize, size_t hashSize);
64

65
Data blake2b(const byte* data, size_t dataSize, size_t hsshSize, const Data& personal);
66

67
/// Computes the Groestl 512 hash.
68
Data groestl512(const byte* data, size_t size);
69

70
/// Computes the XXHash hash.
71
uint64_t xxhash(const byte* data, size_t size, uint64_t seed);
72

73
/// Computes the XXHash hash with 64 encoding.
74
Data xxhash64(const byte* data, size_t size, uint64_t seed);
75

76
/// Computes the XXHash hash concatenated, xxhash64 with seed 0 and 1,
77
Data xxhash64concat(const byte* data, size_t size);
78

79
/// Computes the XXHash hash.
80
uint64_t xxhash(const byte* data, const byte* end, uint64_t seed);
81

82
/// Computes the XXHash hash with 64 encoding.
83
Data xxhash64(const byte* data, const byte* end, uint64_t seed);
84

85
/// Computes the XXHash hash concatenated, xxhash64 with seed 0 and 1,
86
Data xxhash64concat(const byte* data, const byte* end);
87

88
// Templated versions for any type with data() and size()
89

90
/// Computes requested hash for data.
91
template <typename T>
92 1
Data hash(Hasher hasher, const T& data) {
93 1
    return hasher(reinterpret_cast<const byte*>(data.data()), data.size());
94
}
95

96
/// Computes the SHA1 hash.
97
template <typename T>
98 1
Data sha1(const T& data) {
99 1
    return sha1(reinterpret_cast<const byte*>(data.data()), data.size());
100
}
101

102
/// Computes the SHA256 hash.
103
template <typename T>
104 1
Data sha256(const T& data) {
105 1
    return sha256(reinterpret_cast<const byte*>(data.data()), data.size());
106
}
107

108
/// Computes the SHA512 hash.
109
template <typename T>
110 1
Data sha512(const T& data) {
111 1
    return sha512(reinterpret_cast<const byte*>(data.data()), data.size());
112
}
113

114
/// Computes the SHA512/256 hash.
115
template <typename T>
116 1
Data sha512_256(const T& data) {
117 1
    return sha512_256(reinterpret_cast<const byte*>(data.data()), data.size());
118
}
119

120
/// Computes the Keccak SHA256 hash.
121
template <typename T>
122 1
Data keccak256(const T& data) {
123 1
    return keccak256(reinterpret_cast<const byte*>(data.data()), data.size());
124
}
125

126
/// Computes the Keccak SHA512 hash.
127
template <typename T>
128
Data keccak512(const T& data) {
129
    return keccak512(reinterpret_cast<const byte*>(data.data()), data.size());
130
}
131

132
/// Computes the version 3 SHA256 hash.
133
template <typename T>
134 1
Data sha3_256(const T& data) {
135 1
    return sha3_256(reinterpret_cast<const byte*>(data.data()), data.size());
136
}
137

138
/// Computes the version 3 SHA512 hash.
139
template <typename T>
140
Data sha3_512(const T& data) {
141
    return sha3_512(reinterpret_cast<const byte*>(data.data()), data.size());
142
}
143

144
/// Computes the RIPEMD160 hash.
145
template <typename T>
146 1
Data ripemd(const T& data) {
147 1
    return ripemd(reinterpret_cast<const byte*>(data.data()), data.size());
148
}
149

150
/// Computes the Blake256 hash.
151
template <typename T>
152 1
Data blake256(const T& data) {
153 1
    return blake256(reinterpret_cast<const byte*>(data.data()), data.size());
154
}
155

156
/// Computes the Blake2b hash.
157
template <typename T>
158 1
Data blake2b(const T& data, size_t size) {
159 1
    return blake2b(reinterpret_cast<const byte*>(data.data()), data.size(), size);
160
}
161

162
template <typename T>
163 1
Data blake2b(const T& data, size_t size, const Data& personal) {
164 1
    return blake2b(reinterpret_cast<const byte*>(data.data()), data.size(), size, personal);
165
}
166

167
/// Computes the Groestl512 hash.
168
template <typename T>
169 1
Data groestl512(const T& data) {
170 1
    return groestl512(reinterpret_cast<const byte*>(data.data()), data.size());
171
}
172

173
/// Computes the SHA256 hash of the SHA256 hash.
174 1
inline Data sha256d(const byte* data, size_t size) {
175 1
    return sha256(sha256(data, size));
176
}
177

178
/// Computes the ripemd hash of the SHA256 hash.
179 1
inline Data sha256ripemd(const byte* data, size_t size) {
180 1
    return ripemd(sha256(data, size));
181
}
182

183
/// Computes the ripemd hash of the SHA256 hash.
184 1
inline Data sha3_256ripemd(const byte* data, size_t size) {
185 1
    return ripemd(sha3_256(data, size));
186
}
187

188
/// Computes the Blake256 hash of the Blake256 hash.
189 1
inline Data blake256d(const byte* data, size_t size) {
190 1
    return blake256(blake256(data, size));
191
}
192

193
/// Computes the ripemd hash of the Blake256 hash.
194 1
inline Data blake256ripemd(const byte* data, size_t size) {
195 1
    return ripemd(blake256(data, size));
196
}
197

198
/// Computes the Groestl512 hash of the Groestl512 hash.
199 1
inline Data groestl512d(const byte* data, size_t size) {
200 1
    return groestl512(groestl512(data, size));
201
}
202

203
/// Compute the SHA256-based HMAC of a message
204
Data hmac256(const Data& key, const Data& message);
205

206
} // namespace TW::Hash

Read our documentation on viewing source code .

Loading