From b5a62bd17db0e28e6842111034fa6714d0701edb Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 8 Jan 2020 16:23:08 +0100 Subject: [PATCH] Refs #27468 -- Added explicit tests for django.utils.crypto.salted_hmac() --- tests/utils_tests/test_crypto.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/utils_tests/test_crypto.py b/tests/utils_tests/test_crypto.py index c2045fc4ab..1c3868f4ca 100644 --- a/tests/utils_tests/test_crypto.py +++ b/tests/utils_tests/test_crypto.py @@ -1,7 +1,7 @@ import hashlib import unittest -from django.utils.crypto import constant_time_compare, pbkdf2 +from django.utils.crypto import constant_time_compare, pbkdf2, salted_hmac class TestUtilsCryptoMisc(unittest.TestCase): @@ -13,6 +13,25 @@ class TestUtilsCryptoMisc(unittest.TestCase): self.assertTrue(constant_time_compare('spam', 'spam')) self.assertFalse(constant_time_compare('spam', 'eggs')) + def test_salted_hmac(self): + tests = [ + ((b'salt', b'value'), {}, 'b51a2e619c43b1ca4f91d15c57455521d71d61eb'), + (('salt', 'value'), {}, 'b51a2e619c43b1ca4f91d15c57455521d71d61eb'), + ( + ('salt', 'value'), + {'secret': 'abcdefg'}, + '8bbee04ccddfa24772d1423a0ba43bd0c0e24b76', + ), + ( + ('salt', 'value'), + {'secret': 'x' * hashlib.sha1().block_size}, + 'bd3749347b412b1b0a9ea65220e55767ac8e96b0', + ), + ] + for args, kwargs, digest in tests: + with self.subTest(args=args, kwargs=kwargs): + self.assertEqual(salted_hmac(*args, **kwargs).hexdigest(), digest) + class TestUtilsCryptoPBKDF2(unittest.TestCase):