def print_results(self, matches, args): commons = matches['common'] propers = matches['proper'] if args.unformatted: if args.number: # Unformatted number print(str(len(commons + propers))) else: # Unformatted matches print('\n'.join(commons + propers)) else: width, height = get_terminal_size() width = width - 1 # Wiggle room if args.number: # Formatted number print(self.header(commons, propers, args, width)) else: # Formatted matches print(self.header(commons, propers, args, width)) if propers: print(self.formatted(propers, width, height)) if commons: print(self.formatted(commons, width, height))
def AES(): while True: title('AES Encryption / Decryption', fillchar='-') [print() for i in range(5)] print('Do you want to...') print('1. Encrypt a file') print('2. Decrypt a file') print('3. Exit') print('_' * utils.get_terminal_size()[0]) print() action = input('>> ').lower() if action in ['1', 'encrypt', 'e']: title('AES Encryption / Decryption', fillchar='-') print() print() print('Please select a file in the dialog box.') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Choose a file to encrypt...') password = getpass('Enter password for file: ').encode('utf-8') print('Choose the name for the encrypted file.') outfilename = asksaveasfilename( initialdir=cwd, title='Choose the name for the encrypted file...') chunksize = input('Enter encryption chunksize: ') or 64 * 1024 if chunksize: chunksize = int(chunksize) aes.encrypt(password, filename, outfilename, chunksize) elif action in ['2', 'decrypt', 'd']: title('AES Encryption / Decryption', fillchar='-') print() print() print('Please select a file in the dialog box.') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Choose a file to decrypt...') password = getpass('Enter password to decrpyt file: ').encode( 'utf-8') print('Choose the name for the output file.') outfilename = asksaveasfilename( initialdir=cwd, title='Choose the name for the decrypted file...') chunksize = input('Enter decryption chunksize: ') or 24 * 1024 if chunksize: chunksize = int(chunksize) aes.decrypt(password, filename, outfilename, chunksize) elif action in ['3', 'exit', 'quit', 'q']: exit(0) else: clear() _tmp = input('That is not an action.')
def get_stars(canvas, stars_count=80): stars = [] max_y, max_x = utils.get_terminal_size() for _ in range(0, stars_count): symbol = random.choice('+*.:') y_coord = random.randint(1, max_y - 1) x_coord = random.randint(1, max_x - 1) waiting_time = random.randint(0, 20) star = blink(canvas, y_coord, x_coord, symbol, waiting_time) stars.append(star) return stars
async def run_spaceship(canvas): global year global spaceship_frame global coroutines global obstacles global spaceship_frame max_available_row, max_available_column = utils.get_terminal_size() row, column = max_available_row - 10, max_available_column / 2 row_speed = column_speed = 0 while True: current_year = year.get('current_year') row_frame, column_frame = get_frame_size(spaceship_frame) prev_sprite_row, prev_sprite_column = row, column prev_spaceship_frame = spaceship_frame canvas.nodelay(True) row_pos, column_pos, space = read_controls(canvas) row_speed, column_speed = update_speed(row_speed, column_speed, row_pos, column_pos) row += row_pos + row_speed column += column_pos + column_speed if space and current_year >= 2020: # for gun position in the center of the spaceship column_for_fire = column + 2 fire_animation = fire(canvas, row, column_for_fire, rows_speed=FIRE_SPEED) coroutines.append(fire_animation) row = correct_row(max_available_row, row, row_frame) column = correct_column(max_available_column, column, column_frame) for obstacle in obstacles: if obstacle.has_collision(row, column): draw_frame(canvas, prev_sprite_row, prev_sprite_column, prev_spaceship_frame, negative=True) coroutines.append(utils.show_gameover(canvas)) return await asyncio.sleep(0) draw_frame(canvas, prev_sprite_row, prev_sprite_column, prev_spaceship_frame, negative=True) draw_frame(canvas, row, column, spaceship_frame, negative=False)
def get_trash(canvas): sizes_map = { 'large': utils.get_trash_large, 'small': utils.get_trash_small, 'xl': utils.get_trash_xl, 'hubble': utils.get_hubble, 'lamp': utils.get_lamp, 'duck': utils.get_duck, } _, max_x = utils.get_terminal_size() columns = max_x - 1 list_sizes_keys = list(sizes_map.keys()) trash_size = random.choice(list_sizes_keys) trash_frame = sizes_map[trash_size]() column_for_trash = random.randint(1, columns) return fly_garbage(canvas, column_for_trash, trash_frame)
def print_task_info(body, num_texts, rate_limit_end, rate_limited, sender_no, sms_response, success): t = time.strftime('%I:%m:%S %p %d/%m/%y') (w, h) = utils.get_terminal_size() num = math.floor(w / 2) if success: print "=" * (int(num - 3)), print(utils.bcolors.OKGREEN + " OK " + utils.bcolors.ENDC), print "=" * (int(num - 3)) else: print "=" * (int(num - 3)), print(utils.bcolors.FAIL + "FAIL" + utils.bcolors.ENDC), print "=" * (int(num - 3)) print( "[%s] SMS Response :: " % (t, ) + utils.bcolors.OKBLUE + " %s " % (sender_no, ) + utils.bcolors.ENDC + " :: consumes %d" % ( num_texts, )) if rate_limited: print("Phone number is rate limited (%s) until %s" % (sender_no, rate_limit_end)) print("IN : %s" % body) print("OUT: %s" % sms_response) print "=" * w
def init(): global screen_rows, screen_cols, cx, cy, \ file_loaded, file_rows, row_offset, column_offset, \ file_name, file_type, status_message, status_message_time, \ dirty, quit_times, last_match, direction cx, cy = 0, 0 # curr cursor location screen_rows, screen_cols = get_terminal_size() screen_rows -= 2 # status and message bar file_loaded = False file_type = None file_rows = [] row_offset = 0 # row coefficient column_offset = 0 # col coefficient file_name = None status_message = "" status_message_time = 0 dirty = False # indicate that file is modified quit_times = 3 # number of times to press quit for closing dirty files last_match = (0, -1) # last match index (row, col) direction = 1 # default search direction
def print_task_info(body, num_texts, rate_limit_end, rate_limited, sender_no, sms_response, success): t = time.strftime('%I:%m:%S %p %d/%m/%y') (w, h) = utils.get_terminal_size() num = math.floor(w / 2) if success: print "=" * (int(num - 3)), print(utils.bcolors.OKGREEN + " OK " + utils.bcolors.ENDC), print "=" * (int(num - 3)) else: print "=" * (int(num - 3)), print(utils.bcolors.FAIL + "FAIL" + utils.bcolors.ENDC), print "=" * (int(num - 3)) print("[%s] SMS Response :: " % (t, ) + utils.bcolors.OKBLUE + " %s " % (sender_no, ) + utils.bcolors.ENDC + " :: consumes %d" % (num_texts, )) if rate_limited: print("Phone number is rate limited (%s) until %s" % (sender_no, rate_limit_end)) print("IN : %s" % body) print("OUT: %s" % sms_response) print "=" * w
async def change_year_data(canvas): global year max_y, max_x = utils.get_terminal_size() while True: current_year = year.get('current_year') previous_message = utils.get_message(current_year - 1) utils.draw_frame(canvas, round(max_y - 2), round(2), str(previous_message), negative=True) message = utils.get_message(current_year) utils.draw_frame(canvas, round(max_y - 2), round(2), str(message)) if current_year == 1961: orbit_with_garbage = fill_orbit_with_garbage(canvas) coroutines.append(orbit_with_garbage) if current_year == 2020: fire_animation = get_fire(canvas) coroutines.append(fire_animation) await utils.wait_time(CHANGE_YEAR_DELAY) year['current_year'] += 1
def _get_terminal_width(): from utils import get_terminal_size result = get_terminal_size()[0] return result
def RSA(action): if action == '2': title('RSA keygen', fillchar='-') print() print() print('Select directory for the keys.') Tk().withdraw() while True: outfolder = askdirectory( initialdir=cwd, title='Select directory to save keys in...') if not outfolder: print('Please choose a directory.') else: if not os.path.exists(outfolder): os.makedirs(outfolder, exist_ok=True) break bits = int(input('Size of the key (default is 2048): ') or 2048) rsa.generate(bits, outfolder) elif action == '3': title('RSA Encryption / Decryption', fillchar='-') [print() for i in range(5)] print('Do you want to...') print('1. Encrypt a file') print('2. Decrypt a file') print('3. Exit') print('_' * utils.get_terminal_size()[0]) print() action = input('>> ').lower() if action == '1': title('RSA Encryption / Decryption', fillchar='-') print() print() print('Select a file to encrypt in the dialog box.') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Choose a file to encrypt...') print(filename) filedir = os.path.dirname(filename) print('Select the public key to encrypt the file with.') Tk().withdraw() keypath = askopenfilename(initialdir=cwd, title='Choose a public key...') print(keypath) keydir = os.path.dirname(keypath) print('Select the name for the encrypted file.') Tk().withdraw() outfile = asksaveasfilename(initialdir=filedir, title='Save as...') print('Select the name for the encrypted key.') Tk().withdraw() outkeyfile = asksaveasfilename(initialdir=filedir, title='Save as...') print(outfile) chunksize = input( 'Select chunksize (leave empty for default): ') or 64 * 1024 rsa.encrypt(keypath, filename, outfile, outkeyfile, chunksize) elif action == '2': title('RSA Encryption / Decryption', fillchar='-') print() print() print('Select a file to decrypt in the dialog box.') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Choose a file to decrypt...') print(filename) filedir = os.path.dirname(filename) print('Select the private key to decrypt the file with.') Tk().withdraw() keypath = askopenfilename(initialdir=cwd, title='Choose a private key...') print(keypath) keydir = os.path.dirname(keypath) print('Select the encrypted key file used to encrypt the file.') Tk().withdraw() keyfilepath = askopenfilename( initialdir=filedir, title='Choose the encrypted key file...') print(keyfilepath) print('Select the name for the decrypted file.') Tk().withdraw() outfile = asksaveasfilename(initialdir=filedir, title='Save as...') print(outfile) chunksize = input( 'Select chunksize (leave empty for default): ') or 24 * 1024 rsa.decrypt(keypath, filename, keyfilepath, outfile, chunksize) elif action == '3': exit(0) elif action == '4': title('RSA Signature / verification', fillchar='-') [print() for i in range(5)] print('Do you want to...') print('1. Sign a file') print('2. Verify a file') print('3. Exit') print('_' * utils.get_terminal_size()[0]) print() action = input('>> ').lower() if action == '1': title('RSA Signature / verification', fillchar='-') print() print() print('Select file to sign...') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Select file to sign...') print(filename) filedir = os.path.dirname(filedir) print('Select private key...') Tk().withdraw() privKey = askopenfilename(initialdir=cwd, title='Select private key...') print(privKey) print('Select name for signature file...') signature = asksaveasfilename( initialdir=filedir, title='Select signature filename...') or None print(signature) sign(filename, privKey, signature) elif action == '2': title('RSA Signature / verification', fillchar='-') print() print() print('Select file to verify...') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Select file to verify...') print(filename) filedir = os.path.dirname(filename) print('Select public key...') Tk().withdraw() pubKey = askopenfilename(initialdir=cwd, title='Select public key...') print(pubKey) print('Select signature file...') signature = askopenfilename(initialdir=filedir, title='Select signature file...') print(signature) valid = verify(filename, signature, pubKey) if valid: print( 'Success! Signature and hash are the same, so the file has not been tampered with.' ) _tmp = input('Press enter to continue...') elif not valid: clear() print('FILE AUTHENTICITY COULD NOT BE VERIFIED!') print( 'Do not trust the file or its sender. Did you use the correct public key?' ) print( 'Error: Verification failed. Authenticity of file could not be verified.' ) elif action == '3': pass else: TypeError('invalid action: \'%s\' in RSA()')
type=int) args = parser.parse_args() title('Crypto 2.0!') [print() for i in range(5)] print('What do you want to do?') print("1. Encrypt/decrypt a file with AES") print("2. Generate RSA keys") print("3. Encrypt/decrypt a file with RSA") print('4. Sign a file using RSA') print() print('Type the number corresponding to the desired action.') print('Type q, quit or ctrl-c to quit.') if args.port: print('Port is %s' % args.port) print('_' * utils.get_terminal_size()[0]) print() try: action = input('> ').lower() except KeyboardInterrupt: clear() exit(0) if action == 'q' or action == 'quit': clear() exit(0) elif action in ['1', 'aes']: AES() elif action in ['2', '3', '4']: RSA(action) #!/usr/bin/env python3
def get_fire(canvas): max_y, max_x = utils.get_terminal_size() return fire(canvas, max_y - 11, round(max_x / 2) + 2, rows_speed=FIRE_SPEED)