本文整理汇总了Python中authenticator.HOTP类的典型用法代码示例。如果您正苦于以下问题:Python HOTP类的具体用法?Python HOTP怎么用?Python HOTP使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HOTP类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_code_from_hash_with_alternate_lengths
def test_code_from_hash_with_alternate_lengths(self):
"""Test Otp.code_from_hash().
Try with alternate code lengths.
"""
cut = HOTP()
# code_length 1
#
should_be = ("4", "2", "2", "9", "4", "6", "2", "3", "1", "9")
for i in range(0, 10):
code = cut.code_from_hash(self.expected[i][2], 1)
self.assertEqual(should_be[i], code)
# code_length 9
#
should_be = (
"284755224",
"094287082",
"137359152",
"726969429",
"640338314",
"868254676",
"918287922",
"082162583",
"673399871",
"645520489")
for i in range(0, 10):
code = cut.code_from_hash(self.expected[i][2], 9)
self.assertEqual(should_be[i], code)
示例2: reference_generate_code_from_time
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)
示例3: test_code_from_hash_wrong_length
def test_code_from_hash_wrong_length(self):
"""Test Otp.code_from_hash().
Check that the RFC4226 test cases work for code_from_hash()
"""
cut = HOTP()
with self.assertRaises(ValueError):
cut.code_from_hash(bytes.fromhex("abcdef"))
示例4: test_generate_code_from_time_secret_wrong_type
def test_generate_code_from_time_secret_wrong_type(self):
"""Test Otp.generate_code_from_time().
Check for appropriate exception to wrong secret type.
"""
cut = HOTP()
with self.assertRaises(TypeError):
cut.generate_code_from_time(1.234)
示例5: test_code_from_hash_bad_hash
def test_code_from_hash_bad_hash(self):
"""Test Otp.code_from_hash().
Check that the RFC4226 test cases work for code_from_hash()
"""
cut = HOTP()
with self.assertRaises(TypeError):
cut.code_from_hash("abc")
示例6: test_code_from_hash_long_code_length
def test_code_from_hash_long_code_length(self):
"""Test Otp.code_from_hash().
Check that the RFC4226 test cases work for code_from_hash()
"""
cut = HOTP()
with self.assertRaises(ValueError):
cut.code_from_hash(self.expected[0][2], 11)
示例7: test_hash_from_hmac_not_20bytes
def test_hash_from_hmac_not_20bytes(self):
"""Test Otp.hash_from_hmac().
Check that HMAC length validation is performed.
"""
cut = HOTP()
hmac = bytes.fromhex("ff0102030405060708090a0b0c0d0e0f101112")
with self.assertRaises(ValueError):
cut.hash_from_hmac(hmac)
示例8: test_hash_from_hmac_not_byte_string
def test_hash_from_hmac_not_byte_string(self):
"""Test Otp.hash_from_hmac().
Check that HMAC type (byte string) validation is performed.
"""
cut = HOTP()
hmac = bytearray.fromhex("ff0102030405060708090a0b0c0d0e0f101112f0")
with self.assertRaises(TypeError):
cut.hash_from_hmac(hmac)
示例9: test_generate_code_from_time_period_not_numeric
def test_generate_code_from_time_period_not_numeric(self):
"""Test Otp.generate_code_from_time().
Check for appropriate exception to non-numeric period.
"""
cut = HOTP()
with self.assertRaises(ValueError):
cut.generate_code_from_time(
self.secret, period="abc")
示例10: test_generate_code_from_counter_counter_wrong_type
def test_generate_code_from_counter_counter_wrong_type(self):
"""Test Otp.generate_code_from_counter().
Check for appropriate exception to wrong counter type.
"""
cut = HOTP()
with self.assertRaises(ValueError):
cut.generate_code_from_counter(
self.secret, "abcdefgh")
示例11: test_generate_hmac
def test_generate_hmac(self):
"""Test Otp.generate_hmac().
Check that the RFC4226 test cases work for generate_hmac().
"""
cut = HOTP()
for i in range(0, 10):
hmac = cut.generate_hmac(self.secret, self.expected[i][0])
self.assertEqual(self.expected[i][1], hmac)
示例12: test_code_from_hash
def test_code_from_hash(self):
"""Test Otp.code_from_hash().
Check that the RFC4226 test cases work for code_from_hash()
"""
cut = HOTP()
for i in range(0, 10):
code = cut.code_from_hash(self.expected[i][2])
self.assertEqual(self.expected[i][3], code)
示例13: test_generate_code_from_counter_counter_bad
def test_generate_code_from_counter_counter_bad(self):
"""Test Otp.generate_code_from_counter().
Check for appropriate exception to invalid counter value.
"""
cut = HOTP()
with self.assertRaises(ValueError):
cut.generate_code_from_counter(
self.secret, -1)
示例14: test_counter_from_time_period_wrong_type
def test_counter_from_time_period_wrong_type(self):
"""Test Otp.counter_from_time().
Check that providing a period that is not numeric or convertable
to numeric raises the appropriate exception.
"""
cut = HOTP()
with self.assertRaises(TypeError):
cut.counter_from_time(period=(6, 3))
示例15: test_generate_code_from_counter_code_length_wrong_type
def test_generate_code_from_counter_code_length_wrong_type(self):
"""Test Otp.generate_code_from_counter().
Check for appropriate exception to wrong code_length type.
"""
cut = HOTP()
with self.assertRaises(TypeError):
cut.generate_code_from_counter(
self.secret, self.expected[0][0], code_length=(6, 3))