forked from Bragegs/openssl-example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto.hpp
80 lines (66 loc) · 2.94 KB
/
crypto.hpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
#include <sstream>
#include <iomanip>
#include <string>
// Note: C-style casts, for instance (int), are used to simplify the source code.
// C++ casts, such as static_cast and reinterpret_cast, should otherwise
// be used in modern C++.
/// Limited C++ bindings for the OpenSSL Crypto functions.
class Crypto {
public:
/// Return hex string from bytes in input string.
static std::string hex(const std::string &input) {
std::stringstream hex_stream;
hex_stream << std::hex << std::internal << std::setfill('0');
for (auto &byte : input)
hex_stream << std::setw(2) << (int)(unsigned char)byte;
return hex_stream.str();
}
/// Return the MD5 (128-bit) hash from input.
static std::string md5(const std::string &input, size_t iterations = 1) {
std::string hash;
hash.resize(128 / 8);
MD5((const unsigned char *)input.c_str(), input.size(), (unsigned char *) hash.c_str());
return hash;
}
/// Return the SHA-1 (160-bit) hash from input.
static std::string sha1(const std::string &input, size_t iterations = 1) {
std::string hash;
hash.resize(160 / 8);
SHA1((const unsigned char *)input.c_str(), input.size(), (unsigned char *)hash.c_str());
for (size_t c = 1; c < iterations; ++c)
SHA1((const unsigned char *)hash.c_str(), hash.size(), (unsigned char *)hash.c_str());
return hash;
}
/// Return the SHA-256 (256-bit) hash from input.
static std::string sha256(const std::string &input, size_t iterations = 1) {
std::string hash;
hash.resize(256 / 8);
SHA256((const unsigned char *)input.c_str(), input.size(), (unsigned char *)hash.c_str());
for (size_t c = 1; c < iterations; ++c)
SHA256((const unsigned char *)hash.c_str(), hash.size(), (unsigned char *)hash.c_str());
return hash;
}
/// Return the SHA-512 (512-bit) hash from input.
static std::string sha512(const std::string &input, size_t iterations = 1) {
std::string hash;
hash.resize(256 / 8);
SHA256((const unsigned char *)input.c_str(), input.size(), (unsigned char *)hash.c_str());
for (size_t c = 1; c < iterations; ++c)
SHA256((const unsigned char *)hash.c_str(), hash.size(), (unsigned char *)hash.c_str());
return hash;
}
/// Return key from the Password-Based Key Derivation Function 2 (PBKDF2).
static std::string pbkdf2(const std::string &password, const std::string &salt, int iterations = 4096, int key_length = 256 / 8) {
std::string key;
key.resize(key_length);
auto success = PKCS5_PBKDF2_HMAC_SHA1(password.c_str(), password.size(),
(const unsigned char *)salt.c_str(), salt.size(), iterations,
key_length, (unsigned char *)key.c_str());
if (!success)
throw std::runtime_error("openssl: error calling PBKCS5_PBKDF2_HMAC_SHA1");
return key;
}
};