-
Notifications
You must be signed in to change notification settings - Fork 14
/
sha256.h
45 lines (32 loc) · 834 Bytes
/
sha256.h
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
#ifndef SHA256_HASH
#define SHA256_HASH
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
#include <openssl/sha.h>
string sha256( const string &str ){
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init( &sha256 );
SHA256_Update( &sha256, str.c_str(), str.size() );
SHA256_Final( hash, &sha256 );
stringstream ss;
for( int i = 0; i < SHA256_DIGEST_LENGTH; i++ ){
ss << hex << setw(2) << setfill('0') << (int)hash[i];
}
return ss.str();
}
/*
sudo apt-get install libssl-dev
g++ -lcrypto main.cc
int main() {
cout << sha256("1234567890_1") << endl;
cout << sha256("1234567890_2") << endl;
cout << sha256("1234567890_3") << endl;
cout << sha256("1234567890_4") << endl;
return 0;
}
*/
#endif