當前位置: 首頁>>代碼示例>>Python>>正文


Python authenticator.HOTP類代碼示例

本文整理匯總了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)
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:29,代碼來源:test_hotp.py

示例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)
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:57,代碼來源:test_hotp.py

示例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"))
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:9,代碼來源:test_hotp.py

示例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)
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:9,代碼來源:test_hotp.py

示例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")
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:9,代碼來源:test_hotp.py

示例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)
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:9,代碼來源:test_hotp.py

示例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)
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:10,代碼來源:test_hotp.py

示例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)
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:10,代碼來源:test_hotp.py

示例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")
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:10,代碼來源:test_hotp.py

示例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")
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:10,代碼來源:test_hotp.py

示例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)
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:10,代碼來源:test_hotp.py

示例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)
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:10,代碼來源:test_hotp.py

示例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)
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:10,代碼來源:test_hotp.py

示例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))
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:10,代碼來源:test_hotp.py

示例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))
開發者ID:JeNeSuisPasDave,項目名稱:authenticator,代碼行數:10,代碼來源:test_hotp.py


注:本文中的authenticator.HOTP類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。