Amazing Arch

理解 Time-based One-Time Password

2019-03-01  本文已影响3人  天天向上卡索

理解 Time-based One-Time Password Algorithm

TOTP

本文主要介绍基于时间的一次性密码生成算法 TOTP,它由 RFC 6238 定义。和基于事件的一次性密码生成算法不同 HOTP,TOTP 是基于时间的,它和 HOTP 具有如下关系:

TOTP = HOTP(K, T)

HOTP(K,C) = Truncate(HMAC-SHA-1(K,C))

其中:

例如:

>>> import datatime
>>> import time
>>>
>>> current_time = datetime.datetime.now()
>>> print current_time
2016-04-16 22:11:38.508616
>>> time.mktime(current_time.timetuple())
1460815898.0  # Current Unix Time

当前时间为 2016-04-16 22:11:38.508616 的 Unix Time 为 1460815898.0,所以 T 为:

T = (1460815898.0 - 0) / 30 = 48693863

对于 HOTP,客户端和服务端需要同步移动因子 C;而对于 TOTP,只需客户端和服务器端采用相同的时区,不存在同步的问题,提高了便利性和通用性。


Python Demo

import hashlib
import hmac

def get_hotp_passcode(secret, count):
    data = bytearray([int(hex(count >> i & 0xff), 16) for i in (56, 48, 40, 32 ,24, 16, 8, 0)])
    hs = hmac.new(secret, data, hashlib.sha1).hexdigest()
    offset = int(hs[-1], 16)
    sbits = int(hs[offset * 2: offset * 2 + 8], 16) & 0x7fffffff
    return sbits % 1000000

def get_totp_passcode(secret, unix_time):
    return get_totp_passcode(secret, int(unix_time) / 30)

Memo

本文转载至 http://wsfdl.com/algorithm/2016/04/14/%E7%90%86%E8%A7%A3TOTP.html

上一篇下一篇

猜你喜欢

热点阅读