aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2014-03-03 10:14:54 -0800
committerAlex Gaynor <alex.gaynor@gmail.com>2014-03-03 10:14:54 -0800
commitc898e8d1328e6c0c65c13923a9581f7b41911ae3 (patch)
tree2b13c903789b4862ebca583c3bf4061e21d50df3
parent6963d50ff7c867d49bbd62243c3aa0a861a8e7b6 (diff)
parent9ab901df604177ea331b59b94d513af50af8f8e1 (diff)
downloadcryptography-c898e8d1328e6c0c65c13923a9581f7b41911ae3.tar.gz
cryptography-c898e8d1328e6c0c65c13923a9581f7b41911ae3.tar.bz2
cryptography-c898e8d1328e6c0c65c13923a9581f7b41911ae3.zip
Merge pull request #720 from Ayrx/hotp-length-type-check
Added length type check to HOTP and corresponding test
-rw-r--r--cryptography/hazmat/primitives/twofactor/hotp.py3
-rw-r--r--docs/hazmat/primitives/twofactor.rst6
-rw-r--r--tests/hazmat/primitives/twofactor/test_hotp.py6
3 files changed, 13 insertions, 2 deletions
diff --git a/cryptography/hazmat/primitives/twofactor/hotp.py b/cryptography/hazmat/primitives/twofactor/hotp.py
index 88bde715..83260225 100644
--- a/cryptography/hazmat/primitives/twofactor/hotp.py
+++ b/cryptography/hazmat/primitives/twofactor/hotp.py
@@ -27,6 +27,9 @@ class HOTP(object):
if len(key) < 16:
raise ValueError("Key length has to be at least 128 bits.")
+ if not isinstance(length, six.integer_types):
+ raise TypeError("Length parameter must be an integer type")
+
if length < 6 or length > 8:
raise ValueError("Length of HOTP has to be between 6 to 8.")
diff --git a/docs/hazmat/primitives/twofactor.rst b/docs/hazmat/primitives/twofactor.rst
index 0e781439..3912d483 100644
--- a/docs/hazmat/primitives/twofactor.rst
+++ b/docs/hazmat/primitives/twofactor.rst
@@ -50,7 +50,8 @@ codes (HMAC).
:raises TypeError: This is raised if the provided ``algorithm`` is not
:class:`~cryptography.hazmat.primitives.hashes.SHA1()`,
:class:`~cryptography.hazmat.primitives.hashes.SHA256()` or
- :class:`~cryptography.hazmat.primitives.hashes.SHA512()`.
+ :class:`~cryptography.hazmat.primitives.hashes.SHA512()` or if the
+ ``length`` parameter is not an integer.
.. method:: generate(counter)
@@ -145,7 +146,8 @@ similar to the following code.
:raises TypeError: This is raised if the provided ``algorithm`` is not
:class:`~cryptography.hazmat.primitives.hashes.SHA1()`,
:class:`~cryptography.hazmat.primitives.hashes.SHA256()` or
- :class:`~cryptography.hazmat.primitives.hashes.SHA512()`.
+ :class:`~cryptography.hazmat.primitives.hashes.SHA512()` or if the
+ ``length`` parameter is not an integer.
.. method:: generate(time)
diff --git a/tests/hazmat/primitives/twofactor/test_hotp.py b/tests/hazmat/primitives/twofactor/test_hotp.py
index 4c726b77..0f8c4a53 100644
--- a/tests/hazmat/primitives/twofactor/test_hotp.py
+++ b/tests/hazmat/primitives/twofactor/test_hotp.py
@@ -87,3 +87,9 @@ class TestHOTP(object):
with pytest.raises(InvalidToken):
hotp.verify(b"123456", counter)
+
+ def test_length_not_int(self, backend):
+ secret = b"12345678901234567890"
+
+ with pytest.raises(TypeError):
+ HOTP(secret, b"foo", SHA1(), backend)