-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.h
55 lines (44 loc) · 1.49 KB
/
auth.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
//
// Created by Arne Gockeln on 30.07.17.
// Modified by CosminPerRam on 25.04.19.
//
#ifndef AUTH_H
#define AUTH_H
#include <iostream>
#include <libcppotp/otp.h>
#include <ctime>
#include <algorithm>
#include <regex>
namespace auth
{
uint32_t generateToken(const std::string& t_secret, const int& t_interval = 30) {
const CppTotp::Bytes::ByteString key = CppTotp::Bytes::fromBase32(t_secret);
uint32_t token = CppTotp::totp(key, time(nullptr), 0, t_interval, 6);
return token;
}
std::string base32_decode(const std::string& t_base32str) {
std::string base32_string = t_base32str;
std::transform(base32_string.begin(), base32_string.end(), base32_string.begin(), ::toupper);
std::string allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
// check for invalid characters
std::regex regEx("^[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]+$");
if(!std::regex_match(base32_string, regEx)){
return "no match";
}
const int strLen = base32_string.length();
int j = 0, n = 0;
std::string binary;
for(int i = 0; i < strLen; i++){
n = n << 5;
n = n + std::distance(allowedChars.begin(), std::find(allowedChars.begin(), allowedChars.end(), base32_string[i]));
j += 5;
if(j >= 8){
j -= 8;
char c = (n & ( 0xFF << j ) ) >> j;
binary += c;
}
}
return binary;
}
}
#endif