forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHash.h
More file actions
195 lines (152 loc) · 5.61 KB
/
Copy pathHash.h
File metadata and controls
195 lines (152 loc) · 5.61 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#pragma once
#include "Data.h"
#include <functional>
namespace TW::Hash {
/// Hashing function.
using Hasher = std::function<Data(const byte*, const byte*)>;
/// Number of bytes in a SHA1 hash.
static const size_t sha1Size = 20;
/// Number of bytes in a SHA256 hash.
static const size_t sha256Size = 32;
/// Number of bytes in a SHA512 hash.
static const size_t sha512Size = 64;
/// Number of bytes in a RIPEMD160 hash.
static const size_t ripemdSize = 20;
/// Computes the SHA1 hash.
Data sha1(const byte* begin, const byte* end);
/// Computes the SHA256 hash.
Data sha256(const byte* begin, const byte* end);
/// Computes the SHA512 hash.
Data sha512(const byte* begin, const byte* end);
/// Computes the SHA512/256 hash.
Data sha512_256(const byte* begin, const byte* end);
/// Computes the Keccak SHA256 hash.
Data keccak256(const byte* begin, const byte* end);
/// Computes the Keccak SHA512 hash.
Data keccak512(const byte* begin, const byte* end);
/// Computes the version 3 SHA256 hash.
Data sha3_256(const byte* begin, const byte* end);
/// Computes the version 3 SHA512 hash.
Data sha3_512(const byte* begin, const byte* end);
/// Computes the RIPEMD160 hash.
Data ripemd(const byte* begin, const byte* end);
/// Computes the Blake256 hash.
Data blake256(const byte* begin, const byte* end);
/// Computes the Blake2b hash.
Data blake2b(const byte* begin, const byte* end, size_t size);
Data blake2b(const byte* begin, const byte* end, size_t size, const Data& personal);
/// Computed the Groestl 512 hash.
Data groestl512(const byte* begin, const byte* end);
/// Computes requested hash for data.
template <typename T>
Data hash(Hasher hasher, const T& data) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return hasher(begin, begin + data.size());
}
/// Computes the SHA1 hash.
template <typename T>
Data sha1(const T& data) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return sha1(begin, begin + data.size());
}
/// Computes the SHA256 hash.
template <typename T>
Data sha256(const T& data) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return sha256(begin, begin + data.size());
}
/// Computes the SHA512 hash.
template <typename T>
Data sha512(const T& data) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return sha512(begin, begin + data.size());
}
/// Computes the SHA512/256 hash.
template <typename T>
Data sha512_256(const T& data) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return sha512_256(begin, begin + data.size());
}
/// Computes the Keccak SHA256 hash.
template <typename T>
Data keccak256(const T& data) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return keccak256(begin, begin + data.size());
}
/// Computes the Keccak SHA512 hash.
template <typename T>
Data keccak512(const T& data) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return keccak512(begin, begin + data.size());
}
/// Computes the version 3 SHA256 hash.
template <typename T>
Data sha3_256(const T& data) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return sha3_256(begin, begin + data.size());
}
/// Computes the version 3 SHA512 hash.
template <typename T>
Data sha3_512(const T& data) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return sha3_512(begin, begin + data.size());
}
/// Computes the RIPEMD160 hash.
template <typename T>
Data ripemd(const T& data) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return ripemd(begin, begin + data.size());
}
/// Computes the Blake256 hash.
template <typename T>
Data blake256(const T& data) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return blake256(begin, begin + data.size());
}
/// Computes the Blake2b hash.
template <typename T>
Data blake2b(const T& data, size_t size) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return blake2b(begin, begin + data.size(), size);
}
template <typename T>
Data blake2b(const T& data, size_t size, const Data& personal) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return blake2b(begin, begin + data.size(), size, personal);
}
/// Computes the Groestl512 hash.
template <typename T>
Data groestl512(const T& data) {
const auto begin = reinterpret_cast<const byte*>(data.data());
return groestl512(begin, begin + data.size());
}
/// Computes the SHA256 hash of the SHA256 hash.
inline Data sha256d(const byte* begin, const byte* end) {
return sha256(sha256(begin, end));
}
/// Computes the ripemd hash of the SHA256 hash.
inline Data sha256ripemd(const byte* begin, const byte* end) {
return ripemd(sha256(begin, end));
}
/// Computes the ripemd hash of the SHA256 hash.
inline Data sha3_256ripemd(const byte* begin, const byte* end) {
return ripemd(sha3_256(begin, end));
}
/// Computes the Blake256 hash of the Blake256 hash.
inline Data blake256d(const byte* begin, const byte* end) {
return blake256(blake256(begin, end));
}
/// Computes the ripemd hash of the Blake256 hash.
inline Data blake256ripemd(const byte* begin, const byte* end) {
return ripemd(blake256(begin, end));
}
/// Computes the Groestl512 hash of the Groestl512 hash.
inline Data groestl512d(const byte* begin, const byte* end) {
return groestl512(groestl512(begin, end));
}
} // namespace TW::Hash