<s> """<STR_LIT>""" <EOL> import sys <EOL> import time <EOL> import hmac <EOL> import base64 <EOL> import struct <EOL> import hashlib <EOL> import warnings <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> PY2 = False <EOL> string_type = str <EOL> else : <EOL> PY2 = True <EOL> string_type = unicode <EOL> range = xrange <EOL> __author__ = '<STR_LIT>' <EOL> __homepage__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> HOTP = '<STR_LIT>' <EOL> TOTP = '<STR_LIT>' <EOL> class OtpAuth ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , secret ) : <EOL> self . secret = secret <EOL> def hotp ( self , counter = <NUM_LIT:4> ) : <EOL> """<STR_LIT>""" <EOL> return generate_hotp ( self . secret , counter ) <EOL> def totp ( self , period = <NUM_LIT:30> , timestamp = None ) : <EOL> """<STR_LIT>""" <EOL> return generate_totp ( self . secret , period , timestamp ) <EOL> def valid_hotp ( self , code , last = <NUM_LIT:0> , trials = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> if not valid_code ( code ) : <EOL> return False <EOL> code = bytes ( int ( code ) ) <EOL> for i in range ( last + <NUM_LIT:1> , last + trials + <NUM_LIT:1> ) : <EOL> if compare_digest ( bytes ( self . hotp ( counter = i ) ) , code ) : <EOL> return i <EOL> return False <EOL> def valid_totp ( self , code , period = <NUM_LIT:30> , timestamp = None ) : <EOL> """<STR_LIT>""" <EOL> if not valid_code ( code ) : <EOL> return False <EOL> return compare_digest ( <EOL> bytes ( self . totp ( period , timestamp ) ) , <EOL> bytes ( int ( code ) ) <EOL> ) <EOL> @ property <EOL> def encoded_secret ( self ) : <EOL> secret = base64 . b32encode ( to_bytes ( self . secret ) ) <EOL> secret = secret . decode ( '<STR_LIT:utf-8>' ) <EOL> return secret . strip ( '<STR_LIT:=>' ) <EOL> def to_uri ( self , type , label , issuer , counter = None ) : <EOL> """<STR_LIT>""" <EOL> type = type . lower ( ) <EOL> if type not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if type == '<STR_LIT>' and not counter : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> url = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> dct = dict ( <EOL> type = type , label = label , issuer = issuer , <EOL> secret = self . encoded_secret , counter = counter <EOL> ) <EOL> ret = url % dct <EOL> if type == '<STR_LIT>' : <EOL> ret = '<STR_LIT>' % ( ret , counter ) <EOL> return ret <EOL> def to_google ( self , type , label , issuer , counter = None ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( '<STR_LIT>' , DeprecationWarning ) <EOL> return self . to_uri ( type , label , issuer , counter ) <EOL> def generate_hotp ( secret , counter = <NUM_LIT:4> ) : <EOL> """<STR_LIT>""" <EOL> msg = struct . pack ( '<STR_LIT>' , counter ) <EOL> digest = hmac . new ( to_bytes ( secret ) , msg , hashlib . sha1 ) . digest ( ) <EOL> ob = digest [ <NUM_LIT> ] <EOL> if PY2 : <EOL> ob = ord ( ob ) <EOL> pos = ob & <NUM_LIT:15> <EOL> base = struct . unpack ( '<STR_LIT>' , digest [ pos : pos + <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] & <NUM_LIT> <EOL> token = base % <NUM_LIT> <EOL> return token <EOL> def generate_totp ( secret , period = <NUM_LIT:30> , timestamp = None ) : <EOL> """<STR_LIT>""" <EOL> if timestamp is None : <EOL> timestamp = time . time ( ) <EOL> counter = int ( timestamp ) // period <EOL> return generate_hotp ( secret , counter ) <EOL> def to_bytes ( text ) : <EOL> if isinstance ( text , string_type ) : <EOL> text = text . encode ( '<STR_LIT:utf-8>' ) <EOL> return text <EOL> def valid_code ( code ) : <EOL> code = string_type ( code ) <EOL> return code . isdigit ( ) and len ( code ) <= <NUM_LIT:6> <EOL> def compare_digest ( a , b ) : <EOL> func = getattr ( hmac , '<STR_LIT>' , None ) <EOL> if func : <EOL> return func ( a , b ) <EOL> if len ( a ) != len ( b ) : <EOL> return False <EOL> rv = <NUM_LIT:0> <EOL> if PY2 : <EOL> from itertools import izip <EOL> for x , y in izip ( a , b ) : <EOL> rv |= ord ( x ) ^ ord ( y ) <EOL> else : <EOL> for x , y in zip ( a , b ) : <EOL> rv |= x ^ y <EOL> return rv == <NUM_LIT:0> </s>
