Skip to content

Instantly share code, notes, and snippets.

@dougwollison
Created March 19, 2015 18:11
Show Gist options
  • Save dougwollison/ccbf823973f7971ff9c3 to your computer and use it in GitHub Desktop.
Save dougwollison/ccbf823973f7971ff9c3 to your computer and use it in GitHub Desktop.
OTP Generator Python Script
#!/usr/bin/env python
import sys
import base64
import time
import struct
import hashlib
import hmac
# Abort with error if not used properly
if len( sys.argv ) != 2 :
exit( 'Usage: getotp <SECRET>' )
# Get and sanitize the secret
secret = sys.argv[1]
secret = secret.replace( ' ', '' )
secret = secret.upper()
# Generate the password
secret = base64.b32decode( secret )
counter = int( time.time() ) // 30;
counter = struct.pack( '>Q', counter )
hash = hmac.new( secret, counter, hashlib.sha1 ).digest()
offset = ord( hash[19] ) & 0xF
password = ( struct.unpack( '>I', hash[ offset:offset + 4 ] )[0] & 0x7FFFFFFF ) % 1000000
# Output the resulting OTP
print password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment