Browse Source

* Sample to demonstrate generating/showing/checking a TOTP

Michaël Van Canneyt 3 years ago
parent
commit
2391d317bc
1 changed files with 33 additions and 0 deletions
  1. 33 0
      packages/fcl-hash/examples/gentotp.pp

+ 33 - 0
packages/fcl-hash/examples/gentotp.pp

@@ -0,0 +1,33 @@
+{$mode objfpc}
+{$h+}
+uses sysutils, onetimepass;
+
+Var
+  aCount : Longint;
+
+begin
+  If ParamCount=0 then
+    Writeln('New key: ',TOTPSharedSecret)
+  else If ParamCount=1 then 
+    begin
+    if (ParamStr(1)='-h') or (ParamStr(1)='--help') then
+      begin
+      Writeln('Usage : ',ExtractFileName(Paramstr(0)),' [key [code]]');
+      Writeln('If no options are specified, generate key');
+      Writeln('If only key is specified, print current code');
+      Writeln('If both key and code are specified then check code'); 
+      end
+    else
+      Writeln('Current token : ',TOTPGenerateToken(ParamStr(1)));
+    end
+  else  
+    begin
+    if TOTPValidate(Paramstr(1),StrToIntDef(ParamStr(2),-1),1,aCount) then
+      Writeln('Code OK')
+    else
+      begin
+      Writeln('Code wrong');
+      ExitCode:=1;
+      end;
+    end;
+end.