-
Notifications
You must be signed in to change notification settings - Fork 2
/
openssl_hash.cc
66 lines (53 loc) · 1.97 KB
/
openssl_hash.cc
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
#include "openssl_utils.hpp"
#include <openssl/pem.h>
// hash
auto hash(const std::string &plain, const EVP_MD *type) -> std::string
{
std::string hash;
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
if (ctx == nullptr) {
openssl_error();
return hash;
}
if (EVP_DigestInit_ex(ctx, type, nullptr) != 1) {
openssl_error();
EVP_MD_CTX_free(ctx);
return hash;
}
if (EVP_DigestUpdate(ctx, plain.c_str(), plain.size()) != 1) {
openssl_error();
EVP_MD_CTX_free(ctx);
return hash;
}
unsigned char md[EVP_MAX_MD_SIZE];
unsigned int md_len = 0;
if (EVP_DigestFinal_ex(ctx, md, &md_len) != 1) {
openssl_error();
EVP_MD_CTX_free(ctx);
return hash;
}
hash.resize(md_len);
memcpy(&hash[0], md, md_len);
EVP_MD_CTX_free(ctx);
return hash;
}
auto main() -> int
{
openssl_version();
std::string plain = "hello world";
std::cout << "MD5: " << toHex(hash(plain, EVP_md5())) << '\n';
std::cout << "SHA1: " << toHex(hash(plain, EVP_sha1())) << '\n';
std::cout << "SHA256: " << toHex(hash(plain, EVP_sha256())) << '\n';
std::cout << "SHA512: " << toHex(hash(plain, EVP_sha512())) << '\n';
std::cout << "SHA3-256: " << toHex(hash(plain, EVP_sha3_256())) << '\n';
std::cout << "SHA3-512: " << toHex(hash(plain, EVP_sha3_512())) << '\n';
std::cout << "BLAKE2s-256: " << toHex(hash(plain, EVP_blake2s256())) << '\n';
std::cout << "BLAKE2b-512: " << toHex(hash(plain, EVP_blake2b512())) << '\n';
std::cout << "RIPEMD160: " << toHex(hash(plain, EVP_ripemd160())) << '\n';
std::cout << "SM3: " << toHex(hash(plain, EVP_sm3())) << '\n';
std::cout << "MD4: " << toHex(hash(plain, EVP_md4())) << '\n';
std::cout << "MD5-SHA1: " << toHex(hash(plain, EVP_md5_sha1())) << '\n';
std::cout << "SHA3-224: " << toHex(hash(plain, EVP_sha3_224())) << '\n';
std::cout << "SHA3-384: " << toHex(hash(plain, EVP_sha3_384())) << '\n';
return 0;
}