-
Notifications
You must be signed in to change notification settings - Fork 43
/
GoogleOTP.pas
187 lines (158 loc) · 4.37 KB
/
GoogleOTP.pas
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
{###############################################################################
https://github.com/wendelb/DelphiOTP
###############################################################################}
unit GoogleOTP;
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
interface
uses
{$IFNDEF FPC}System.{$ENDIF}SysUtils, {$IFNDEF FPC}System.{$ENDIF}Math, Base32U, {$IFNDEF FPC}System.{$ENDIF}DateUtils
{$IFNDEF FPC}
, IdGlobal, IdHMACSHA1
{$ELSE}
, HMAC
{$IFEND};
(*
Test Case for the CalculateOTP function
---------------------------------------
Init key: AAAAAAAAAAAAAAAAAAAA
Timestamp: 1
BinCounter: 0000000000000001 (HEX-Representation)
Hash: eeb00b0bcc864679ff2d8dd30bec495cb5f2ee9e (HEX-Representation)
Offset: 14
Part 1: 73
Part 2: 92
Part 3: 181
Part 4: 242
One time password: 812658
Easy Display: Format('%.6d', [CalculateOTP(SECRET)]);
*)
function CalculateOTP(const Secret: String; const Counter: Integer = -1): Integer;
function ValidateTOPT(const Secret: String; const Token: Integer; const WindowSize: Integer = 4): Boolean;
function GenerateOTPSecret(len: Integer = -1): String;
implementation
Type
{$IFNDEF FPC}
OTPBytes = TIdBytes;
{$ELSE}
OTPBytes = TBytes;
{$IFEND}
const
otpLength = 6;
keyRegeneration = 30;
SecretLengthDef = 20;
{$IFDEF FPC}
function BytesToStringRaw(const InValue: TBytes): RawByteString;
begin
SetString(Result, PAnsiChar(Pointer(InValue)), length(InValue));
end;
function RawByteStringToBytes(const InValue: RawByteString): TBytes;
begin
Result := [];
SetLength(Result, Length(InValue));
Move(InValue[1], Result[0], Length(InValue));
end;
function ToBytes(const InValue: Int64): TBytes;
begin
Result := [];
SetLength(Result, SizeOf(Int64));
Move(InValue, Result[0], SizeOf(Int64));
end;
{$ENDIF}
/// <summary>
/// Sign the Buffer with the given Key
/// </summary>
function HMACSHA1(const _Key: OTPBytes; const Buffer: OTPBytes): OTPBytes;
begin
{$IFNDEF FPC}
with TIdHMACSHA1.Create do
begin
Key := _Key;
Result := HashValue(Buffer);
Free;
end;
{$ELSE}
Result := HMAC.HMACSHA1Digest(BytesToStringRaw(_Key), BytesToStringRaw(Buffer));
{$IFEND}
end;
/// <summary>
/// Reverses TIdBytes (from low->high to high->low)
/// </summary>
function ReverseIdBytes(const inBytes: OTPBytes): OTPBytes;
var
i: Integer;
begin
{$IFDEF FPC}Result := [];{$IFEND}
SetLength(Result, Length(inBytes));
for i := Low(inBytes) to High(inBytes) do
Result[High(inBytes) - i] := inBytes[i];
end;
/// <summary>
/// My own ToBytes function. Something in the original one isn't working as expected.
/// </summary>
function StrToIdBytes(const inString: String): OTPBytes;
var
ch: Char;
i: Integer;
begin
{$IFDEF FPC}Result := [];{$ENDIF}
SetLength(Result, Length(inString));
i := 0;
for ch in inString do
begin
Result[i] := Ord(ch);
inc(i);
end;
end;
function CalculateOTP(const Secret: String; const Counter: Integer = -1): Integer;
var
BinSecret: String;
Hash: String;
Offset: Integer;
Part1, Part2, Part3, Part4: Integer;
Key: Integer;
Time: Integer;
begin
if Counter <> -1 then
Time := Counter
else
Time := DateTimeToUnix(Now, False) div keyRegeneration;
BinSecret := Base32.Decode(Secret);
Hash := BytesToStringRaw(HMACSHA1(StrToIdBytes(BinSecret), ReverseIdBytes(ToBytes(Int64(Time)))));
Offset := (ord(Hash[20]) AND $0F) + 1;
Part1 := (ord(Hash[Offset+0]) AND $7F);
Part2 := (ord(Hash[Offset+1]) AND $FF);
Part3 := (ord(Hash[Offset+2]) AND $FF);
Part4 := (ord(Hash[Offset+3]) AND $FF);
Key := (Part1 shl 24) OR (Part2 shl 16) OR (Part3 shl 8) OR (Part4);
Result := Key mod Trunc(IntPower(10, otpLength));
end;
function ValidateTOPT(const Secret: String; const Token: Integer; const WindowSize: Integer = 4): Boolean;
var
TimeStamp: Integer;
TestValue: Integer;
begin
Result := false;
TimeStamp := DateTimeToUnix(Now, False) div keyRegeneration;
for TestValue := Timestamp - WindowSize to TimeStamp + WindowSize do
begin
if (CalculateOTP(Secret, TestValue) = Token) then
Result := true;
end;
end;
function GenerateOTPSecret(len: Integer = -1): String;
var
i : integer;
ValCharLen : integer;
begin
Result := '';
ValCharLen := Length(Base32U.ValidChars);
if (len < 1) then
len := SecretLengthDef;
for i := 1 to len do
begin
Result := Result + copy(Base32U.ValidChars, Random(ValCharLen) + 1, 1);
end;
end;
end.