From 5d0827ed4ee5254f4b8b93c226645db2850aebaa Mon Sep 17 00:00:00 2001 From: Alexander Iljin Date: Wed, 29 Jul 2020 18:48:18 +0200 Subject: [PATCH] totp[-docs]: accept TOTP keys in Base 32 encoding Base 32 is the encoding, in which keys are given to Google Authenticator. --- extra/totp/totp-docs.factor | 4 ++-- extra/totp/totp.factor | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/extra/totp/totp-docs.factor b/extra/totp/totp-docs.factor index 449ade9231..47cae0b0d9 100644 --- a/extra/totp/totp-docs.factor +++ b/extra/totp/totp-docs.factor @@ -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 diff --git a/extra/totp/totp.factor b/extra/totp/totp.factor index 5d78b37b42..c41f8766c2 100644 --- a/extra/totp/totp.factor +++ b/extra/totp/totp.factor @@ -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 ;