]> gitweb.factorcode.org Git - factor.git/commitdiff
totp[-docs]: accept TOTP keys in Base 32 encoding
authorAlexander Iljin <ajsoft@yandex.ru>
Wed, 29 Jul 2020 16:48:18 +0000 (18:48 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 29 Jul 2020 17:44:07 +0000 (17:44 +0000)
Base 32 is the encoding, in which keys are given to Google Authenticator.

extra/totp/totp-docs.factor
extra/totp/totp.factor

index 449ade92317343f135e9ba17d5e8c4886aecc0f7..47cae0b0d93560946efd19881d441434a18c96f5 100644 (file)
@@ -31,10 +31,10 @@ $nl
 
 HELP: totp
 { $values
-    { "key" byte-array }
+    { "key" object }
     { "string" string }
 }
-{ $description "Generate a one-time password for the " { $snippet "key" } " based on the current system time. The " { $snippet "string" } " length is " { $link totp-digits } ", and the hash used for HMAC is " { $link totp-hash } "." } ;
+{ $description "Generate a one-time password for the " { $snippet "key" } " based on the current system time. If " { $snippet "key" } " is a " { $link string } ", it is expected to contain the key data in Base 32 encoding, otherwise it should be a " { $link byte-array } ". The " { $snippet "string" } " length is " { $link totp-digits } ", and the hash used for HMAC is " { $link totp-hash } "." } ;
 
 { totp totp* } related-words
 
index 5d78b37b42d3db5338a8c4c8ef6b043f411ea03b..c41f8766c2d4cc238df4875e1494e3ec10bd7299 100644 (file)
@@ -1,7 +1,8 @@
 ! Copyright (C) 2018 Alexander Ilin.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: calendar checksums.hmac checksums.sha io.binary kernel
-math math.bitwise math.parser namespaces sequences ;
+USING: base32 calendar checksums.hmac checksums.sha io.binary
+kernel math math.bitwise math.parser namespaces sequences
+strings unicode ;
 IN: totp
 
 SYMBOLS: totp-hash totp-digits ;
@@ -28,4 +29,5 @@ PRIVATE>
     [ number>string ] dip [ CHAR: 0 pad-head ] keep tail* ;
 
 : totp ( key -- string )
+    dup string? [ >upper base32> ] when
     now timestamp>count swap totp-hash get totp* totp-digits get digits ;