def totp(key, format='dec6', period=30, t=None, hash=hashlib.sha1): ''' Compute a TOTP value as prescribed by OATH specifications. :param key: the TOTP key given as an hexadecimal string :param format: the output format, can be: - hex40, for a 40 characters hexadecimal format, - dec4, for a 4 characters decimal format, - dec6, - dec7, or - dec8 it default to dec6. :param period: a positive integer giving the period between changes of the OTP value, as seconds, it defaults to 30. :param t: a positive integer giving the current time as seconds since EPOCH (1st January 1970 at 00:00 GMT), if None we use time.time(); it defaults to None; :param hash: the hash module (usually from the hashlib package) to use, it defaults to hashlib.sha1. :returns: a string representation of the OTP value (as instructed by the format parameter). :type: str ''' if t is None: t = int(time.time()) if not isinstance(t, int): raise ValueError('Use int(time.time())') T = int(t / period) return hotp(key, T, format=format, hash=hash)
def totp(key, format='dec8', period=30, t=None, hash=hashlib.sha1): '''Compute a TOTP value as prescribed by OATH specifications. See http://tools.ietf.org/html/draft-mraihi-totp-timebased-06 ''' if t is None: t = time.time() else: if isinstance(t, datetime.datetime): t = calendar.timegm(t.utctimetuple()) else: t = int(t) T = int(t/period) return hotp(key, T, format=format, hash=hash)
def post(self): request = self.request status = '<span style="color: red">wrong password</span>' password = request.get('password') if request.get('id') and password: id = int(request.get('id')) secrets = OtpSecret.all().filter('owner', users.get_current_user()).filter('id', id) if secrets: for secret in secrets: for loop in range(0, 10): try: actual = secret.moving_factor + loop genpin = hotp.hotp(secret.secret, actual) except Exception, err: break if genpin == password: status = 'OK' secret.moving_factor = actual + 1 secret.put() break
def totp(key, format='dec6', period=30, t=None, hash=hashlib.sha1): ''' Compute a TOTP value as prescribed by OATH specifications. :param key: the TOTP key given as an hexadecimal string :param format: the output format, can be: - hex40, for a 40 characters hexadecimal format, - dec4, for a 4 characters decimal format, - dec6, - dec7, or - dec8 it default to dec6. :param period: a positive integer giving the period between changes of the OTP value, as seconds, it defaults to 30. :param t: a positive integer giving the current time as seconds since EPOCH (1st January 1970 at 00:00 GMT), if None we use time.time(); it defaults to None; :param hash: the hash module (usually from the hashlib package) to use, it defaults to hashlib.sha1. :returns: a string representation of the OTP value (as instructed by the format parameter). :type: str ''' if t is None: t = int(time.time()) else: if isinstance(t, datetime.datetime): t = calendar.timegm(t.utctimetuple()) else: t = int(t) T = int(t / period) return hotp(key, T, format=format, hash=hash)