From 774730673d0d339de09e1e642cabcb6c9dbb0676 Mon Sep 17 00:00:00 2001 From: MMGen Date: Sun, 6 May 2018 12:31:17 +0000 Subject: [PATCH] crypto.py: user random cleanups --- mmgen/crypto.py | 16 ++++++---------- mmgen/term.py | 15 ++++++++------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/mmgen/crypto.py b/mmgen/crypto.py index ae85ee59..8a54b9f4 100755 --- a/mmgen/crypto.py +++ b/mmgen/crypto.py @@ -129,24 +129,20 @@ def _get_random_data_from_user(uchars): msg_r(prompt.format(uchars)) import time - from mmgen.term import get_char - # time.clock() always returns zero, so we'll use time.time() - saved_time = time.time() - key_data,time_data,pp = '',[],True + from mmgen.term import get_char_raw,kb_hold_protect + key_data,time_data = '',[] for i in range(uchars): - key_data += get_char(immed_chars='ALL',prehold_protect=pp) - pp = False + kb_hold_protect() + key_data += get_char_raw() msg_r('\r'+prompt.format(uchars-i-1)) - now = time.time() - time_data.append(now - saved_time) - saved_time = now + time_data.append(time.time()) if opt.quiet: msg_r('\r') else: msg_r("\rThank you. That's enough.{}\n\n".format(' '*18)) fmt_time_data = map('{:.22f}'.format,time_data) - dmsg('\nUser input:\n{}\nKeystroke time intervals:\n{}\n'.format(key_data,'\n'.join(fmt_time_data))) + dmsg('\nUser input:\n{!r}\nKeystroke time values:\n{}\n'.format(key_data,'\n'.join(fmt_time_data))) prompt = 'User random data successfully acquired. Press ENTER to continue' prompt_and_get_char(prompt,'',enter_ok=True) diff --git a/mmgen/term.py b/mmgen/term.py index e77aad4d..eb29ff42 100755 --- a/mmgen/term.py +++ b/mmgen/term.py @@ -51,6 +51,9 @@ def _kb_hold_protect_unix(): termios.tcsetattr(fd, termios.TCSADRAIN, old) break +# Use os.read(), not file.read(), to get a variable number of bytes without blocking. +# Request 5 bytes to cover escape sequences generated by F1, F2, .. Fn keys (5 bytes) +# as well as UTF8 chars (4 bytes max). def _get_keypress_unix(prompt='',immed_chars='',prehold_protect=True): msg_r(prompt) timeout = float(0.3) @@ -60,7 +63,7 @@ def _get_keypress_unix(prompt='',immed_chars='',prehold_protect=True): while True: # Protect against held-down key before read() key = select([sys.stdin], [], [], timeout)[0] - ch = sys.stdin.read(1) + ch = os.read(fd,5) if prehold_protect: if key: continue if immed_chars == 'ALL' or ch in immed_chars: break @@ -71,21 +74,19 @@ def _get_keypress_unix(prompt='',immed_chars='',prehold_protect=True): termios.tcsetattr(fd, termios.TCSADRAIN, old) return ch -def _get_keypress_unix_stub(prompt='',immed_chars='',prehold_protect=None): - msg_r(prompt) - return sys.stdin.read(1) - -# Use os.read(), not file.read(), to get less than the requested number of characters without blocking. def _get_keypress_unix_raw(prompt='',immed_chars='',prehold_protect=None): msg_r(prompt) fd = sys.stdin.fileno() old = termios.tcgetattr(fd) tty.setcbreak(fd) - # 5 because escape sequences (F1, F2, .. Fn) have 5 bytes max. UTF8 chars have 4 bytes max ch = os.read(fd,5) termios.tcsetattr(fd, termios.TCSADRAIN, old) return ch +def _get_keypress_unix_stub(prompt='',immed_chars='',prehold_protect=None): + msg_r(prompt) + return sys.stdin.read(1) + def _kb_hold_protect_mswin(): timeout = float(0.5)