Commit 36b22493 authored by tobias.dussa's avatar tobias.dussa

Cleanup, moved password hiding/showing code into main file.

parent 585c4647
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from OpenSSL import crypto, SSL
from os.path import basename, dirname, isfile, splitext
from sys import exit
from tkinter import Tk, filedialog, messagebox, simpledialog, Label, Entry, Button, N, E, W, S, NORMAL, DISABLED
class PasswordEntry:
def __init__(self, master):
self.master = master
master.title("Enter Password")
infilename = ""
infile = ""
self.label = Label(master, text = "Passwort")
self.label.grid(row=0, sticky=W)
self.passentry = Entry(master)
self.passentry.grid(row=0, column=1, sticky=W)
self.toggleShow()
self.passentry.focus_set()
self.passentry.bind("<Return>", self.reencodeOnReturn)
#self.toggle = Button(master, text="⚷")
#self.toggle.bind("<Button-1>", lambda a: self.passentry["show"] == "")
#self.toggle.bind("<ButtonRelease-1>", lambda a: self.passentry["show"] == "●")
self.toggle = Button(master, text="⚷", command=self.toggleShow)
self.toggle.grid(row=0, column=2, sticky=W)
self.okbutton = Button(master, text="OK", command=self.reencode)
self.okbutton.grid(row=1, column=0, columnspan=3, sticky=W+E+N+S)
self.okbutton.config()
# ask for input file
self.askInputFile()
def reencodeOnReturn(self, event):
self.reencode()
# check password while being typed in
def reencode(self):
try:
p12 = crypto.load_pkcs12(self.infile, self.passentry.get().encode('utf-8'))
except crypto.Error:
messagebox.showerror("Ok", "Falsches Passwort")
return
# write output file
outfile = filedialog.asksaveasfilename(title='PKCS12-Ausgabedatei',
initialdir=dirname(self.infilename),
initialfile=splitext(basename(self.infilename))[0] + '-repariert.p12',
defaultextension='.p12',
filetypes=(('PKCS12-Dateien', ('*.p12', '*.P12', '*.pfx', '*.PFX')),
('alle Dateien', '*.*')))
if not (outfile):
messagebox.showinfo('Abbruch', 'Die Dateiauswahl wurde abgebrochen!')
return
try:
with open(outfile, 'wb') as file:
file.write(p12.export(self.passentry.get().encode('utf-8')))
except:
messagebox.showinfo('Fehler!', 'Kann PKCS12-Datei {} nicht schreiben!'.format(outfile))
self.master.quit()
# toggle password hiding
def toggleShow(self):
if self.passentry["show"] == "":
self.passentry["show"] = "●"
else:
self.passentry["show"] = ""
# get input file
def askInputFile(self):
# Read input file
while True:
self.infilename = filedialog.askopenfilename(title='P12-Eingabedatei',
filetypes=(('PKCS12-Dateien', ('*.p12', '*.P12', '*.pfx', '*.PFX')),
('alle Dateien', '*.*')))
if not (self.infilename):
messagebox.showerror('Abbruch', 'Die Dateiauswahl wurde abgebrochen!')
self.master.quit()
# Verify file exists; if so, break
if isfile(self.infilename):
try:
with open(self.infilename, 'rb') as pkcs12file:
self.infile = pkcs12file.read()
except Exception as e:
messagebox.showerror("Ok", e)
continue
return
# Create and hide root window
root = Tk()
decodegui = PasswordEntry(root)
root.mainloop()
...@@ -4,7 +4,51 @@ ...@@ -4,7 +4,51 @@
from OpenSSL import crypto, SSL from OpenSSL import crypto, SSL
from os.path import basename, dirname, isfile, splitext from os.path import basename, dirname, isfile, splitext
from sys import exit from sys import exit
from tkinter import Tk, filedialog, messagebox, simpledialog from tkinter import Button, E, Entry, Label, LEFT, Tk, W, filedialog, messagebox, simpledialog
# Define password query dialog
class _QueryPasswordDialog(simpledialog._QueryDialog):
def body(self, master):
w = Label(master, text=self.prompt, justify=LEFT)
w.grid(row=0, padx=5, sticky=W)
self.entry = Entry(master, name="entry")
self.entry.grid(row=1, padx=5, sticky=W+E)
self.toggle = Button(master, text="⚷", command=self.toggleShow)
self.toggle.grid(row=1, padx=5, column=1, sticky=W+E)
if self.initialvalue is not None:
self.entry.insert(0, self.initialvalue)
self.entry.select_range(0, END)
return self.entry
def toggleShow(self):
if self.entry["show"] == "":
self.entry["show"] = "●"
else:
self.entry["show"] = ""
class _QueryPassword(_QueryPasswordDialog):
def __init__(self, *args, **kw):
if "show" in kw:
self.__show = kw["show"]
del kw["show"]
else:
self.__show = "●"
_QueryPasswordDialog.__init__(self, *args, **kw)
def body(self, master):
entry = _QueryPasswordDialog.body(self, master)
if self.__show is not None:
entry.configure(show=self.__show)
return entry
def askpassword(title, prompt, **kw):
d = _QueryPassword(title, prompt, **kw)
return d.result
# Create and hide root window # Create and hide root window
...@@ -28,7 +72,7 @@ while True: ...@@ -28,7 +72,7 @@ while True:
# Read password # Read password
while True: while True:
password = simpledialog.askstring('Passworteingabe', 'Passwort des geheimen Schlüssels:') password = askpassword('Passworteingabe', 'Passwort des geheimen Schlüssels:')
if password is None: if password is None:
messagebox.showinfo('Abbruch', 'Die Passworteingabe wurde abgebrochen!') messagebox.showinfo('Abbruch', 'Die Passworteingabe wurde abgebrochen!')
exit(0) exit(0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment