<s> import sys <EOL> import unittest <EOL> import pyhsm <EOL> import test_common <EOL> class TestHMACSHA1 ( test_common . YHSM_TestCase ) : <EOL> def setUp ( self ) : <EOL> test_common . YHSM_TestCase . setUp ( self ) <EOL> self . kh = <NUM_LIT> <EOL> def test_nist_test_vector ( self ) : <EOL> """<STR_LIT>""" <EOL> data = '<STR_LIT>' <EOL> this = self . hsm . hmac_sha1 ( self . kh , data ) . execute ( ) <EOL> self . assertEquals ( this . get_hash ( ) . encode ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEquals ( str , type ( str ( this ) ) ) <EOL> def test_hmac_numeric_flags ( self ) : <EOL> """<STR_LIT>""" <EOL> data = '<STR_LIT>' <EOL> flags = pyhsm . defines . YSM_HMAC_SHA1_RESET | pyhsm . defines . YSM_HMAC_SHA1_FINAL <EOL> this = self . hsm . hmac_sha1 ( self . kh , data , flags = flags ) . execute ( ) <EOL> self . assertEquals ( this . get_hash ( ) . encode ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_hmac_continuation ( self ) : <EOL> """<STR_LIT>""" <EOL> data = '<STR_LIT>' <EOL> this = self . hsm . hmac_sha1 ( self . kh , data [ : <NUM_LIT:3> ] , final = False ) <EOL> self . assertEquals ( this . get_hash ( ) . encode ( '<STR_LIT>' ) , '<STR_LIT>' * <NUM_LIT:20> ) <EOL> this . next ( data [ <NUM_LIT:3> : ] , final = True ) . execute ( ) <EOL> self . assertEquals ( this . get_hash ( ) . encode ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_hmac_continuation2 ( self ) : <EOL> """<STR_LIT>""" <EOL> data = '<STR_LIT>' <EOL> this = self . hsm . hmac_sha1 ( self . kh , '<STR_LIT>' , final = False ) <EOL> self . assertEquals ( this . get_hash ( ) . encode ( '<STR_LIT>' ) , '<STR_LIT>' * <NUM_LIT:20> ) <EOL> this . next ( data [ : <NUM_LIT:3> ] , final = False ) . execute ( ) <EOL> this . next ( data [ <NUM_LIT:3> : ] , final = False ) . execute ( ) <EOL> this . next ( '<STR_LIT>' , final = True ) . execute ( ) <EOL> self . assertEquals ( this . get_hash ( ) . encode ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_hmac_interrupted ( self ) : <EOL> """<STR_LIT>""" <EOL> data = '<STR_LIT>' <EOL> this = self . hsm . hmac_sha1 ( self . kh , data [ : <NUM_LIT:3> ] , final = False ) <EOL> self . assertEquals ( this . get_hash ( ) . encode ( '<STR_LIT>' ) , '<STR_LIT>' * <NUM_LIT:20> ) <EOL> self . assertTrue ( self . hsm . echo ( '<STR_LIT>' ) ) <EOL> this . next ( data [ <NUM_LIT:3> : ] , final = True ) . execute ( ) <EOL> self . assertEquals ( this . get_hash ( ) . encode ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_hmac_interrupted2 ( self ) : <EOL> """<STR_LIT>""" <EOL> data = '<STR_LIT>' <EOL> plaintext = '<STR_LIT>' . ljust ( pyhsm . defines . YSM_BLOCK_SIZE ) <EOL> kh_encrypt = <NUM_LIT> <EOL> kh_decrypt = <NUM_LIT> <EOL> this = self . hsm . hmac_sha1 ( self . kh , data [ : <NUM_LIT:3> ] , final = False ) <EOL> self . assertEquals ( this . get_hash ( ) . encode ( '<STR_LIT>' ) , '<STR_LIT>' * <NUM_LIT:20> ) <EOL> ciphertext = self . hsm . aes_ecb_encrypt ( kh_encrypt , plaintext ) <EOL> self . assertNotEqual ( plaintext , ciphertext ) <EOL> decrypted = self . hsm . aes_ecb_decrypt ( kh_decrypt , ciphertext ) <EOL> self . assertEqual ( plaintext , decrypted ) <EOL> this . next ( data [ <NUM_LIT:3> : ] , final = True ) . execute ( ) <EOL> self . assertEquals ( this . get_hash ( ) . encode ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_hmac_wrong_key_handle ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> res = self . hsm . hmac_sha1 ( <NUM_LIT> , '<STR_LIT:foo>' ) . execute ( ) <EOL> self . fail ( "<STR_LIT>" % ( res ) ) <EOL> except pyhsm . exception . YHSM_CommandFailed , e : <EOL> self . assertEquals ( e . status , pyhsm . defines . YSM_FUNCTION_DISABLED ) <EOL> def test_who_can_hash ( self ) : <EOL> """<STR_LIT>""" <EOL> data = '<STR_LIT>' <EOL> this = lambda kh : self . hsm . hmac_sha1 ( kh , data ) . execute ( ) <EOL> self . who_can ( this , expected = [ <NUM_LIT> ] ) <EOL> def test_generated_sha1_class ( self ) : <EOL> """<STR_LIT>""" <EOL> this = pyhsm . hmac_cmd . YHSM_GeneratedHMACSHA1 ( <NUM_LIT> , '<STR_LIT:a>' * <NUM_LIT:20> , True ) <EOL> self . assertEquals ( str , type ( str ( this ) ) ) <EOL> def test_sha1_to_buffer ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( <NUM_LIT:0> , self . hsm . load_random ( <NUM_LIT:0> , offset = <NUM_LIT:0> ) ) <EOL> self . hsm . hmac_sha1 ( self . kh , '<STR_LIT>' , to_buffer = True ) <EOL> self . assertEqual ( pyhsm . defines . YSM_SHA1_HASH_SIZE , self . hsm . load_random ( <NUM_LIT:0> , offset = <NUM_LIT:1> ) ) <EOL> def test_hmac_continuation_with_buffer ( self ) : <EOL> """<STR_LIT>""" <EOL> data = '<STR_LIT>' <EOL> self . assertEqual ( <NUM_LIT:0> , self . hsm . load_random ( <NUM_LIT:0> , offset = <NUM_LIT:0> ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . hsm . load_random ( <NUM_LIT:0> , offset = <NUM_LIT:1> ) ) <EOL> this = self . hsm . hmac_sha1 ( self . kh , '<STR_LIT>' , final = False ) <EOL> self . assertEquals ( this . get_hash ( ) . encode ( '<STR_LIT>' ) , '<STR_LIT>' * <NUM_LIT:20> ) <EOL> this . next ( data [ : <NUM_LIT:3> ] , final = False ) . execute ( ) <EOL> this . next ( data [ <NUM_LIT:3> : ] , final = False ) . execute ( ) <EOL> this . next ( '<STR_LIT>' , final = True , to_buffer = True ) . execute ( ) <EOL> self . assertEqual ( pyhsm . defines . YSM_SHA1_HASH_SIZE , self . hsm . load_random ( <NUM_LIT:0> , offset = <NUM_LIT:1> ) ) </s>
