-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOneTimePasswordAlgorithm.h
executable file
·346 lines (241 loc) · 8.7 KB
/
OneTimePasswordAlgorithm.h
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// OneTimePasswordAlgorithm.h
// TestOTP
//
// Created by Robson Ventura Rodrigues on 15/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#ifndef TestOTP_OneTimePasswordAlgorithm_h
#define TestOTP_OneTimePasswordAlgorithm_h
const int OAUTHLIB_BUFFSIZE_LARGE = 20;
#include <sstream>
#include "HMAC_SHA1.h"
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
using namespace std;
std::string generateOCRA(std::string ocraSuite,
std::string key,
std::string counter,
std::string question,
std::string password,
std::string sessionInformation,
std::string timeStamp) {
int code_digits = 0;
std::string crypto = "";
std::string result;
int ocraSuiteLength = ocraSuite.size();
int counterLength = 0;
int questionLength = 0;
int passwordLength = 0;
int sessionInformationLength = 0;
int timeStampLength = 0;
vector<string> ocra_suites;
boost::split(ocra_suites, ocraSuite, boost::is_any_of(":"));
std::string crypto_function = ocra_suites[1];
std::string data_input = ocra_suites[2];
boost::algorithm::to_lower(crypto_function);
boost::algorithm::to_lower(data_input);
size_t found;
found = crypto_function.find("sha1");
if (found != string::npos) {
crypto = "HmacSHA1";
}
found = crypto_function.find("sha256");
if (found != string::npos) {
crypto = "HmacSHA256";
}
found = crypto_function.find("sha512");
if (found != string::npos) {
crypto = "HmacSHA512";
}
vector<string> crypto_function_vector;
boost::split(crypto_function_vector, crypto_function, boost::is_any_of("-"));
string number = crypto_function_vector[2];
code_digits = boost::lexical_cast< int >(number);
if (data_input.c_str()[0] == 'c') {
// Fix the length of the HEX string
while (counter.length() < 16) {
counter = "0" + counter;
}
counterLength = 8;
}
// Question - always 128 bytes
found = crypto_function.find("-q");
if (data_input.c_str()[0] == 'q'
|| found != string::npos) {
while (question.length() < 256) {
question = question + "0";
}
questionLength = 128;
}
// Password - sha1
found = crypto_function.find("psha1");
if (found > 1) {
while (password.length() < 40) {
password = "0" + password;
}
passwordLength = 20;
}
// Password - sha256
found = crypto_function.find("psha256");
if (found > 1) {
while (password.length() < 64) {
password = "0" + password;
}
passwordLength = 32;
}
// Password - sha512
found = crypto_function.find("psha512");
if (found > 1) {
while (password.length() < 128) {
password = "0" + password;
}
passwordLength = 64;
}
// sessionInformation - s064
found = crypto_function.find("s064");
if (found > 1) {
while (sessionInformation.length() < 128) {
sessionInformation = "0" + sessionInformation;
}
sessionInformationLength = 64;
}
// sessionInformation - s128
found = crypto_function.find("s128");
if (found > 1) {
while (sessionInformation.length() < 256) {
sessionInformation = "0" + sessionInformation;
}
sessionInformationLength = 128;
}
// sessionInformation - s256
found = crypto_function.find("s256");
if (found > 1) {
while (sessionInformation.length() < 512) {
sessionInformation = "0" + sessionInformation;
}
sessionInformationLength = 256;
}
// sessionInformation - s512
found = crypto_function.find("s512");
if (found > 1) {
while (sessionInformation.length() < 1024) {
sessionInformation = "0" + sessionInformation;
}
sessionInformationLength = 512;
}
// TimeStamp
found = crypto_function.find("-t");
if (data_input.c_str()[0] == 't'
|| found > 1) {
while (timeStamp.length() < 16) {
timeStamp = "0" + timeStamp;
}
timeStampLength = 8;
}
// Remember to add "1" for the "00" byte delimiter
int msg_lenth = ocraSuiteLength + counterLength + questionLength + passwordLength + sessionInformationLength + timeStampLength + 1;
char msg [msg_lenth];
// Put the bytes of "ocraSuite" parameters into the message
const char* bArray = ocraSuite.c_str();
ocraSuite.copy(msg, 0, ocraSuite.length());
// Delimiter
msg[ocraSuite.length()] = 0x00;
//=======================Continuar===================================================
// Put the bytes of "Counter" to the message
// Input is HEX encoded
if (counterLength > 0) {
bArray = hexStr2Bytes(counter);
System.arraycopy(bArray, 0, msg, ocraSuiteLength + 1, bArray.length);
}
// Put the bytes of "question" to the message
// Input is text encoded
if (questionLength > 0) {
bArray = hexStr2Bytes(question);
System.arraycopy(bArray, 0, msg, ocraSuiteLength + 1 + counterLength, bArray.length);
}
// Put the bytes of "password" to the message
// Input is HEX encoded
if (passwordLength > 0) {
bArray = hexStr2Bytes(password);
System.arraycopy(bArray, 0, msg, ocraSuiteLength + 1 + counterLength + questionLength, bArray.length);
}
// Put the bytes of "sessionInformation" to the message
// Input is text encoded
if (sessionInformationLength > 0) {
bArray = hexStr2Bytes(sessionInformation);
System.arraycopy(bArray, 0, msg, ocraSuiteLength + 1 + counterLength + questionLength + passwordLength, bArray.length);
}
// Put the bytes of "time" to the message
// Input is text value of minutes
if (timeStampLength > 0) {
bArray = hexStr2Bytes(timeStamp);
System.arraycopy(bArray, 0, msg, ocraSuiteLength + 1 + counterLength + questionLength + passwordLength + sessionInformationLength, bArray.length);
}
bArray = hexStr2Bytes(key);
byte[] hash = hmac_sha1(crypto, bArray, msg);
// put selected bytes into result int
int offset = hash[hash.length - 1] & 0xf;
int binary = ((hash[offset] & 0x7f) << 24)
| ((hash[offset + 1] & 0xff) << 16)
| ((hash[offset + 2] & 0xff) << 8)
| (hash[offset + 3] & 0xff);
int otp = binary % DIGITS_POWER[codeDigits];
result = Integer.toString(otp);
while (result.length() < codeDigits) {
result = "0" + result;
}
return result;
return number;
// How many digits should we return
//codeDigits = Integer.decode(crypto_function.substring(crypto_function.lastIndexOf("-") + 1)).intValue();
}
std::string generate_otp(BYTE *secret, int secret_length,
long moving_factor,
int code_digits,
int truncation_offset) {
int digits = code_digits;
BYTE text[8];
int text_length = sizeof (text) / sizeof (char);
for (int i = text_length - 1; i >= 0; i--) {
text[i] = (moving_factor & 0xff);
moving_factor = moving_factor >> 8;
}
for (int i = 0; i < 8; i++) {
text[i] = static_cast<char> (text[i]);
std::cout << (int) text[i] << ",";
}
std::cout << std::endl << std::endl;
unsigned char unsigned_hash[OAUTHLIB_BUFFSIZE_LARGE];
CHMAC_SHA1 objHMACSHA1;
objHMACSHA1.HMAC_SHA1(text, text_length, secret, secret_length, unsigned_hash);
char hash[OAUTHLIB_BUFFSIZE_LARGE];
for (int i = 0; i < OAUTHLIB_BUFFSIZE_LARGE; i++) {
hash[i] = static_cast<char> (unsigned_hash[i]);
std::cout << (int) hash[i] << ",";
}
std::cout << std::endl;
int hash_length = sizeof (hash) / sizeof (char);
int offset = hash[hash_length - 1] & 0xf;
if ((0 <= truncation_offset) &&
(truncation_offset < (hash_length - 4))) {
offset = truncation_offset;
}
int binary =
((hash[offset] & 0x7f) << 24)
| ((hash[offset + 1] & 0xff) << 16)
| ((hash[offset + 2] & 0xff) << 8)
| (hash[offset + 3] & 0xff);
int DIGITS_POWER[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
int otp = binary % DIGITS_POWER[code_digits];
std::stringstream result_stream;
result_stream << otp;
std::string result = result_stream.str();
while (result.length() < digits) {
result = "0" + result;
}
return result;
}
#endif