←back to thread

248 points dogacel | 1 comments | | HN request time: 0.291s | source
Show context
3eb7988a1663 ◴[] No.43688279[source]
It is a bit terse, but there is a 20-line Python implementation which cleared up the ideas for me: https://github.com/susam/mintotp
replies(4): >>43688338 #>>43688730 #>>43689457 #>>43689797 #
lifthrasiir ◴[] No.43688730[source]
It is even shorter without boilerplates:

    def hotp(key, counter, digits=6, digest='sha1'):
        key = base64.b32decode(key.upper() + '=' * ((8 - len(key)) % 8))
        counter = struct.pack('>Q', counter)
        mac = hmac.new(key, counter, digest).digest()
        offset = mac[-1] & 0x0f
        binary = struct.unpack('>L', mac[offset:offset+4])[0] & 0x7fffffff
        return str(binary)[-digits:].zfill(digits)
    
    def totp(key, time_step=30, digits=6, digest='sha1'):
        return hotp(key, int(time.time() / time_step), digits, digest)
replies(2): >>43690702 #>>43695989 #
1. LtWorf ◴[] No.43695989[source]
Why not do an integer division?