<s> import sys <EOL> import pyhsm <EOL> import argparse <EOL> import getpass <EOL> default_device = "<STR_LIT>" <EOL> def parse_args ( ) : <EOL> """<STR_LIT>""" <EOL> global default_device <EOL> parser = argparse . ArgumentParser ( description = "<STR_LIT>" , <EOL> add_help = True <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> default = default_device , <EOL> required = False , <EOL> help = '<STR_LIT>' % default_device <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> type = int , dest = '<STR_LIT>' , <EOL> required = True , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> required = True , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> type = int , dest = '<STR_LIT>' , <EOL> required = False , default = <NUM_LIT:20> , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> args = parser . parse_args ( ) <EOL> if args . set and args . validate : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if not args . set and not args . validate : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return args <EOL> def generate_aead ( hsm , args , password ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> pw = password . ljust ( args . min_len , chr ( <NUM_LIT> ) ) <EOL> return hsm . generate_aead_simple ( args . nonce . decode ( '<STR_LIT>' ) , args . key_handle , pw ) <EOL> except pyhsm . exception . YHSM_CommandFailed , e : <EOL> if e . status_str == '<STR_LIT>' : <EOL> print "<STR_LIT>" % ( args . key_handle ) <EOL> return None <EOL> else : <EOL> print "<STR_LIT>" % ( e . reason ) <EOL> def validate_aead ( hsm , args , password ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> pw = password . ljust ( args . min_len , chr ( <NUM_LIT> ) ) <EOL> return hsm . validate_aead ( args . nonce . decode ( '<STR_LIT>' ) , args . key_handle , args . validate . decode ( '<STR_LIT>' ) , pw ) <EOL> except pyhsm . exception . YHSM_CommandFailed , e : <EOL> if e . status_str == '<STR_LIT>' : <EOL> print "<STR_LIT>" % ( args . key_handle ) <EOL> return None <EOL> else : <EOL> print "<STR_LIT>" % ( e . reason ) <EOL> def main ( ) : <EOL> args = parse_args ( ) <EOL> what = "<STR_LIT>" <EOL> if args . validate : <EOL> what = "<STR_LIT>" <EOL> user_input = getpass . getpass ( '<STR_LIT>' % ( what ) ) <EOL> if not user_input : <EOL> print "<STR_LIT>" <EOL> return <NUM_LIT:0> <EOL> try : <EOL> hsm = pyhsm . base . YHSM ( device = args . device , debug = args . debug ) <EOL> except pyhsm . exception . YHSM_Error , e : <EOL> print "<STR_LIT>" % e <EOL> return <NUM_LIT:1> <EOL> if args . set : <EOL> aead = generate_aead ( hsm , args , user_input ) <EOL> if not aead : <EOL> return <NUM_LIT:1> <EOL> if args . verbose : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" % ( aead . data . encode ( '<STR_LIT>' ) , args . nonce ) <EOL> else : <EOL> if not validate_aead ( hsm , args , user_input ) : <EOL> if args . verbose : <EOL> print "<STR_LIT>" <EOL> return <NUM_LIT:1> <EOL> if args . verbose : <EOL> print "<STR_LIT>" <EOL> return <NUM_LIT:0> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> res = main ( ) <EOL> sys . exit ( res ) </s>
