<s> """<STR_LIT>""" <EOL> import struct <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> import pyhsm . exception <EOL> import pyhsm . defines <EOL> from pyhsm . cmd import YHSM_Cmd <EOL> class YHSM_Cmd_HMAC_SHA1_Write ( YHSM_Cmd ) : <EOL> """<STR_LIT>""" <EOL> status = None <EOL> result = None <EOL> def __init__ ( self , stick , key_handle , data , flags = None , final = True , to_buffer = False ) : <EOL> data = pyhsm . util . input_validate_str ( data , '<STR_LIT:data>' , max_len = pyhsm . defines . YSM_MAX_PKT_SIZE - <NUM_LIT:6> ) <EOL> self . key_handle = pyhsm . util . input_validate_key_handle ( key_handle ) <EOL> if flags != None : <EOL> flags = pyhsm . util . input_validate_int ( flags , '<STR_LIT>' , max_value = <NUM_LIT> ) <EOL> else : <EOL> flags = pyhsm . defines . YSM_HMAC_SHA1_RESET <EOL> if final : <EOL> flags |= pyhsm . defines . YSM_HMAC_SHA1_FINAL <EOL> if to_buffer : <EOL> flags |= pyhsm . defines . YSM_HMAC_SHA1_TO_BUFFER <EOL> self . final = final <EOL> self . flags = flags <EOL> packed = _raw_pack ( self . key_handle , self . flags , data ) <EOL> YHSM_Cmd . __init__ ( self , stick , pyhsm . defines . YSM_HMAC_SHA1_GENERATE , packed ) <EOL> def next ( self , data , final = False , to_buffer = False ) : <EOL> """<STR_LIT>""" <EOL> if final : <EOL> self . flags = pyhsm . defines . YSM_HMAC_SHA1_FINAL <EOL> else : <EOL> self . flags = <NUM_LIT> <EOL> if to_buffer : <EOL> self . flags |= pyhsm . defines . YSM_HMAC_SHA1_TO_BUFFER <EOL> self . payload = _raw_pack ( self . key_handle , self . flags , data ) <EOL> self . final = final <EOL> return self <EOL> def get_hash ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . executed : <EOL> raise pyhsm . exception . YHSM_Error ( "<STR_LIT>" ) <EOL> return self . result . hash_result <EOL> def __repr__ ( self ) : <EOL> if self . executed : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> hex ( id ( self ) ) , <EOL> self . key_handle , <EOL> self . flags , <EOL> self . executed , <EOL> ) <EOL> def parse_result ( self , data ) : <EOL> key_handle , self . status , num_bytes = struct . unpack_from ( '<STR_LIT>' , data , <NUM_LIT:0> ) <EOL> pyhsm . util . validate_cmd_response_hex ( '<STR_LIT>' , key_handle , self . key_handle ) <EOL> if self . status == pyhsm . defines . YSM_STATUS_OK : <EOL> if num_bytes : <EOL> sha1 = data [ <NUM_LIT:6> : <NUM_LIT:6> + num_bytes ] <EOL> else : <EOL> sha1 = '<STR_LIT:\x00>' * pyhsm . defines . YSM_SHA1_HASH_SIZE <EOL> self . result = YHSM_GeneratedHMACSHA1 ( key_handle , sha1 , self . final ) <EOL> return self <EOL> else : <EOL> raise pyhsm . exception . YHSM_CommandFailed ( '<STR_LIT>' , self . status ) <EOL> def _raw_pack ( key_handle , flags , data ) : <EOL> """<STR_LIT>""" <EOL> return struct . pack ( '<STR_LIT>' , key_handle , flags , len ( data ) ) + data <EOL> class YHSM_GeneratedHMACSHA1 ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key_handle , sha1 , final ) : <EOL> self . key_handle = key_handle <EOL> self . hash_result = sha1 <EOL> self . final = final <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> hex ( id ( self ) ) , <EOL> self . key_handle , <EOL> self . hash_result [ : <NUM_LIT:4> ] . encode ( '<STR_LIT>' ) , <EOL> self . final , <EOL> ) </s>
