Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
test.test_hmac.TestVectorsTestCase Class Reference

List of all members.

Public Member Functions

def test_md5_vectors
def test_sha_vectors
def test_sha224_rfc4231
def test_sha256_rfc4231
def test_sha384_rfc4231
def test_sha512_rfc4231
def test_legacy_block_size_warnings

Public Attributes

 digest_size

Private Member Functions

def _rfc4231_test_cases

Private Attributes

 _x

Detailed Description

Definition at line 7 of file test_hmac.py.


Member Function Documentation

def test.test_hmac.TestVectorsTestCase._rfc4231_test_cases (   self,
  hashfunc 
) [private]

Definition at line 79 of file test_hmac.py.

00079 
00080     def _rfc4231_test_cases(self, hashfunc):
00081         def hmactest(key, data, hexdigests):
00082             h = hmac.HMAC(key, data, digestmod=hashfunc)
00083             self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])
00084 
00085         # 4.2.  Test Case 1
00086         hmactest(key = b'\x0b'*20,
00087                  data = b'Hi There',
00088                  hexdigests = {
00089                    hashlib.sha224: '896fb1128abbdf196832107cd49df33f'
00090                                    '47b4b1169912ba4f53684b22',
00091                    hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b'
00092                                    '881dc200c9833da726e9376c2e32cff7',
00093                    hashlib.sha384: 'afd03944d84895626b0825f4ab46907f'
00094                                    '15f9dadbe4101ec682aa034c7cebc59c'
00095                                    'faea9ea9076ede7f4af152e8b2fa9cb6',
00096                    hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0'
00097                                    '2379f4e2ce4ec2787ad0b30545e17cde'
00098                                    'daa833b7d6b8a702038b274eaea3f4e4'
00099                                    'be9d914eeb61f1702e696c203a126854',
00100                  })
00101 
00102         # 4.3.  Test Case 2
00103         hmactest(key = b'Jefe',
00104                  data = b'what do ya want for nothing?',
00105                  hexdigests = {
00106                    hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f'
00107                                    '8bbea2a39e6148008fd05e44',
00108                    hashlib.sha256: '5bdcc146bf60754e6a042426089575c7'
00109                                    '5a003f089d2739839dec58b964ec3843',
00110                    hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b'
00111                                    '9c7ef464f5a01b47e42ec3736322445e'
00112                                    '8e2240ca5e69e2c78b3239ecfab21649',
00113                    hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3'
00114                                    '87bd64222e831fd610270cd7ea250554'
00115                                    '9758bf75c05a994a6d034f65f8f0e6fd'
00116                                    'caeab1a34d4a6b4b636e070a38bce737',
00117                  })
00118 
00119         # 4.4.  Test Case 3
00120         hmactest(key = b'\xaa'*20,
00121                  data = b'\xdd'*50,
00122                  hexdigests = {
00123                    hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264'
00124                                    '9365b0c1f65d69d1ec8333ea',
00125                    hashlib.sha256: '773ea91e36800e46854db8ebd09181a7'
00126                                    '2959098b3ef8c122d9635514ced565fe',
00127                    hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f'
00128                                    '0aa635d947ac9febe83ef4e55966144b'
00129                                    '2a5ab39dc13814b94e3ab6e101a34f27',
00130                    hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9'
00131                                    'b1b5dbdd8ee81a3655f83e33b2279d39'
00132                                    'bf3e848279a722c806b485a47e67c807'
00133                                    'b946a337bee8942674278859e13292fb',
00134                  })
00135 
00136         # 4.5.  Test Case 4
00137         hmactest(key = bytes(x for x in range(0x01, 0x19+1)),
00138                  data = b'\xcd'*50,
00139                  hexdigests = {
00140                    hashlib.sha224: '6c11506874013cac6a2abc1bb382627c'
00141                                    'ec6a90d86efc012de7afec5a',
00142                    hashlib.sha256: '82558a389a443c0ea4cc819899f2083a'
00143                                    '85f0faa3e578f8077a2e3ff46729665b',
00144                    hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7'
00145                                    '7a9981480850009cc5577c6e1f573b4e'
00146                                    '6801dd23c4a7d679ccf8a386c674cffb',
00147                    hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7'
00148                                    'e576d97ff94b872de76f8050361ee3db'
00149                                    'a91ca5c11aa25eb4d679275cc5788063'
00150                                    'a5f19741120c4f2de2adebeb10a298dd',
00151                  })
00152 
00153         # 4.7.  Test Case 6
00154         hmactest(key = b'\xaa'*131,
00155                  data = b'Test Using Larger Than Block-Siz'
00156                         b'e Key - Hash Key First',
00157                  hexdigests = {
00158                    hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2'
00159                                    'd499f112f2d2b7273fa6870e',
00160                    hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f'
00161                                    '8e0bc6213728c5140546040f0ee37f54',
00162                    hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4'
00163                                    '4f9ef1012a2b588f3cd11f05033ac4c6'
00164                                    '0c2ef6ab4030fe8296248df163f44952',
00165                    hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4'
00166                                    '9b46d1f41b4aeec1121b013783f8f352'
00167                                    '6b56d037e05f2598bd0fd2215d6a1e52'
00168                                    '95e64f73f63f0aec8b915a985d786598',
00169                  })
00170 
00171         # 4.8.  Test Case 7
00172         hmactest(key = b'\xaa'*131,
00173                  data = b'This is a test using a larger th'
00174                         b'an block-size key and a larger t'
00175                         b'han block-size data. The key nee'
00176                         b'ds to be hashed before being use'
00177                         b'd by the HMAC algorithm.',
00178                  hexdigests = {
00179                    hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd'
00180                                    '946770db9c2b95c9f6f565d1',
00181                    hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944'
00182                                    'bfdc63644f0713938a7f51535c3a35e2',
00183                    hashlib.sha384: '6617178e941f020d351e2f254e8fd32c'
00184                                    '602420feb0b8fb9adccebb82461e99c5'
00185                                    'a678cc31e799176d3860e6110c46523e',
00186                    hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd'
00187                                    'debd71f8867289865df5a32d20cdc944'
00188                                    'b6022cac3c4982b10d5eeb55c3e4de15'
00189                                    '134676fb6de0446065c97440fa8c6a58',
00190                  })

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 203 of file test_hmac.py.

