/
encoding.cpp
118 lines (92 loc) · 3.54 KB
/
encoding.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
113
114
115
116
117
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/md5.h>
#include "encoding.h"
namespace encoding {
ustring sstring2ustring(sstring const& s) {
return ustring((unsigned char*)s.c_str(), s.length());
}
ustring generate_random_sequence(int n) {
ustring seq(n, 0);
for (int i = 0; i < n; ++i) {
seq[i] = (unsigned char) rand();
}
return seq;
}
ustring md5_hash(ustring const& passphrase, ustring const& indata) {
ustring str = indata + passphrase;
MD5_CTX md5handler;
unsigned char md5digest[MD5_DIGEST_LENGTH];
MD5(str.c_str(), str.length(), md5digest);
return ustring(md5digest, MD5_DIGEST_LENGTH);
}
ustring md5_hash(sstring const& passphrase, ustring const& indata) {
return md5_hash(sstring2ustring(passphrase), indata);
}
ustring aes_encode(ustring const& passphrase, ustring const& indata, int length) {
AES_KEY key;
AES_set_encrypt_key(passphrase.c_str(), 8 * length, &key);
unsigned char* outdata = new unsigned char[length];
AES_encrypt(indata.c_str(), outdata, &key);
ustring res(outdata, length);
delete[] outdata;
return res;
}
ustring aes_encode(sstring const& passphrase, ustring const& indata, int length) {
return aes_encode(sstring2ustring(passphrase), indata, length);
}
ustring aes_decode(ustring const& passphrase, ustring const& indata, int length) {
AES_KEY key;
AES_set_decrypt_key(passphrase.c_str(), 8 * length, &key);
unsigned char* outdata = new unsigned char[length];
AES_decrypt(indata.c_str(), outdata, &key);
ustring res(outdata, length);
delete[] outdata;
return res;
}
ustring aes_decode(sstring const& passphrase, ustring const& indata, int length) {
return aes_decode(sstring2ustring(passphrase), indata, length);
}
bool check_passphrase(ustring const& correct_hash, ustring const& passphrase, ustring const& indata) {
return correct_hash == md5_hash(passphrase, indata);
}
bool check_passphrase(ustring const& correct_hash, sstring const& passphrase, ustring const& indata) {
return check_passphrase(correct_hash, sstring2ustring(passphrase), indata);
}
sstring base64_encode(ustring const& input) {
BIO* b64 = BIO_new(BIO_f_base64());
BIO* bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, input.c_str(), input.length());
BIO_flush(b64);
BUF_MEM* bptr;
BIO_get_mem_ptr(b64, &bptr);
sstring buffer(bptr->data, bptr->length - 1);
BIO_free_all(b64);
return buffer;
}
static int base64_decode_length(sstring const& input) {
int padding = 0;
if (input[input.length() - 1] == '=') {
++padding;
}
if (input[input.length() - 2] == '=') {
++padding;
}
return (input.length() * 6 - 8 * padding) / 8;
}
ustring base64_decode(sstring const& input) {
BIO* b64 = BIO_new(BIO_f_base64());
sstring str = input + "\n";
BIO* bmem = BIO_new_mem_buf((void*) str.c_str(), str.length());
bmem = BIO_push(b64, bmem);
unsigned char *buffer = new unsigned char[str.length()];
BIO_read(bmem, buffer, str.length());
BIO_free_all(bmem);
ustring res(buffer, base64_decode_length(input));
delete[] buffer;
return res;
}
}