<commit_before>! Copyright (C) 2018, 2020 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: base32 calendar checksums.hmac checksums.sha endian
kernel math math.bitwise math.parser namespaces sequences
strings unicode ;
IN: totp

SYMBOLS: totp-hash totp-digits ;
totp-hash [ sha1 ] initialize
totp-digits [ 6 ] initialize

<PRIVATE

: totp-value ( hash-bytes -- n )
    [ last 4 bits dup 4 + ] keep <slice> be> 31 clear-bit ;

PRIVATE>

: timestamp>count* ( timestamp secs/count -- count )
    [ timestamp>unix-time ] dip /i 8 >be ; inline

: timestamp>count ( timestamp -- count )
    30 timestamp>count* ;

: totp* ( count key hash -- n )
    hmac-bytes totp-value ;

: digits ( n digits -- string )
    [ 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 ;<commit_msg>totp: ignore spaces in the key string<commit_after>! Copyright (C) 2018, 2020 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: base32 calendar checksums.hmac checksums.sha endian
kernel math math.bitwise math.parser namespaces sequences
strings unicode ;
IN: totp

SYMBOLS: totp-hash totp-digits ;
totp-hash [ sha1 ] initialize
totp-digits [ 6 ] initialize

<PRIVATE

: totp-value ( hash-bytes -- n )
    [ last 4 bits dup 4 + ] keep <slice> be> 31 clear-bit ;

PRIVATE>

: timestamp>count* ( timestamp secs/count -- count )
    [ timestamp>unix-time ] dip /i 8 >be ; inline

: timestamp>count ( timestamp -- count )
    30 timestamp>count* ;

: totp* ( count key hash -- n )
    hmac-bytes totp-value ;

: digits ( n digits -- string )
    [ number>string ] dip [ CHAR: 0 pad-head ] keep tail* ;

: totp ( key -- string )
    dup string? [ [ CHAR: space = ] reject >upper base32> ] when
    now timestamp>count swap totp-hash get totp* totp-digits get digits ;
{'fraction_non_alphanumeric': 0.12141740489838458, 'fraction_numerical': 0.022928608650338717, 'mean_word_length': 3.183006535947712, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1166707', 'n_tokens_mistral': 685, 'n_tokens_neox': 633, 'n_words': 211}