Skip to content

Instantly share code, notes, and snippets.

@djui
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djui/6f64dab33c25944fa0da to your computer and use it in GitHub Desktop.
Save djui/6f64dab33c25944fa0da to your computer and use it in GitHub Desktop.
Shortest TOTP I know of
#!/usr/bin/env python
import base64,hashlib,hmac,struct,time,sys
if not len(sys.argv[1:]): sys.exit('usage: totp <secret>')
digest = hmac.new(base64.b32decode(sys.argv[1]), struct.pack('>Q', int(time.time()) // 30), hashlib.sha1).digest()
offset = ord(digest[19]) & 15
print '06d' % (struct.unpack('>I', digest[offset:offset+4])[0] & 0x7fffffff % 1000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment