-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCredential.cpp
61 lines (48 loc) · 1.27 KB
/
Credential.cpp
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
#include "Credential.h"
#include "Base32.h"
#include <TOTP.h>
#include <cstring>
#include <string>
Credential::Credential() {}
Credential::Credential(String name, String secret, bool isOTP){
this->_name = name;
this->_secret = secret;
this->_isOTP = isOTP;
}
String Credential::getSecret() {
if(this->_isOTP) {
return this->getOTP();
}
else {
return this->_secret;
}
}
String Credential::getOTP() {
NTP ntp;
unsigned long timestamp = ntp.getEpoch();
long asciilength = this->_secret.length()+1;
byte* buffer = (byte*) malloc(asciilength);
this->_secret.getBytes(buffer, asciilength);
asciilength -= 1;
long base32length = ceil((double)asciilength/5.0)*8;
byte* base32buffer = (byte*) malloc(base32length);
Base32().fromBase32(buffer, asciilength, base32buffer);
long noPaddingBase32Length = floor((double)asciilength*5.0/8);
TOTP totp = TOTP(base32buffer, noPaddingBase32Length);
return String(totp.getCode(timestamp));
}
String Credential::getName() {
return this->_name;
}
void Credential::setName(String name) {
this->_name = name;
}
void Credential::setSecret(String secret) {
this->_secret = secret;
}
bool Credential::isOTP() {
return this->_isOTP;
}
void Credential::isOTP(bool value) {
this->_isOTP = value;
}