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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
#include "commonheaders.h"
const string padding = "PADDING!PADDING!PADDING!PADDING!"; // 256 bit
string pad256(string& str)
{
string out = str;
int pad = out.length() % 32;
if (pad) { // ¤® ¢ë஢ïâì 256 ¡¨â
if (pad > 3) {
out += tlv(0, padding.substr(0, 32 - 3 - pad));
}
else {
if (pad == 3) out += string("\0\0\0");
else
if (pad == 2) out += string("\0\0");
else out += string("\0");
}
}
return out;
}
string& add_delim(string& str, const string& del, int len)
{
string val;
for (u_int i = 0; i < str.length(); i += len) {
val += str.substr(i, len) + del;
}
str = val;
return str;
}
string& del_delim(string& str, const string& del)
{
string val;
for (u_int i = 0; i < str.length(); i++) {
if (del.find(str[i]) == string::npos)
val += str[i];
}
str = val;
return str;
}
string tlv(int t, const string& v)
{
string b;
t |= v.length() << 8;
b.assign((const char*)&t, 3);
b += v;
return b;
}
string tlv(int t, const char* v)
{
return tlv(t, string(v));
}
string tlv(int t, int v)
{
string s;
u_int l = (v <= 0xFF) ? 1 : ((v <= 0xFFFF) ? 2 : ((v <= 0xFFFFFF) ? 3 : 4));
s.assign((char*)&v, l);
return tlv(t, s);
}
string& un_tlv(string& b, int& t, string& v)
{
string r; v = r;
if (b.length() > 3) {
t = 0;
b.copy((char*)&t, 3);
u_int l = t >> 8;
t &= 0xFF;
if (b.length() >= 3 + l) {
v = b.substr(3, l);
r = b.substr(3 + l);
}
}
if (!v.length()) {
t = -1;
}
b = r;
return b;
}
string& un_tlv(string& b, int& t, int& v)
{
string s;
un_tlv(b, t, s);
v = 0;
s.copy((char*)&v, s.length());
return b;
}
int str2int(string& s)
{
int v = 0;
if (s.length() <= sizeof(int))
s.copy((char*)&v, s.length());
return v;
}
string hash(string& b)
{
return ::hash((PBYTE)b.data(), b.length());
}
string hash(PBYTE b, size_t l)
{
BYTE h[RSA_KEYSIZE];
RSA_CalculateDigest(h, b, l);
string s; s.assign((char*)&h, RSA_KEYSIZE);
return s;
}
string hash128(string& b)
{
return hash128((PBYTE)b.data(), b.length());
}
string hash128(LPSTR b)
{
return hash128((PBYTE)b, strlen(b));
}
string hash128(PBYTE b, size_t l)
{
BYTE h[RIPEMD128::DIGESTSIZE];
RIPEMD128().CalculateDigest(h, b, l);
string s; s.assign((char*)&h, sizeof(h));
return s;
}
string hash256(string& b)
{
return hash256((PBYTE)b.data(), b.length());
}
string hash256(LPSTR b)
{
return hash256((PBYTE)b, strlen(b));
}
string hash256(PBYTE b, size_t l)
{
BYTE h[RIPEMD256::DIGESTSIZE];
RIPEMD256().CalculateDigest(h, b, l);
string s; s.assign((char*)&h, sizeof(h));
return s;
}
Integer BinaryToInteger(const string& data)
{
StringSource ss(data, true, NULL);
SecByteBlock result(ss.MaxRetrievable());
ss.Get(result, result.size());
return Integer(result, result.size());
}
string IntegerToBinary(const Integer& value)
{
SecByteBlock sbb(value.MinEncodedSize());
value.Encode(sbb, sbb.size());
string data;
StringSource(sbb, sbb.size(), true, new StringSink(data));
return data;
}
AutoSeededRandomPool& GlobalRNG()
{
static AutoSeededRandomPool randomPool;
return randomPool;
}
void GenerateRSAKey(unsigned int keyLength, string& privKey, string& pubKey)
{
RSAES_PKCS1v15_Decryptor priv(GlobalRNG(), keyLength);
StringSink privFile(privKey);
priv.DEREncode(privFile);
privFile.MessageEnd();
RSAES_PKCS1v15_Encryptor pub(priv);
StringSink pubFile(pubKey);
pub.DEREncode(pubFile);
pubFile.MessageEnd();
}
string RSAEncryptString(const RSA::PublicKey& pubkey, const string& plaintext)
{
RSAES_PKCS1v15_Encryptor pub(pubkey);
string result;
StringSource(plaintext, true, new PK_EncryptorFilter(GlobalRNG(), pub, new StringSink(result)));
return result;
}
string RSADecryptString(const RSA::PrivateKey& privkey, const string& ciphertext)
{
RSAES_PKCS1v15_Decryptor priv(privkey);
string result;
try {
StringSource(ciphertext, true, new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result)));
}
catch (...) {
;
}
return result;
}
string RSASignString(const RSA::PrivateKey& privkey, const string& plaintext)
{
RSASSA_PKCS1v15_SHA_Signer priv(privkey);
string result;
try {
StringSource(plaintext, true, new SignerFilter(GlobalRNG(), priv, new StringSink(result)));
}
catch (...) {
;
}
return result;
}
BOOL RSAVerifyString(const RSA::PublicKey& pubkey, const string& plaintext, const string& sig)
{
RSASSA_PKCS1v15_SHA_Verifier ver(pubkey);
return ver.VerifyMessage((PBYTE)plaintext.data(), plaintext.length(), (PBYTE)sig.data(), sig.length());
}
|