我将时间步长设置为300秒,将TOTP数字设置为6。我没有在时间步长/间隔中接收相同的OTP,这意味着在给定的时间步长中接收到不同的OTP。即,在2019-11-03 19:15:00,我将获得一个代码; 19:13:00,我将获得另一个新代码。
这是代码:
import datetime
import time
from django_otp.oath import TOTP
from django_otp.util import random_hex
class TOTPVerification:
def __init__(self):
# secret key that will be used to generate a token,
# User can provide a custom value to the key.
self.key = random_hex(20)
# counter with which last token was verified.
# Next token must be generated at a higher counter value.
self.last_verified_counter = -1
# this value will return True, if a token has been successfully
# verified.
self.verified = False
# number of digits in a token. Default is 6
self.number_of_digits = 6
# validity period of a token. Default is 30 second.
self.token_validity_period = 300
def totp_obj(self):
# create a TOTP object
totp = TOTP(key=self.key,
step=self.token_validity_period,
digits=self.number_of_digits)
# the current time will be used to generate a counter
totp.time = time.time()
return totp
def generate_token(self):
# get the TOTP object and use that to create token
totp = self.totp_obj()
# token can be obtained with `totp.token()`
token = str(totp.token()).zfill(6)
return token
def verify_token(self, token, tolerance=0):
try:
# convert the input token to integer
token = int(token)
except ValueError:
# return False, if token could not be converted to an integer
self.verified = False
else:
totp = self.totp_obj()
# check if the current counter value is higher than the value of
# last verified counter and check if entered token is correct by
# calling totp.verify_token()
if ((totp.t() > self.last_verified_counter) and
(totp.verify(token, tolerance=tolerance))):
# if the condition is true, set the last verified counter value
# to current counter value, and return True
self.last_verified_counter = totp.t()
self.verified = True
else:
# if the token entered was invalid or if the counter value
# was less than last verified counter, then return False
self.verified = False
return self.verified
if __name__ == '__main__':
# verify token the normal way
phone1 = TOTPVerification()
start_time = time.time()
expiry_time = time.time() + phone1.token_validity_period
i = datetime.timedelta(seconds=start_time)
j = datetime.timedelta(seconds=expiry_time)
print('Start time:', start_time, str(i))
print('Expiry time:', expiry_time, str(j))
while time.time() < expiry_time:
k = datetime.timedelta(seconds=time.time())
generated_token = phone1.generate_token()
print("Generated token is: {} , current time is :{}".format(generated_token, k))
time.sleep(35)
结果:
('Start time:', 1573004969.106607, '18206 days, 1:49:29.106607')
('Expiry time:', 1573005269.106608, '18206 days, 1:54:29.106608')
Generated token is: 400250 , current time is :18206 days, 1:49:29.107410
Generated token is: 219130 , current time is :18206 days, 1:50:04.109554
Generated token is: 219130 , current time is :18206 days, 1:50:39.115040
Generated token is: 219130 , current time is :18206 days, 1:51:14.118708
Generated token is: 219130 , current time is :18206 days, 1:51:49.124145
Generated token is: 219130 , current time is :18206 days, 1:52:24.124820
Generated token is: 219130 , current time is :18206 days, 1:52:59.126073
Generated token is: 219130 , current time is :18206 days, 1:53:34.126536
Generated token is: 219130 , current time is :18206 days, 1:54:09.131970
我做错了什么,我想念什么吗?。