def openAttachment(self): if len(self.mail_fromlabel.text()) < 1: return self.statusbar.showMessage("Getting attachment...") if self.attachmentButton.text() == "Import PGP": path = n(os.path.join("pgp_keys", self.attachment_name)) with open(path, "w") as f: f.write(self.attachment) f.close() self.statusbar.showMessage("PGP Key imported.", 3000) else: path = n(os.path.join("downloaded", self.attachment_name)) if isinstance(self.attachment, bytes): writeType = "wb" else: writeType = "w" with open(path, writeType) as f: f.write(self.attachment) f.close() if platform.system() == "Windows": os.startfile(path) elif platform.system() == "Darwin": os.popen("open " + path) else: os.popen("xdg-open " + path)
def find_key(self, email_addr, offset): self.refresh_data() for k in self.otp_keys: if email_addr in k: with open(n(os.path.join('otp_keys', k)), ) as f: keydata = json.load(f) return keydata[str(offset)]
def pgp_setup(self, passphrase): self.refresh_data() self.passphrase = passphrase toAdd = {} key = pgpy.PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 4096) uid = pgpy.PGPUID.new(self.name, comment='Pitch Perfect PGP key', email=self.username) key.add_uid(uid, usage={ KeyFlags.Sign, KeyFlags.EncryptCommunications, KeyFlags.EncryptStorage }, hashes=[ HashAlgorithm.SHA256, HashAlgorithm.SHA384, HashAlgorithm.SHA512, HashAlgorithm.SHA224 ], ciphers=[ SymmetricKeyAlgorithm.AES256, SymmetricKeyAlgorithm.AES192, SymmetricKeyAlgorithm.AES128 ], compression=[ CompressionAlgorithm.ZLIB, CompressionAlgorithm.BZ2, CompressionAlgorithm.ZIP, CompressionAlgorithm.Uncompressed ]) subkey = pgpy.PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 4096) key.add_subkey(subkey, usage={KeyFlags.Authentication}) key.protect(self.passphrase, SymmetricKeyAlgorithm.AES256, HashAlgorithm.SHA256) toAdd["PGP"] = {"id": self.username, "passphrase": self.passphrase} self.data.update(toAdd) filename = " ".join(key.userids[0].userid.split()[:-1] + [key.userids[0].userid.split()[-1][1:-1]]) with open(n(os.path.join("pgp_keys", "%s-secret.asc" % (filename))), "w") as f: f.write(str(key)) f.close() with open(n(os.path.join("pgp_keys", "%s-public.asc" % (filename))), "w") as f: pubkey = key.pubkey f.write(str(pubkey)) f.close() with open('config.json', 'w') as conf: json.dump(self.data, conf, indent=4) return "pgp setup success."
def export_key(self): privkey = self.find_secret(self.credentials("username")) with privkey.unlock(self.credentials("passphrase")): pubkey = privkey.pubkey filename = " ".join(privkey.userids[0].userid.split()[:-1] + [privkey.userids[0].userid.split()[-1][1:-1]]) return (str(pubkey), n(os.path.join("pgp_keys", "%s-public.asc" % (filename))))
def hardReset(): pgp_keys = os.listdir("pgp_keys") otp_keys = os.listdir("otp_keys") downloaded = os.listdir("downloaded") if "__pycache__" in os.listdir("."): shutil.rmtree("__pycache__", ignore_errors=True) if "config.json" in os.listdir("."): os.remove("config.json") if "contacts.json" in os.listdir("."): os.remove("contacts.json") if "local.json" in os.listdir("archive"): os.remove(os.path.join("archive", "local.json")) for k in pgp_keys: os.remove(n(os.path.join("pgp_keys", k))) for k in otp_keys: os.remove(n(os.path.join("otp_keys", k))) for k in downloaded: os.remove(n(os.path.join("downloaded", k)))
def __init__(self): self.refresh_data() self.available_keys = os.listdir("pgp_keys") self.otp_keys = os.listdir("otp_keys") for k in self.available_keys: if self.credentials("username") in k and "secret" in k: self.privkey, _ = pgpy.PGPKey.from_file( n(os.path.join('pgp_keys', k)))
class ExternalExe(object): ''' Main class to derive others that will control the execution of external programs. Program configuration is defined in the configSBI.txt file or can be defined in a file linked to a environment variable called SBI_CONFIG_FILE. ''' __metaclass__ = ABCMeta DEFAULT_CONFIG_FILE = j(n(d(__file__)), 'configSBI.txt') # Executable configuration _CONFIG = ConfigParser.RawConfigParser(allow_no_value=True) _EXE = None def __new__(cls, *args, **kwargs): cls._CONFIG.read(os.getenv('SBI_CONFIG_FILE', cls.DEFAULT_CONFIG_FILE)) return super(ExternalExe, cls).__new__(cls, *args, **kwargs) ################### # PRIVATE METHODS # ################### def _set_default_executable(self, external_id): ''' Configure the {Executable} of the class according to the default configuration as defined in the default configSBI.txt or the SBI_CONFIG_FILE @param: external_id @pdef: name of the external program as referred in the configSBI.txt file. @ptype: {String} ''' i, e, p = external_id, 'executable', 'path' self._EXE = Executable(executable=self._CONFIG.get(i, e), path=self._CONFIG.get(i, p)) @staticmethod def _set_dynamic_executable(executable, path): ''' Manual configuration of the {Executable}. @param: executable @pdef: name of the executable file @ptype: {String} @param: path @pdef: path to the executable file @ptype: {String} ''' ExternalExe._EXE = Executable(executable=executable, path=path)
def importOTPKeys(self): self.statusbar.showMessage("Importing keys...") self.d = FileDialog() location = self.d.getOpenFileName(self.d, 'Open File') filename = QFileInfo(location[0]).fileName() try: with open(location[0], "r") as origin: key = origin.read() with open(n(os.path.join("otp_keys", "%s" % (filename))), "w") as f: f.write(key) self.statusbar.showMessage("Key imported successfully.", 3000) except FileNotFoundError: self.statusbar.showMessage("Key not imported.", 3000)
def otp_setup(self): self.refresh_data() otp_keys = {} toAdd = {} otp_keys["signature"] = secrets.token_hex(16) for i in range(0, 2048): otp_keys[i] = secrets.token_hex(512) with open(n(os.path.join("otp_keys", "%s-otp.json" % (self.username))), "w") as conf: json.dump(otp_keys, conf, indent=4) toAdd["OTP"] = {self.username: 0} #Initial mode for the first time run toAdd["INIT"] = 0 self.data.update(toAdd) with open('config.json', 'w') as conf: json.dump(self.data, conf, indent=4) return "otp setup success."
def updateSelf(self): with open(n(os.path.join("lib", "VERSION"))) as v: self_version = v.read() check_version = requests.get( "https://raw.githubusercontent.com/f34rl00/pitch-perfect/master/lib/VERSION" ).text if (float(check_version) > float(self_version)): QMessageBox.about( self, "Security", "A new version is available: Version %s\nPerforming update...\nThe program will close itself." % (check_version.strip())) os.execv(sys.executable, ['python', "updater.py", check_version, self_version]) self.close() else: QMessageBox.about( self, "Security", "You are already up-to-date!\nVersion: %s" % (self_version)) return
def find_public(self, receiver): for k in self.available_keys: if receiver == k.split()[-1][:-11] and "public" in k: print(k) key, _ = pgpy.PGPKey.from_file(n(os.path.join('pgp_keys', k))) return key
def find_secret(self, sender): for k in self.available_keys: if sender in k and "secret" in k: key, _ = pgpy.PGPKey.from_file(n(os.path.join('pgp_keys', k))) return key
def find_public_with_name(self, receiver): for k in self.available_keys: if receiver in k and "public" in k: key, _ = pgpy.PGPKey.from_file(n(os.path.join('pgp_keys', k))) return key, k