-
Notifications
You must be signed in to change notification settings - Fork 0
/
trying_openssl_sha256.cpp
40 lines (36 loc) · 1.17 KB
/
trying_openssl_sha256.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
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <openssl/evp.h>
bool computeHash(const std::string& unhashed, std::string& hashed) {
bool success =false;
EVP_MD_CTX* context = EVP_MD_CTX_new();
if (context != NULL) {
if (EVP_DigestInit_ex(context, EVP_sha256(),NULL)) {
if (EVP_DigestUpdate(context, unhashed.c_str(), unhashed.length())) {
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int lengthOfHash = 0;
if (EVP_DigestFinal_ex(context, hash, &lengthOfHash)) {
std::stringstream ss;
for (unsigned int i=0; i<lengthOfHash; i++) {
ss<<std::hex << std::setw(2)<<std::setfill('0')<< (int)hash[i];
}
hashed = ss.str();
success = true;
}
}
}
EVP_MD_CTX_free(context);
}
return success;
}
int main() {
std::string pw1,pw1hashed,hash;
std::cin>>pw1;
computeHash(pw1, pw1hashed);
computeHash(pw1hashed, hash);
std::cout<<pw1hashed<<std::endl;
std::cout<<hash<<std::endl;
return 0;
}