Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Created March 10, 2010 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBronosky/327899 to your computer and use it in GitHub Desktop.
Save RichardBronosky/327899 to your computer and use it in GitHub Desktop.
## bashpass.py
# inspired by http://www.bash.org/?244321 and http://code.activestate.com/recipes/134892/
# This is a very nasty first effort. The next step is to shoe horn this
# functionality into getpass.py in the standard library. Demo via:
# python bashpass.py
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
def getpass(bullets='hunter2'):
import sys
password = ''
character = getch()
length = len(bullets)
output = 0
entropy = [3,1,2,3,1,2,0,1,2]
while character not in ('\r','\n','\x03'):
password += character
if output == length-entropy[0]:
entropy.append(entropy.pop(0))
sys.stdout.write('\b \b')
output -= 1
else:
try:
sys.stdout.write(bullets[output])
output += 1
except IndexError:
sys.stdout.write('\b \b')
output -= 1
character = getch()
bullets += '\n'
sys.stdout.write(bullets[output-len(bullets):])
return password
getch = _Getch()
if __name__ == '__main__':
import sys
sys.stdout.write('password: ')
password = getpass();
print "You entered '%s'" % password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment