def time_code(self, moment: int=None):
"""
Returns a string indicating the current valid token which will be
generated, and which should be matched to authenticate the user.
:param moment: A time value, defaulting to now.
:type moment: int
:return: A 6-digit authentication token
:rtype: str
"""
if moment is None:
moment = time.time()
moment = int(moment // 30)
time_bytes = struct.pack('>q', moment)
hash_digest = hmac.HMAC(self._secret, time_bytes, hashlib.sha1).digest()
offset = hash_digest[-1] & 0x0F
truncated_digest = hash_digest[offset:offset + 4]
code = struct.unpack('>L', truncated_digest)[0]
code &= 0x7FFFFFFF
code %= 1000000
return '%06d' % code
评论列表
文章目录