def load_private_text(self,filename,pinreader=passphrase_callback): log.info("Trying to load a private certificate.") try: loadsh = serializer.loads(open(filename,'r').read()) log.info("Load as plain text with no passphrase seems OK.") except: if pinreader != None: try: log.info("Load as encrypted text. Requiring passphrase.") passphrase = pinreader(False) if type(passphrase) != str: raise Exception("User refused passphrase request.") key = self.derive_savekey(passphrase) self.private_save_key = key decryptor = ciphers.xipher(key) shcontent = decryptor.decrypt(open(filename,'r').read()) loadsh = serializer.loads(shcontent) except Exception,e: log.exception("Unable to decrypt given file: %s",e) raise Exception("Unable to decrypt given file: %s" % e) else: raise Exception("Unable to load given file.")
def _decryptor(self,key,ciphertext): # print key.encode('hex') if len(key) < 128: key = Hash('sha512',key).digest() + Hash('whirlpool',key).digest() # print "decrypt with: %s" % Hash('md5',key).hexdigest() xi = ciphers.xipher(key) return xi.decrypt(ciphertext)
def _encryptor(self,key,message): # print key.encode('hex') if len(key) < 128: key = Hash('sha512',key).digest() + Hash('whirlpool',key).digest() # print "encrypt with: %s" % Hash('md5',key).hexdigest() xi = ciphers.xipher(key) ctext = xi.encrypt(message) return ctext
def save_private_text(self,filename,pinreader=passphrase_callback): if not self.is_ours: log.exception("Attempt to save a public certificate's private info failed.") raise Exception("Trying to save private info of a public certificate.") if os.path.isfile(filename): os.remove(filename) savesh = {} # save info savesh['Title'] = 'Xi_Certificate_Private' savesh['Basic'] = self.get_baseinfo() savesh['Signatures'] = [] # save self.keys keyindex = 1 for k in self.keys: keydata = k.get_privatekey(raw=True) savesh['Basic']['Public_Key_Ring'][str(keyindex)] = keydata keyindex += 1 # save signatures if self.signatures: for sig in self.signatures: savesh['Signatures'].append(sig) # final # open(filename,'w+').write(savesh) shcontent = serializer.dumps(savesh) if pinreader != None: if self.private_save_key == None: passphrase = pinreader(True) key = self.derive_savekey(passphrase) self.private_save_key = key else: key = self.private_save_key #print key.encode('base64') encryptor = ciphers.xipher(key) shcontent = encryptor.encrypt(shcontent) open(filename,'w').write(shcontent) log.info("Successfully saved private info.")