diff --git a/README.rst b/README.rst index e423a3f..3cb6a9a 100644 --- a/README.rst +++ b/README.rst @@ -15,6 +15,8 @@ Changelog +---------+------------+------------------------------------------------------+ | Version | Date | Changes | +=========+============+======================================================+ +| 1.0.2 | 2017-06-16 | - automatically pad secrets to expected length | ++---------+------------+------------------------------------------------------+ | 1.0.1 | 2015-07-31 | - fixed tests and build system, | | | | - extended test coverage with Py3.5, PyPy and PyPy3, | +---------+------------+------------------------------------------------------+ diff --git a/onetimepass/__init__.py b/onetimepass/__init__.py index 7cbccf2..95fe19a 100755 --- a/onetimepass/__init__.py +++ b/onetimepass/__init__.py @@ -3,7 +3,7 @@ time-based. It is compatible with Google Authenticator application and applications based on it. -@version: 1.0.1 +@version: 1.0.2 @author: Tomasz Jaskowski @contact: http://github.com/tadeck @license: MIT @@ -35,8 +35,8 @@ import time __author__ = 'Tomasz Jaskowski ' -__date__ = '31 July 2015' -__version_info__ = (1, 0, 1) +__date__ = '16 June 2017' +__version_info__ = (1, 1, 0) __version__ = '%s.%s.%s' % __version_info__ __license__ = 'MIT' @@ -110,6 +110,10 @@ def get_hotp( # Get rid of all the spacing: secret = secret.replace(b' ', b'') try: + # Autopad if necessary + mod = len(secret) % 8 + if mod: + secret += b'=' * (8 - mod) key = base64.b32decode(secret, casefold=casefold) except (TypeError): raise TypeError('Incorrect secret') diff --git a/tests/__init__.py b/tests/__init__.py index 58dfb2e..77cbe35 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -177,6 +177,17 @@ def test_hotp_for_range_preceding_match(self): secret = b'MFRGGZDFMZTWQ2LK' self.assertFalse(valid_hotp(713385, secret, last=1, trials=2)) + def test_autopadding(self): + """ + Check if unpadded and padded secrets resolve to the same token + """ + secret = b'MFRGGZDFMZTWQ2LK' + for length in [2, 4, 5, 7, 10, 12, 13, 15]: + fragment = secret[0:length] + fragment_padded = fragment + b'=' * (8 - len(fragment) % 8) + self.assertEqual(get_hotp(fragment_padded, 123), + get_hotp(fragment, 123)) + class TotpGenerationTestCase(TestCase): """