1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Refs #27468 -- Added algorithm parameter to django.utils.crypto.salted_hmac().

This commit is contained in:
Claude Paroz
2020-01-08 16:27:26 +01:00
committed by Mariusz Felisiak
parent c00b863ac6
commit 50cf183d21
2 changed files with 44 additions and 10 deletions

View File

@@ -1,10 +1,13 @@
import hashlib
import unittest
from django.utils.crypto import constant_time_compare, pbkdf2, salted_hmac
from django.test import SimpleTestCase
from django.utils.crypto import (
InvalidAlgorithm, constant_time_compare, pbkdf2, salted_hmac,
)
class TestUtilsCryptoMisc(unittest.TestCase):
class TestUtilsCryptoMisc(SimpleTestCase):
def test_constant_time_compare(self):
# It's hard to test for constant time, just test the result.
@@ -27,11 +30,31 @@ class TestUtilsCryptoMisc(unittest.TestCase):
{'secret': 'x' * hashlib.sha1().block_size},
'bd3749347b412b1b0a9ea65220e55767ac8e96b0',
),
(
('salt', 'value'),
{'algorithm': 'sha256'},
'ee0bf789e4e009371a5372c90f73fcf17695a8439c9108b0480f14e347b3f9ec',
),
(
('salt', 'value'),
{
'algorithm': 'blake2b',
'secret': 'x' * hashlib.blake2b().block_size,
},
'fc6b9800a584d40732a07fa33fb69c35211269441823bca431a143853c32f'
'e836cf19ab881689528ede647dac412170cd5d3407b44c6d0f44630690c54'
'ad3d58',
),
]
for args, kwargs, digest in tests:
with self.subTest(args=args, kwargs=kwargs):
self.assertEqual(salted_hmac(*args, **kwargs).hexdigest(), digest)
def test_invalid_algorithm(self):
msg = "'whatever' is not an algorithm accepted by the hashlib module."
with self.assertRaisesMessage(InvalidAlgorithm, msg):
salted_hmac('salt', 'value', algorithm='whatever')
class TestUtilsCryptoPBKDF2(unittest.TestCase):