python-trezor/trezorlib/pinmatrix.py

112 lines
3.6 KiB
Python
Raw Normal View History

2013-10-07 20:00:17 +00:00
import sys
import math
import operator
from PyQt4.Qt import QApplication, QWidget, QGridLayout, QVBoxLayout, QHBoxLayout
from PyQt4.QtGui import QPushButton, QLineEdit, QSizePolicy, QRegExpValidator, QLabel
2013-10-07 22:18:44 +00:00
from PyQt4.QtCore import QObject, SIGNAL, QRegExp, Qt
2013-10-07 20:00:17 +00:00
class PinButton(QPushButton):
def __init__(self, password, encoded_value):
super(PinButton, self).__init__()
self.password = password
self.encoded_value = encoded_value
QObject.connect(self, SIGNAL('clicked()'), self._pressed)
def _pressed(self):
self.password.setText(self.password.text() + str(self.encoded_value))
2014-01-31 18:48:19 +00:00
print self.encoded_value
2013-10-07 20:00:17 +00:00
self.password.setFocus()
class PinMatrixWidget(QWidget):
'''
Displays widget with nine blank buttons and password box.
Encodes button clicks into sequence of numbers for passing
into PinAck messages of Trezor.
show_strength=True may be useful for entering new PIN
2013-10-07 20:00:17 +00:00
'''
def __init__(self, show_strength=True, parent=None):
2013-10-07 20:00:17 +00:00
super(PinMatrixWidget, self).__init__(parent)
self.password = QLineEdit()
2013-10-07 21:27:02 +00:00
self.password.setValidator(QRegExpValidator(QRegExp('[1-9]+'), None))
2013-10-07 20:00:17 +00:00
self.password.setEchoMode(QLineEdit.Password)
QObject.connect(self.password, SIGNAL('textChanged(QString)'), self._password_changed)
self.strength = QLabel()
self.strength.setMinimumWidth(75)
self.strength.setAlignment(Qt.AlignCenter)
self._set_strength(0)
2013-10-07 20:00:17 +00:00
grid = QGridLayout()
grid.setSpacing(0)
2014-01-31 18:48:19 +00:00
for y in range(3)[::-1]:
for x in range(3):
button = PinButton(self.password, x + y * 3 + 1)
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
button.setFocusPolicy(Qt.NoFocus)
grid.addWidget(button, 3 - y, x)
2013-10-07 20:00:17 +00:00
hbox = QHBoxLayout()
hbox.addWidget(self.password)
if show_strength:
hbox.addWidget(self.strength)
2013-10-07 20:00:17 +00:00
vbox = QVBoxLayout()
vbox.addLayout(grid)
vbox.addLayout(hbox)
2013-10-07 20:00:17 +00:00
self.setLayout(vbox)
def _set_strength(self, strength):
if strength < 3000:
self.strength.setText('weak')
self.strength.setStyleSheet("QLabel { color : #d00; }")
elif strength < 60000:
self.strength.setText('fine')
self.strength.setStyleSheet("QLabel { color : #db0; }")
elif strength < 360000:
self.strength.setText('strong')
self.strength.setStyleSheet("QLabel { color : #0a0; }")
else:
self.strength.setText('ULTIMATE')
self.strength.setStyleSheet("QLabel { color : #000; font-weight: bold;}")
def _password_changed(self, password):
self._set_strength(self.get_strength())
def get_strength(self):
digits = len(set(str(self.password.text())))
strength = math.factorial(9) / math.factorial(9 - digits)
return strength
2013-10-07 20:00:17 +00:00
def get_value(self):
return self.password.text()
if __name__ == '__main__':
'''
Demo application showing PinMatrix widget in action
'''
a = QApplication(sys.argv)
matrix = PinMatrixWidget()
def clicked():
print "PinMatrix value is", matrix.get_value()
print "Possible button combinations:", matrix.get_strength()
2013-10-07 20:00:17 +00:00
sys.exit()
ok = QPushButton('OK')
QObject.connect(ok, SIGNAL('clicked()'), clicked)
vbox = QVBoxLayout()
vbox.addWidget(matrix)
vbox.addWidget(ok)
w = QWidget()
w.setLayout(vbox)
2013-10-07 23:43:16 +00:00
w.move(100, 100)
2013-10-07 20:00:17 +00:00
w.show()
a.exec_()