00203 
00204     def test_legacy_block_size_warnings(self):
00205         class MockCrazyHash(object):
00206             """Ain't no block_size attribute here."""
00207             def __init__(self, *args):
00208                 self._x = hashlib.sha1(*args)
00209                 self.digest_size = self._x.digest_size
00210             def update(self, v):
00211                 self._x.update(v)
00212             def digest(self):
00213                 return self._x.digest()
00214 
00215         with warnings.catch_warnings():
00216             warnings.simplefilter('error', RuntimeWarning)
00217             with self.assertRaises(RuntimeWarning):
00218                 hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
00219                 self.fail('Expected warning about missing block_size')
00220 
00221             MockCrazyHash.block_size = 1
00222             with self.assertRaises(RuntimeWarning):
00223                 hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
00224                 self.fail('Expected warning about small block_size')
00225 
00226 

Here is the call graph for this function:

Definition at line 9 of file test_hmac.py.

00009 
00010     def test_md5_vectors(self):
00011         # Test the HMAC module against test vectors from the RFC.
00012 
00013         def md5test(key, data, digest):
00014             h = hmac.HMAC(key, data)
00015             self.assertEqual(h.hexdigest().upper(), digest.upper())
00016 
00017         md5test(b"\x0b" * 16,
00018                 b"Hi There",
00019                 "9294727A3638BB1C13F48EF8158BFC9D")
00020 
00021         md5test(b"Jefe",
00022                 b"what do ya want for nothing?",
00023                 "750c783e6ab0b503eaa86e310a5db738")
00024 
00025         md5test(b"\xaa" * 16,
00026                 b"\xdd" * 50,
00027                 "56be34521d144c88dbb8c733f0e8b3f6")
00028 
00029         md5test(bytes(range(1, 26)),
00030                 b"\xcd" * 50,
00031                 "697eaf0aca3a3aea3a75164746ffaa79")
00032 
00033         md5test(b"\x0C" * 16,
00034                 b"Test With Truncation",
00035                 "56461ef2342edc00f9bab995690efd4c")
00036 
00037         md5test(b"\xaa" * 80,
00038                 b"Test Using Larger Than Block-Size Key - Hash Key First",
00039                 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
00040 
00041         md5test(b"\xaa" * 80,
00042                 (b"Test Using Larger Than Block-Size Key "
00043                  b"and Larger Than One Block-Size Data"),
00044                 "6f630fad67cda0ee1fb1f562db3aa53e")

Here is the call graph for this function:

Definition at line 191 of file test_hmac.py.

00191 
00192     def test_sha224_rfc4231(self):
00193         self._rfc4231_test_cases(hashlib.sha224)

Here is the call graph for this function:

Definition at line 194 of file test_hmac.py.

00194 
00195     def test_sha256_rfc4231(self):
00196         self._rfc4231_test_cases(hashlib.sha256)

Here is the call graph for this function:

Definition at line 197 of file test_hmac.py.

00197 
00198     def test_sha384_rfc4231(self):
00199         self._rfc4231_test_cases(hashlib.sha384)

Here is the call graph for this function:

Definition at line 200 of file test_hmac.py.

00200 
00201     def test_sha512_rfc4231(self):
00202         self._rfc4231_test_cases(hashlib.sha512)

Here is the call graph for this function:

Definition at line 45 of file test_hmac.py.

00045 
00046     def test_sha_vectors(self):
00047         def shatest(key, data, digest):
00048             h = hmac.HMAC(key, data, digestmod=hashlib.sha1)
00049             self.assertEqual(h.hexdigest().upper(), digest.upper())
00050 
00051         shatest(b"\x0b" * 20,
00052                 b"Hi There",
00053                 "b617318655057264e28bc0b6fb378c8ef146be00")
00054 
00055         shatest(b"Jefe",
00056                 b"what do ya want for nothing?",
00057                 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
00058 
00059         shatest(b"\xAA" * 20,
00060                 b"\xDD" * 50,
00061                 "125d7342b9ac11cd91a39af48aa17b4f63f175d3")
00062 
00063         shatest(bytes(range(1, 26)),
00064                 b"\xCD" * 50,
00065                 "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
00066 
00067         shatest(b"\x0C" * 20,
00068                 b"Test With Truncation",
00069                 "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
00070 
00071         shatest(b"\xAA" * 80,
00072                 b"Test Using Larger Than Block-Size Key - Hash Key First",
00073                 "aa4ae5e15272d00e95705637ce8a3b55ed402112")
00074 
00075         shatest(b"\xAA" * 80,
00076                 (b"Test Using Larger Than Block-Size Key "
00077                  b"and Larger Than One Block-Size Data"),
00078                 "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")

Here is the call graph for this function:


Member Data Documentation

Definition at line 207 of file test_hmac.py.

Definition at line 208 of file test_hmac.py.


The documentation for this class was generated from the following file: