Skip to content

Commit

Permalink
code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosHe committed Jun 26, 2022
1 parent e861b4f commit cc72a1a
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/cores/OTP.Core.Calculator.pas
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,12 @@ implementation

function TOTPCalculator.Calculate: UInt32;
var
LCounter: Byte;
LHash: TArray<Byte>;
LTimeKey: TArray<Byte>;
LBinSecret: TArray<Byte>;
LParts: array [0 .. 3] of UInt8;
LOffset: UInt8;
LKey: UInt32;
LBinCode: UInt32;
LTime: Int64;
const
LPartDic: array [0 .. 3] of Byte = ($7F, $FF, $FF, $FF);
begin
LTime := DateTimeToUnix(Now(), False) div FKeyRegeneration;

Expand All @@ -63,12 +59,10 @@ function TOTPCalculator.Calculate: UInt32;
LHash := THashSHA1.GetHMACAsBytes(LTimeKey, LBinSecret);

LOffset := (LHash[19] AND $0F);
for LCounter := Low(LParts) to High(LParts) do
LParts[LCounter] := LHash[LOffset + LCounter] and LPartDic[LCounter];

LKey := (LParts[0] shl 24) or (LParts[1] shl 16) or (LParts[2] shl 8) or (LParts[3]);
LBinCode := ((LHash[LOffset] and $7F) shl 24) or ((LHash[LOffset + 1] and $FF) shl 16) or ((LHash[LOffset + 2] and $FF) shl 8) or (LHash[LOffset + 3] and $FF);

Result := LKey mod Trunc(IntPower(10, FLength));
Result := LBinCode mod Trunc(IntPower(10, FLength));
end;

constructor TOTPCalculator.Create;
Expand Down

0 comments on commit cc72a1a

Please sign in to comment.