本文整理匯總了Python中authenticator.HOTP.num_to_counter方法的典型用法代碼示例。如果您正苦於以下問題:Python HOTP.num_to_counter方法的具體用法?Python HOTP.num_to_counter怎麽用?Python HOTP.num_to_counter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類authenticator.HOTP
的用法示例。
在下文中一共展示了HOTP.num_to_counter方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_num_to_counter_too_large
# 需要導入模塊: from authenticator import HOTP [as 別名]
# 或者: from authenticator.HOTP import num_to_counter [as 別名]
def test_num_to_counter_too_large(self):
"""Test Otp.num_to_counter().
Check that an exception is raised if a very
value is passed to num_to_counter().
"""
cut = HOTP()
num = 2**64
with self.assertRaises(ValueError):
cut.num_to_counter(num)
示例2: test_num_to_counter_negative
# 需要導入模塊: from authenticator import HOTP [as 別名]
# 或者: from authenticator.HOTP import num_to_counter [as 別名]
def test_num_to_counter_negative(self):
"""Test Otp.num_to_counter().
Check that an exception is raised if a negative
value is passed to num_to_counter().
"""
cut = HOTP()
num = -1
with self.assertRaises(ValueError):
cut.num_to_counter(num)
示例3: test_num_to_counter_not_number
# 需要導入模塊: from authenticator import HOTP [as 別名]
# 或者: from authenticator.HOTP import num_to_counter [as 別名]
def test_num_to_counter_not_number(self):
"""Test Otp.num_to_counter().
Check that an exception is raised if a non-numeric
value is passed to num_to_counter().
"""
cut = HOTP()
num = "abcd"
with self.assertRaises(ValueError):
cut.num_to_counter(num)
示例4: test_num_to_counter_float
# 需要導入模塊: from authenticator import HOTP [as 別名]
# 或者: from authenticator.HOTP import num_to_counter [as 別名]
def test_num_to_counter_float(self):
"""Test Otp.num_to_counter().
Check that floating point values work.
"""
cut = HOTP()
counter = cut.num_to_counter(12345678.9)
self.assertEqual(bytes.fromhex("0000000000bc614e"), counter)
for i in range(0, 10):
counter = cut.num_to_counter(i + 0.6)
self.assertEqual(self.expected[i][0], counter)
return
示例5: test_num_to_counter
# 需要導入模塊: from authenticator import HOTP [as 別名]
# 或者: from authenticator.HOTP import num_to_counter [as 別名]
def test_num_to_counter(self):
"""Test Otp.num_to_counter().
Check that various integer values work. Includes large
and small values.
"""
cut = HOTP()
counter = cut.num_to_counter(2 ** 63 + 7)
self.assertEqual(bytes.fromhex("8000000000000007"), counter)
for i in range(0, 10):
counter = cut.num_to_counter(i)
self.assertEqual(self.expected[i][0], counter)
return
示例6: reference_generate_code_from_time
# 需要導入模塊: from authenticator import HOTP [as 別名]
# 或者: from authenticator.HOTP import num_to_counter [as 別名]
def reference_generate_code_from_time(self, secret_key):
"""Reference implementation of generate_code_from_time method.
A reference/alternate implementation of Otp.generate_code_from_time()
which is to be used to generate expected values for unit tests.
Returns:
A tuple containing:
* The time-based OTP, as a string of digits.
* The integer number of seconds remaining in the current
interval.
"""
import time
import datetime
from hashlib import sha1
import hmac
cut = HOTP()
# message := current Unix time ÷ 30
#
local_now = datetime.datetime.now()
seconds_now = time.mktime(local_now.timetuple())
intervals = seconds_now // 30
remaining_seconds = seconds_now - (intervals * 30)
message = cut.num_to_counter(intervals)
# hash := HMAC-SHA1(key, message)
#
hmac = hmac.new(secret_key, message, sha1)
hash = hmac.hexdigest()
# offset := last nibble of hash
#
offset = int("0" + hash[-1], 16)
offset *= 2
# truncated_hash := hash[offset..offset+3]
# (that is 4 bytes starting at the offset)
#
truncated_hash = hash[offset: offset + (4 * 2)]
# Set the first bit of truncated_hash to zero
# (remove the most significant bit)
#
new_high_order_byte = hex(
int(truncated_hash[0:2], 16) & int('7F', 16))[2:]
new_high_order_byte = \
"0" * (2 - len(new_high_order_byte)) + new_high_order_byte
truncated_hash = new_high_order_byte + truncated_hash[2:]
# code := truncated_hash mod 1000000
#
int_hash = int(truncated_hash, 16)
code = int_hash % 1000000
# pad code with 0 until length of code is 6
#
code_string = str(code)
code_string = "0" * (6 - len(code_string)) + code_string
# return code
#
return code_string, int(30 - remaining_seconds)
示例7: test_hmac_from_counter
# 需要導入模塊: from authenticator import HOTP [as 別名]
# 或者: from authenticator.HOTP import num_to_counter [as 別名]
def test_hmac_from_counter(self):
"""Test Otp.generate_hmac().
Check that expected HMAC-SHA-1 digest values are produced.
"""
cut = HOTP()
for i in range(0, 10):
counter = cut.num_to_counter(i)
actual_hmac = cut.generate_hmac(self.secret, counter)
self.assertEqual(self.expected[i][1], actual_hmac)