-
Notifications
You must be signed in to change notification settings - Fork 1
/
cal_hash.cpp
58 lines (50 loc) · 1.38 KB
/
cal_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
#include <string.h>
#include<iostream>
#include <sstream>
#include "bitcoin_sha256.h"
using namespace std;
string get_sha_256(const unsigned char *data, size_t len)
{
unsigned char buf[CSHA256::OUTPUT_SIZE];
CSHA256().Reset().Write(data,len).Finalize(buf);
std::stringstream str_stream;
char buf2[3]= {0};
for (int i = 0; i < 32; ++i)
{
memset(buf2,0,sizeof(buf2));
snprintf(buf2,sizeof(buf2),"%02X",buf[i]);
str_stream<<buf2;
}
string hash_result = str_stream.str();
return hash_result;
}
void HexStrToByte(const char* source, unsigned char* dest, int sourceLen)
{
short i;
unsigned char highByte, lowByte;
for (i = 0; i < sourceLen; i += 2)
{
highByte = toupper(source[i]);
lowByte = toupper(source[i + 1]);
if (highByte > 0x39)
highByte -= 0x37;
else
highByte -= 0x30;
if (lowByte > 0x39)
lowByte -= 0x37;
else
lowByte -= 0x30;
dest[i / 2] = (highByte << 4) | lowByte;
}
return ;
}
int main()
{
string add_input = "00010966776006953D5567439E5E39F86A0D273BEE";
unsigned char temp[200]={0};
HexStrToByte(add_input.data(), temp, add_input.size());
string hash_result;
hash_result = get_sha_256(temp, add_input.size() / 2);
cout << "hash: " << hash_result << endl;
return 0;
}