Browse Source

crypto.py: user random cleanups

MMGen 7 years ago
parent
commit
774730673d
2 changed files with 14 additions and 17 deletions
  1. 6 10
      mmgen/crypto.py
  2. 8 7
      mmgen/term.py

+ 6 - 10
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)
 

+ 8 - 7
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)