def process_file(path, fn, args, state):
full_fn = os.path.join(path, fn)<EOL>if not re.match("<STR_LIT>", fn):<EOL><INDENT>if args.debug:<EOL><INDENT>sys.stderr.write("<STR_LIT>" % (full_fn))<EOL><DEDENT>return True<EOL><DEDENT>if (args.start_id is not None) or (args.stop_id is not None):<EOL><INDENT>this = int(pyhsm.yubikey.modhex_decode(fn), <NUM_LIT:16>)<EOL>if (args.start_id is not None) and this < args.start_id:<EOL><INDENT>if args.debug:<EOL><INDENT>sys.stderr.write("<STR_LIT>" % (fn, this, args.start_id))<EOL><DEDENT>return True<EOL><DEDENT>if (args.stop_id is not None) and this > args.stop_id:<EOL><INDENT>if args.debug:<EOL><INDENT>sys.stderr.write("<STR_LIT>" % (fn, this, args.stop_id))<EOL><DEDENT>return True<EOL><DEDENT><DEDENT>if args.debug:<EOL><INDENT>sys.stderr.write("<STR_LIT>" % full_fn)<EOL><DEDENT>aead = pyhsm.aead_cmd.YHSM_GeneratedAEAD(None, None, '<STR_LIT>')<EOL>aead.load(full_fn)<EOL>if not aead.nonce:<EOL><INDENT>if args.key_handle is None:<EOL><INDENT>sys.stderr.write("<STR_LIT>" % (full_fn))<EOL>state.log_failed(full_fn)<EOL>return False<EOL><DEDENT>aead.key_handle = args.key_handle<EOL>aead.nonce = pyhsm.yubikey.modhex_decode(fn).decode('<STR_LIT>')<EOL><DEDENT>if args.debug:<EOL><INDENT>sys.stderr.write("<STR_LIT>" % aead)<EOL>sys.stderr.write("<STR_LIT>" % (len(aead.data), aead.data.encode('<STR_LIT>')))<EOL><DEDENT>pt = pyhsm.soft_hsm.aesCCM(args.aes_key, aead.key_handle, aead.nonce, aead.data, decrypt = True)<EOL>if args.print_filename:<EOL><INDENT>print(("<STR_LIT>" % (full_fn)), end='<STR_LIT:U+0020>')<EOL><DEDENT>if args.format == '<STR_LIT>':<EOL><INDENT>print((pt.encode('<STR_LIT>')))<EOL><DEDENT>elif args.format == '<STR_LIT>':<EOL><INDENT>ct = pyhsm.soft_hsm.aesCCM(args.aes_key_out, args.key_handle_out, aead.nonce, pt, decrypt = False)<EOL>aead_out = pyhsm.aead_cmd.YHSM_GeneratedAEAD(aead.nonce, args.key_handle_out, ct)<EOL>filename = aead_filename(args.output_dir, args.key_handle_out_orig, fn)<EOL>aead_out.save(filename)<EOL>if args.print_filename:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT><DEDENT>elif args.format == '<STR_LIT>':<EOL><INDENT>key = pt[:pyhsm.defines.KEY_SIZE]<EOL>uid = pt[pyhsm.defines.KEY_SIZE:]<EOL>access_code = '<STR_LIT>' * <NUM_LIT:6><EOL>timestamp = '<STR_LIT>'<EOL>global yknum<EOL>yknum += <NUM_LIT:1><EOL>print(("<STR_LIT>" % (yknum,<EOL>fn,<EOL>uid.encode('<STR_LIT>'),<EOL>key.encode('<STR_LIT>'),<EOL>access_code,<EOL>timestamp,<EOL>)))<EOL><DEDENT>state.log_success(full_fn)<EOL>return True<EOL>
The main function for reading a file and decrypting it.