-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcrypto.cpp
112 lines (98 loc) · 3.26 KB
/
mcrypto.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <vector>
#include <string.h>
#include <cstring>
#include "mcrypto.hpp"
#include "mutil.h"
#include "mdebug.h"
/*
* Base64Encoder
*/
Base64Encoder::Base64Encoder(void)
{}
Base64Encoder::~Base64Encoder(void)
{}
std::string Base64Encoder::encode(std::basic_string<unsigned char> &data)
{
char *encoded = base64_encode_openssl(data.data(), data.size());
if (encoded == NULL) {
throw std::runtime_error("base64_encode_openssl returned a NULL");
}
std::string response(encoded);
free(encoded);
return response;
}
std::basic_string<unsigned char> Base64Encoder::decode(std::string &b64string)
{
size_t output_size = 0;
unsigned char *decoded = base64_decode_openssl(b64string.c_str(), &output_size);
mdbgf("b64 decoded b64string, size is %d bytes\n", output_size);
mdbgf("strlen reports %d bytes\n", strlen((char *)decoded));
if (decoded == NULL) {
throw std::runtime_error("base64_encode_openssl returned a NULL");
}
std::basic_string<unsigned char> response;
response.resize( output_size );
std::memcpy(&response[0], decoded, output_size);
mdbgf("b64 decoded response is %d long\n", response.size());
free(decoded);
return response;
}
/*
* SHAEncoder
*/
SHAEncoder::SHAEncoder(SHAStrength strength)
: m_strength(strength)
{
if (m_strength != SHAStrength::sha1) {
throw std::runtime_error("only sha1 is supported at this time");
}
}
SHAEncoder::~SHAEncoder(void) {}
std::basic_string<unsigned char> SHAEncoder::quickdigest(std::basic_string<unsigned char> in) {
unsigned int cdata_length = 0;
unsigned char *cdata = digest_sha1(in.c_str(), in.size(), &cdata_length);
std::basic_string<unsigned char> data(cdata, cdata+cdata_length);
OPENSSL_free(cdata);
return data;
}
/*
* AESEncryptor
*/
AESEncryptor::AESEncryptor(std::basic_string<unsigned char> &key,
std::basic_string<unsigned char> &iv,
const EVP_CIPHER *cipher_type)
: m_key(key)
, m_iv(iv)
, m_cipher_type(cipher_type)
{}
AESEncryptor::~AESEncryptor(void)
{}
std::basic_string<unsigned char> AESEncryptor::encrypt(std::string &plaintext)
{
unsigned char *encrypted = encrypt_ssl(m_key.c_str(),
m_iv.c_str(),
m_cipher_type,
(unsigned char *)plaintext.c_str(),
plaintext.size());
if (encrypted == NULL) {
throw std::runtime_error("encrypt_aes returned a NULL");
}
std::basic_string<unsigned char> response(encrypted);
free(encrypted);
return response;
}
std::string AESEncryptor::decrypt(std::basic_string<unsigned char> &ciphertext)
{
unsigned char *unencrypted = decrypt_ssl(m_key.c_str(),
m_iv.c_str(),
m_cipher_type,
ciphertext.c_str(),
ciphertext.size());
if (unencrypted == NULL) {
throw std::runtime_error("decrypt_aes returned a NULL");
}
std::string response((char *)unencrypted);
free(unencrypted);
return response;
}