本文整理汇总了Python中cryptography.hazmat.primitives.twofactor.totp.TOTP类的典型用法代码示例。如果您正苦于以下问题:Python TOTP类的具体用法?Python TOTP怎么用?Python TOTP使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TOTP类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_generate_sha512
def test_generate_sha512(self, backend, params):
secret = params["secret"]
time = int(params["time"])
totp_value = params["totp"]
totp = TOTP(secret, 8, hashes.SHA512(), 30, backend)
assert totp.generate(time) == totp_value
示例2: test_floating_point_time_generate
def test_floating_point_time_generate(self, backend):
secret = b"12345678901234567890"
time = 59.1
totp = TOTP(secret, 8, hashes.SHA1(), 30, backend)
assert totp.generate(time) == b"94287082"
示例3: test_verify_sha256
def test_verify_sha256(self, backend, params):
secret = params["secret"]
time = int(params["time"])
totp_value = params["totp"]
totp = TOTP(secret, 8, hashes.SHA256(), 30, backend)
assert totp.verify(totp_value, time) is None
示例4: test_invalid_verify
def test_invalid_verify(self, backend):
secret = b"12345678901234567890"
time = 59
totp = TOTP(secret, 8, hashes.SHA1(), 30, backend)
with pytest.raises(InvalidToken):
totp.verify(b"12345678", time)
示例5: test_get_provisioning_uri
def test_get_provisioning_uri(self, backend):
secret = b"12345678901234567890"
totp = TOTP(secret, 6, hashes.SHA1(), 30, backend=backend)
assert totp.get_provisioning_uri("Alice Smith", None) == (
"otpauth://totp/Alice%20Smith?digits=6&secret=GEZDGNBVG"
"Y3TQOJQGEZDGNBVGY3TQOJQ&algorithm=SHA1&period=30")
assert totp.get_provisioning_uri("Alice Smith", 'World') == (
"otpauth://totp/World:Alice%20Smith?digits=6&secret=GEZ"
"DGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&algorithm=SHA1&issuer=World"
"&period=30")
示例6: Totp
def Totp(self, key):
# simple constructor helper
if not key:
key = os.urandom(20) # == 160 bytes which is recommended
totp = TOTP(key,
asint(config.get('auth.multifactor.totp.length', 6)),
SHA1(),
asint(config.get('auth.multifactor.totp.time', 30)),
backend=default_backend())
totp.key = key # for convenience, else you have to use `totp._hotp._key`
return totp
示例7: Password
"""
Implementación de un sistema que valida tokens TOTP.
Atención: Revisa cuidadosemente la hora del validador/Cliente.
La mayoria de problemas con los tokens TOTP vienen por desfases
temporales entre cliente y servidor
RFC-6238 Time-based One-time Password (TOTP)
"""
import time
import os
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.twofactor.totp import TOTP, InvalidToken
from cryptography.hazmat.primitives.hashes import SHA1
key = b'abcdefghij'
totp = TOTP(key, 6, SHA1(), 30, backend=default_backend())
token = input("Token?: ").encode()
try:
totp.verify(token, time.time())
print("Token Válido")
except InvalidToken:
print("Token Inválido")
示例8: input
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.twofactor.totp import TOTP
from cryptography.hazmat.primitives.hashes import SHA1
from cryptography.hazmat.primitives.twofactor import InvalidToken
import pyqrcode
key = os.urandom(16)
counter = 1
time_value = time.time()
issuer = 'GruPyPR'
account_name = input('Your name: ')
totp = TOTP(key, 6, SHA1(), 30, backend=default_backend())
uri = totp.get_provisioning_uri(account_name, issuer)
url = pyqrcode.create(uri)
print('Scan this!\n')
url.svg('totp.svg', scale=8)
webbrowser.open('totp.svg')
while True:
try:
totp_value = bytes(input('Two factor password: '), encoding='utf-8')
totp.verify(totp_value, time.time())
print('You are authenticated!\n')
except InvalidToken:
print('You shall not pass!')
示例9: TOTP
#!/usr/bin/env python3
""" Genera un QR TOTP compatible con Google Authenticator """
import webbrowser
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.hashes import SHA1
from cryptography.hazmat.primitives.twofactor.totp import TOTP
google_url = 'http://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl='
cuenta = '[email protected]'
expedida_por = None
key = b'abcdefghij'
totp = TOTP(key, 8, SHA1(), 30, backend=default_backend())
uri = totp.get_provisioning_uri(cuenta, expedida_por)
url = '%s%s' % (google_url, uri)
webbrowser.open(url)
示例10: test_buffer_protocol
def test_buffer_protocol(self, backend):
key = bytearray(b"a long key with lots of entropy goes here")
totp = TOTP(key, 8, hashes.SHA512(), 30, backend)
time = 60
assert totp.generate(time) == b"53049576"
示例11: verify_totp_code
def verify_totp_code(user, code):
totp = TOTP(bytes(user.secret), 6, SHA1(), 30, backend=default_backend())
return totp.verify(force_bytes(code), time.time())
示例12: test_verify_totp_failure
def test_verify_totp_failure(skew):
secret = generate_totp_secret()
totp = TOTP(secret, TOTP_LENGTH, SHA1(), TOTP_INTERVAL, backend=default_backend())
value = totp.generate(time.time() + skew)
assert not verify_totp(secret, value)