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
#include "Hash.h"
8
#include "XXHash64.h"
9
#include "BinaryCoding.h"
10

11
#include <TrezorCrypto/blake256.h>
12
#include <TrezorCrypto/blake2b.h>
13
#include <TrezorCrypto/groestl.h>
14
#include <TrezorCrypto/ripemd160.h>
15
#include <TrezorCrypto/sha2.h>
16
#include <TrezorCrypto/sha3.h>
17
#include <TrezorCrypto/hmac.h>
18

19
#include <string>
20

21
using namespace TW;
22

23 1
Data Hash::sha1(const byte* data, size_t size) {
24 1
    Data result(sha1Size);
25 1
    sha1_Raw(data, size, result.data());
26 1
    return result;
27
}
28

29 1
Data Hash::sha256(const byte* data, size_t size) {
30 1
    Data result(sha256Size);
31 1
    sha256_Raw(data, size, result.data());
32 1
    return result;
33
}
34

35 1
Data Hash::sha512(const byte* data, size_t size) {
36 1
    Data result(sha512Size);
37 1
    sha512_Raw(data, size, result.data());
38 1
    return result;
39
}
40

41 1
Data Hash::sha512_256(const byte* data, size_t size) {
42 1
    Data result(sha256Size);
43 1
    sha512_256_Raw(data, size, result.data());
44 1
    return result;
45
}
46

47 1
Data Hash::keccak256(const byte* data, size_t size) {
48 1
    Data result(sha256Size);
49 1
    keccak_256(data, size, result.data());
50 1
    return result;
51
}
52

53 1
Data Hash::keccak512(const byte* data, size_t size) {
54 1
    Data result(sha512Size);
55 1
    keccak_512(data, size, result.data());
56 1
    return result;
57
}
58

59 1
Data Hash::sha3_256(const byte* data, size_t size) {
60 1
    Data result(sha256Size);
61 1
    ::sha3_256(data, size, result.data());
62 1
    return result;
63
}
64

65 1
Data Hash::sha3_512(const byte* data, size_t size) {
66 1
    Data result(sha512Size);
67 1
    ::sha3_512(data, size, result.data());
68 1
    return result;
69
}
70

71 1
Data Hash::ripemd(const byte* data, size_t size) {
72 1
    Data result(ripemdSize);
73 1
    ::ripemd160(data, static_cast<uint32_t>(size), result.data());
74 1
    return result;
75
}
76

77 1
Data Hash::blake256(const byte* data, size_t size) {
78 1
    Data result(sha256Size);
79 1
    ::blake256(data, size, result.data());
80 1
    return result;
81
}
82

83 1
Data Hash::blake2b(const byte* data, size_t dataSize, size_t hashSize) {
84 1
    Data result(hashSize);
85 1
    ::blake2b(data, static_cast<uint32_t>(dataSize), result.data(), hashSize);
86 1
    return result;
87
}
88

89 1
Data Hash::blake2b(const byte* data, size_t dataSize, size_t hashSize, const Data& personal) {
90 1
    Data result(hashSize);
91 1
    ::blake2b_Personal(data, static_cast<uint32_t>(dataSize), personal.data(), personal.size(), result.data(), hashSize);
92 1
    return result;
93
}
94

95 1
Data Hash::groestl512(const byte* data, size_t size) {
96
    GROESTL512_CTX ctx;
97 1
    Data result(sha512Size);
98 1
    groestl512_Init(&ctx);
99 1
    groestl512_Update(&ctx, data, size);
100 1
    groestl512_Final(&ctx, result.data());
101 1
    return result;
102
}
103

104 0
uint64_t Hash::xxhash(const byte* data, size_t size, uint64_t seed)
105
{
106 0
    return XXHash64::hash(data, size, seed);
107
}
108

109 1
Data Hash::xxhash64(const byte* data, size_t size, uint64_t seed)
110
{
111 1
    const auto hash = XXHash64::hash(data, size, seed);
112 1
    Data result;
113 1
    encode64LE(hash, result);
114 1
    return result; 
115
}
116

117 1
Data Hash::xxhash64concat(const byte* data, size_t size)
118
{
119 1
    auto key1 = xxhash64(data, size, 0);
120 1
    const auto key2 = xxhash64(data, size, 1);
121 1
    TW::append(key1, key2);
122 1
    return key1;
123
}
124

125 1
Data Hash::hmac256(const Data& key, const Data& message) {
126 1
    Data hmac(SHA256_DIGEST_LENGTH);
127 1
    hmac_sha256(key.data(), static_cast<uint32_t>(key.size()), message.data(), static_cast<uint32_t>(message.size()), hmac.data());
128 1
    return hmac;
129
}

Read our documentation on viewing source code .

Loading