forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.cpp
125 lines (106 loc) · 3.83 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
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
#include "Hash.h"
#include "rust/bindgen/WalletCoreRSBindgen.h"
#include "rust/Wrapper.h"
#include <string>
using namespace TW;
TW::Hash::HasherSimpleType Hash::functionPointerFromEnum(TW::Hash::Hasher hasher) {
switch (hasher) {
case Hash::HasherSha1:
return Hash::sha1;
case Hash::HasherSha256:
return Hash::sha256;
case Hash::HasherSha512:
return Hash::sha512;
case Hash::HasherSha512_256:
return Hash::sha512_256;
case Hash::HasherKeccak256:
return Hash::keccak256;
case Hash::HasherKeccak512:
return Hash::keccak512;
case Hash::HasherSha3_256:
return Hash::sha3_256;
case Hash::HasherSha3_512:
return Hash::sha3_512;
case Hash::HasherRipemd:
return Hash::ripemd;
case Hash::HasherBlake256:
return Hash::blake256;
case Hash::HasherGroestl512:
return Hash::groestl512;
case Hash::HasherSha256d:
return Hash::sha256d;
case Hash::HasherSha256ripemd:
return Hash::sha256ripemd;
case Hash::HasherSha3_256ripemd:
return Hash::sha3_256ripemd;
case Hash::HasherBlake2b:
return Hash::blake2b;
case Hash::HasherBlake256d:
return Hash::blake256d;
case Hash::HasherBlake256ripemd:
return Hash::blake256ripemd;
case Hash::HasherGroestl512d:
return Hash::groestl512d;
}
}
Data Hash::sha1(const byte* data, size_t size) {
return Rust::CByteArrayWrapper(Rust::sha1(data, size)).data;
}
Data Hash::sha256(const byte* data, size_t size) {
return Rust::CByteArrayWrapper(Rust::sha256(data, size)).data;
}
Data Hash::sha512(const byte* data, size_t size) {
return Rust::CByteArrayWrapper(Rust::sha512(data, size)).data;
}
Data Hash::sha512_256(const byte* data, size_t size) {
return Rust::CByteArrayWrapper(Rust::sha512_256(data, size)).data;
}
Data Hash::keccak256(const byte* data, size_t size) {
return Rust::CByteArrayWrapper(Rust::keccak256(data, size)).data;
}
Data Hash::keccak512(const byte* data, size_t size) {
return Rust::CByteArrayWrapper(Rust::keccak512(data, size)).data;
}
Data Hash::sha3_256(const byte* data, size_t size) {
return Rust::CByteArrayWrapper(Rust::sha3__256(data, size)).data;
}
Data Hash::sha3_512(const byte* data, size_t size) {
return Rust::CByteArrayWrapper(Rust::sha3__512(data, size)).data;
}
Data Hash::ripemd(const byte* data, size_t size) {
return Rust::CByteArrayWrapper(Rust::ripemd_160(data, size)).data;
}
Data Hash::blake256(const byte* data, size_t size) {
return Rust::CByteArrayWrapper(Rust::blake_256(data, size)).data;
}
Data Hash::blake2b(const byte* data, size_t dataSize) {
Rust::CByteArrayResultWrapper res = Rust::blake2_b(data, dataSize, 32);
if (res.isErr()) {
throw std::runtime_error("Error 'blake2_b' hashing");
}
return res.unwrap().data;
}
Data Hash::blake2b(const byte* data, size_t dataSize, size_t hashSize) {
Rust::CByteArrayResultWrapper res = Rust::blake2_b(data, dataSize, hashSize);
if (res.isErr()) {
throw std::runtime_error("Error 'blake2_b' hashing");
}
return res.unwrap().data;
}
Data Hash::blake2b(const byte* data, size_t dataSize, size_t hashSize, const Data& personal) {
Rust::CByteArrayResultWrapper res = Rust::blake2_b_personal(data, dataSize, hashSize, personal.data(), personal.size());
if (res.isErr()) {
throw std::runtime_error("Error 'blake2_b_personal' hashing");
}
return res.unwrap().data;
}
Data Hash::groestl512(const byte* data, size_t size) {
return Rust::CByteArrayWrapper(Rust::groestl_512(data, size)).data;
}
Data Hash::hmac256(const Data& key, const Data& message) {
Rust::CByteArrayWrapper res = Rust::hmac__sha256(key.data(), key.size(), message.data(), message.size());
return res.data;
}