-
Notifications
You must be signed in to change notification settings - Fork 17
/
cotp.hpp
191 lines (148 loc) · 3.77 KB
/
cotp.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
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
#pragma once
#if defined(__cplusplus)
extern "C"
{
#include "cotp.h"
#include "otpuri.h"
}
#include <cstdint>
namespace COTP
{
class OTP
{
protected:
OTPData* data;
public:
OTP(OTPData* data, const char* base32_secret, COTP_ALGO algo, uint32_t digits)
{
this->data = otp_new(data, base32_secret, algo, digits);
}
~OTP()
{
data = nullptr;
}
COTPRESULT generate(int64_t input, char* output)
{
return otp_generate(data, input, output);
}
COTPRESULT byte_secret(char* out_str)
{
return otp_byte_secret(data, out_str);
}
COTPRESULT num_to_bytestring(uint64_t integer, char* out_str)
{
return otp_num_to_bytestring(integer, out_str);
}
size_t uri_strlen(const char* issuer, const char* name, const char* digest)
{
return otpuri_strlen(data, issuer, name, digest);
}
COTPRESULT build_uri(const char* issuer, const char* name, const char* digest, char* output)
{
return otpuri_build_uri(data, issuer, name, digest, output);
}
OTPData* data_struct()
{
return data;
}
static COTPRESULT random_base32(size_t len, char* out_str)
{
return otp_random_base32(len, out_str);
}
};
class TOTP
{
protected:
OTPData* data;
public:
TOTP(OTPData* data, const char* base32_secret, COTP_ALGO algo, COTP_TIME time, uint32_t digits, uint32_t interval)
{
this->data = totp_new(data, base32_secret, algo, time, digits, interval);
}
~TOTP()
{
data = nullptr;
}
COTPRESULT at(uint64_t for_time, uint64_t offset, char* out_str)
{
return totp_at(data, for_time, offset, out_str);
}
COTPRESULT now(char* out_str)
{
return totp_now(data, out_str);
}
COTPRESULT verify(const char* key, uint64_t for_time, int64_t valid_window)
{
return totp_verify(data, key, for_time, valid_window);
}
uint64_t valid_until(uint64_t for_time, int64_t valid_window)
{
return totp_valid_until(data, for_time, valid_window);
}
uint64_t timecode(uint64_t for_time)
{
return totp_timecode(data, for_time);
}
size_t uri_strlen(const char* issuer, const char* name, const char* digest)
{
return otpuri_strlen(data, issuer, name, digest);
}
COTPRESULT build_uri(const char* issuer, const char* name, const char* digest, char* output)
{
return otpuri_build_uri(data, issuer, name, digest, output);
}
OTPData* data_struct()
{
return data;
}
static COTPRESULT random_base32(size_t len, char* out_str)
{
return otp_random_base32(len, out_str);
}
};
class HOTP
{
protected:
OTPData* data;
public:
HOTP(OTPData* data, const char* base32_secret, COTP_ALGO algo, uint32_t digits, uint64_t count)
{
this->data = hotp_new(data, base32_secret, algo, digits, count);
}
~HOTP()
{
data = nullptr;
}
COTPRESULT at(uint64_t counter, char* out_str)
{
return hotp_at(data, counter, out_str);
}
COTPRESULT next(char* out_str)
{
return hotp_next(data, out_str);
}
COTPRESULT compare(const char* key, uint64_t counter)
{
return hotp_compare(data, key, counter);
}
size_t uri_strlen(const char* issuer, const char* name, const char* digest)
{
return otpuri_strlen(data, issuer, name, digest);
}
COTPRESULT build_uri(const char* issuer, const char* name, const char* digest, char* output)
{
return otpuri_build_uri(data, issuer, name, digest, output);
}
OTPData* data_struct()
{
return data;
}
static COTPRESULT random_base32(size_t len, char* out_str)
{
return otp_random_base32(len, out_str);
}
};
} // namespace COTP
#else
# error "cotp.hpp is a C++ header. __cplusplus not defined."
#endif