totp[-docs]: accept TOTP keys in Base 32 encoding

Base 32 is the encoding, in which keys are given to Google Authenticator.
master
Alexander Iljin 2020-07-29 18:48:18 +02:00 committed by John Benediktsson
parent 92b7c32e19
commit 5d0827ed4e
2 changed files with 6 additions and 4 deletions

View 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

View 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 ;