This repository has been archived by the owner on Oct 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
keygen.cc
84 lines (77 loc) · 2.01 KB
/
keygen.cc
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
#include "keygen.hh"
extern "C" {
extern size_t key_length();
extern int key_generate(
const char *secret,
uint64_t org_id,
uint16_t app_id,
uint32_t credits,
uint64_t expires,
uint8_t flags,
char key[key_engine::b32keylen]
);
extern int key_verify(
const char *secret,
const char key[key_engine::b32keylen],
uint64_t *org_id,
uint16_t *app_id,
uint32_t *credits,
uint64_t *expires,
uint8_t *flags
);
}
extern "C" {
size_t key_length() {
return key_engine::b32keylen;
}
int key_generate(
const char *secret,
uint64_t org_id,
uint16_t app_id,
uint32_t credits,
uint64_t expires,
uint8_t flags,
char key[key_engine::b32keylen]
)
{
key_engine eng{secret};
memset(key, 0, sizeof(key));
try {
std::string k = eng.generate(org_id, app_id, credits, expires, flags);
memcpy(key, k.data(), k.size());
return 0;
} catch (...) {
return -1;
}
return -2;
}
int key_verify(
const char *secret,
const char key[key_engine::b32keylen],
uint64_t *org_id,
uint16_t *app_id,
uint32_t *credits,
uint64_t *expires,
uint8_t *flags
)
{
apikey k;
key_engine eng{secret};
bool valid = eng.verify(key, k);
if (valid) {
if (org_id)
*org_id = k.data.org_id;
if (app_id)
*app_id = k.data.app_id;
if (credits)
*credits = k.data.credits;
if (expires) {
*expires = k.data.expires;
}
if (flags)
*flags = k.data.flags;
return 1;
}
return 0;
}
}