auth.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:pyom_mud 作者: bodrich 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号