实现hotp/totp的两步验证
otpauth的Python项目详细描述
cryptography现在有内置的双因素支持。
otpauth是一次性密码验证,通常称为 两步验证。你可能从google,dropbox和 等
用法
生成并验证otp代码非常简单:
>>> from otpauth import OtpAuth >>> auth = OtpAuth('secret') # a secret string >>> auth.hotp() # generate a count based code, default count is 4 330810 >>> auth.valid_hotp(330810) 4 >>> auth.hotp(2) # generate a count based code, count is 2 720111 >>> auth.valid_hotp(720111) 2 >>> auth.totp() # generate a time based code 828657 >>> auth.valid_totp(828657) True
验证器
您可以创建一个二维码,供google authenticator扫描:
>>> from otpauth import OtpAuth >>> auth = OtpAuth('secret') # a secret string >>> s = auth.to_uri('totp', 'Example:foo@bar.baz', 'Foo') >>> import qrcode >>> img = qrcode.make(s)