-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathotp.c
64 lines (51 loc) · 1.92 KB
/
otp.c
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
#include "otp.h"
#include "output.h"
void OTPProcess(HashratCtx *Ctx)
{
char *Hash=NULL, *Tempstr=NULL;
int validity, period, digits;
digits=atoi(GetVar(Ctx->Vars, "OTP:Digits"));
period=atoi(GetVar(Ctx->Vars, "OTP:Period"));
validity=period - (time(NULL) % period);
Hash=TOTP(Hash, GetVar(Ctx->Vars, "HashType"), GetVar(Ctx->Vars, "EncryptionKey"), ENCODE_BASE32, digits, period);
Tempstr=FormatStr(Tempstr, "valid for: %d seconds", validity);
OutputHash(Ctx, Hash, Tempstr);
Destroy(Tempstr);
Destroy(Hash);
}
void OTPGoogle(HashratCtx *Ctx, const char *Secret)
{
Ctx->Action = ACT_OTP;
SetVar(Ctx->Vars, "HashType", "sha1");
SetVar(Ctx->Vars, "EncryptionKey", Secret);
SetVar(Ctx->Vars, "OTP:Digits", "6");
SetVar(Ctx->Vars, "OTP:Period", "30");
}
//otpauth://totp/ACME%20Co:[email protected]?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30
void OTPParse(HashratCtx *Ctx, const char *Input)
{
char *Proto=NULL, *Host=NULL, *Path=NULL, *Args=NULL;
char *Name=NULL, *Value=NULL;
const char *ptr;
Ctx->Action=ACT_OTP;
if (strncmp(Input, "otpauth:", 8)==0)
{
ParseURL(Input, &Proto, &Host, NULL, NULL, NULL, &Path, &Args);
ptr=GetNameValuePair(Args, "&", "=", &Name, &Value);
while (ptr)
{
if (strcasecmp(Name, "algorithm")==0) SetVar(Ctx->Vars, "HashType", Value);
if (strcasecmp(Name, "secret")==0) SetVar(Ctx->Vars, "EncryptionKey", Value);
if (strcasecmp(Name, "digits")==0) SetVar(Ctx->Vars, "OTP:Digits", Value);
if (strcasecmp(Name, "period")==0) SetVar(Ctx->Vars, "OTP:Period", Value);
ptr=GetNameValuePair(ptr, "&", "=", &Name, &Value);
}
}
else SetVar(Ctx->Vars, "EncryptionKey", Input);
Destroy(Proto);
Destroy(Host);
Destroy(Path);
Destroy(Args);
Destroy(Name);
Destroy(Value);
}