<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> import sys <EOL> import unittest <EOL> import binascii <EOL> from Crypto . Util . py3compat import * <EOL> if sys . hexversion < <NUM_LIT> : <EOL> def dict ( ** kwargs ) : <EOL> return kwargs . copy ( ) <EOL> else : <EOL> dict = dict <EOL> class HashDigestSizeSelfTest ( unittest . TestCase ) : <EOL> def __init__ ( self , hashmod , description , expected ) : <EOL> unittest . TestCase . __init__ ( self ) <EOL> self . hashmod = hashmod <EOL> self . expected = expected <EOL> self . description = description <EOL> def shortDescription ( self ) : <EOL> return self . description <EOL> def runTest ( self ) : <EOL> self . failUnless ( hasattr ( self . hashmod , "<STR_LIT>" ) ) <EOL> self . assertEquals ( self . hashmod . digest_size , self . expected ) <EOL> h = self . hashmod . new ( ) <EOL> self . failUnless ( hasattr ( h , "<STR_LIT>" ) ) <EOL> self . assertEquals ( h . digest_size , self . expected ) <EOL> class HashSelfTest ( unittest . TestCase ) : <EOL> def __init__ ( self , hashmod , description , expected , input ) : <EOL> unittest . TestCase . __init__ ( self ) <EOL> self . hashmod = hashmod <EOL> self . expected = expected <EOL> self . input = input <EOL> self . description = description <EOL> def shortDescription ( self ) : <EOL> return self . description <EOL> def runTest ( self ) : <EOL> h = self . hashmod . new ( ) <EOL> h . update ( self . input ) <EOL> out1 = binascii . b2a_hex ( h . digest ( ) ) <EOL> out2 = h . hexdigest ( ) <EOL> h = self . hashmod . new ( self . input ) <EOL> out3 = h . hexdigest ( ) <EOL> out4 = binascii . b2a_hex ( h . digest ( ) ) <EOL> self . assertEqual ( self . expected , out1 ) <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> : <EOL> self . assertEqual ( self . expected , out2 ) <EOL> self . assertEqual ( self . expected , out3 ) <EOL> else : <EOL> self . assertEqual ( self . expected . decode ( ) , out2 ) <EOL> self . assertEqual ( self . expected . decode ( ) , out3 ) <EOL> self . assertEqual ( self . expected , out4 ) <EOL> h2 = h . new ( ) <EOL> h2 . update ( self . input ) <EOL> out5 = binascii . b2a_hex ( h2 . digest ( ) ) <EOL> self . assertEqual ( self . expected , out5 ) <EOL> class HashTestOID ( unittest . TestCase ) : <EOL> def __init__ ( self , hashmod , oid ) : <EOL> unittest . TestCase . __init__ ( self ) <EOL> self . hashmod = hashmod <EOL> self . oid = oid <EOL> def runTest ( self ) : <EOL> h = self . hashmod . new ( ) <EOL> if self . oid == None : <EOL> try : <EOL> raised = <NUM_LIT:0> <EOL> a = h . oid <EOL> except AttributeError : <EOL> raised = <NUM_LIT:1> <EOL> self . assertEqual ( raised , <NUM_LIT:1> ) <EOL> else : <EOL> self . assertEqual ( h . oid , self . oid ) <EOL> class MACSelfTest ( unittest . TestCase ) : <EOL> def __init__ ( self , hashmod , description , expected_dict , input , key , hashmods ) : <EOL> unittest . TestCase . __init__ ( self ) <EOL> self . hashmod = hashmod <EOL> self . expected_dict = expected_dict <EOL> self . input = input <EOL> self . key = key <EOL> self . hashmods = hashmods <EOL> self . description = description <EOL> def shortDescription ( self ) : <EOL> return self . description <EOL> def runTest ( self ) : <EOL> for hashname in self . expected_dict . keys ( ) : <EOL> hashmod = self . hashmods [ hashname ] <EOL> key = binascii . a2b_hex ( b ( self . key ) ) <EOL> data = binascii . a2b_hex ( b ( self . input ) ) <EOL> expected = b ( "<STR_LIT>" . join ( self . expected_dict [ hashname ] . split ( ) ) ) <EOL> h = self . hashmod . new ( key , digestmod = hashmod ) <EOL> h . update ( data ) <EOL> out1 = binascii . b2a_hex ( h . digest ( ) ) <EOL> out2 = h . hexdigest ( ) <EOL> h = self . hashmod . new ( key , data , hashmod ) <EOL> out3 = h . hexdigest ( ) <EOL> out4 = binascii . b2a_hex ( h . digest ( ) ) <EOL> h2 = h . copy ( ) <EOL> h . update ( b ( "<STR_LIT>" ) ) <EOL> out5 = binascii . b2a_hex ( h2 . digest ( ) ) <EOL> self . assertEqual ( expected , out1 ) <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> : <EOL> self . assertEqual ( expected , out2 ) <EOL> self . assertEqual ( expected , out3 ) <EOL> else : <EOL> self . assertEqual ( expected . decode ( ) , out2 ) <EOL> self . assertEqual ( expected . decode ( ) , out3 ) <EOL> self . assertEqual ( expected , out4 ) <EOL> self . assertEqual ( expected , out5 ) <EOL> def make_hash_tests ( module , module_name , test_data , digest_size , oid = None ) : <EOL> tests = [ ] <EOL> for i in range ( len ( test_data ) ) : <EOL> row = test_data [ i ] <EOL> ( expected , input ) = map ( b , row [ <NUM_LIT:0> : <NUM_LIT:2> ] ) <EOL> if len ( row ) < <NUM_LIT:3> : <EOL> description = repr ( input ) <EOL> else : <EOL> description = row [ <NUM_LIT:2> ] . encode ( '<STR_LIT>' ) <EOL> name = "<STR_LIT>" % ( module_name , i + <NUM_LIT:1> , description ) <EOL> tests . append ( HashSelfTest ( module , name , expected , input ) ) <EOL> if oid is not None : <EOL> oid = b ( oid ) <EOL> name = "<STR_LIT>" % ( module_name , i + <NUM_LIT:1> ) <EOL> tests . append ( HashDigestSizeSelfTest ( module , name , digest_size ) ) <EOL> tests . append ( HashTestOID ( module , oid ) ) <EOL> return tests <EOL> def make_mac_tests ( module , module_name , test_data , hashmods ) : <EOL> tests = [ ] <EOL> for i in range ( len ( test_data ) ) : <EOL> row = test_data [ i ] <EOL> ( key , data , results , description ) = row <EOL> name = "<STR_LIT>" % ( module_name , i + <NUM_LIT:1> , description ) <EOL> tests . append ( MACSelfTest ( module , name , results , data , key , hashmods ) ) <EOL> return tests </s>
