diff --git a/impra/cli.py b/impra/cli.py index 107dea3..1cf0dff 100644 --- a/impra/cli.py +++ b/impra/cli.py @@ -49,7 +49,7 @@ transfert process is transparent. Just vizualize locally the index of stored files and simply select files to download or upload. ImpraStorage automatically launch the parts to download, then merge parts in the appropriate way to rebuild the original file. Inversely, a file -to upload is splitt -in several parts with addition of noise data), and +to upload is split (in several parts with addition of noise data), and ImpraStorage randomly upload each parts then update the index. """ @@ -250,9 +250,12 @@ you can remove index but all presents files on the box %s will be unrecoverable if uid == None : uid = 'EMPTY' if date == None : date = '' if impst.index != None: - impst.index.print(True,'-'*120+'\n -- INDEX(`'+uid+'`) boxname :`'+impst.rootBox+'` '+date+'\n'+'-'*120) + impst.index.print('-'*120+'\n -- INDEX(`'+uid+'`) boxname :`'+impst.rootBox+'` '+date+'\n'+'-'*120) #encData = impst.index.impraEncrypt(impst.index.toString()) - #~ dd = """coucou mon joli coeur :*:* je s""" + #~ dd = """coucou mon joli coeur :*:* je sais que je te saoule avec ça mais bon putain tu va te planter ou merde""" + #~ dd = """01234567890123456789012345678901234567890123456789#""" + #~ + #~ #~ kg = crypt.KeyGen(256) #~ print('-- key --') #~ print(kg.key) @@ -260,7 +263,7 @@ you can remove index but all presents files on the box %s will be unrecoverable #~ print(kg.mark) #~ km = crypt.Kirmah(kg.key, kg.mark) #~ encData = km.encrypt(dd,'.index',22) - #~ print('*'+encData+'*') + #~ #print('*'+encData+'*') #~ decData = km.decrypt(encData,'.index',22) #~ print('*'+decData+'*') @@ -277,8 +280,11 @@ you can remove index but all presents files on the box %s will be unrecoverable else: print(o.get_by_id+' a is not valid id') elif o.search : - print(o.search) - + label = impst.index.searchByPattern(o.search) + if label==None: + print(' -- no match found for pattern `%s` --' % o.search) + else: + impst.index.print('-'*120+'\n -- SEARCH: `'+o.search+'` -- found '+str(len(label))+' results --\n'+'-'*120,label) elif o.remove : print(o.remove) diff --git a/impra/core.py b/impra/core.py index 814b4f0..a145247 100644 --- a/impra/core.py +++ b/impra/core.py @@ -37,16 +37,17 @@ from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import formatdate +from json import dump as jdump, load as jload, dumps as jdumps, loads as jloads from math import ceil, floor from mmap import mmap from os import remove, urandom, sep from os.path import abspath, dirname, join, realpath, basename, getsize, splitext -from re import split as regsplit +from re import split as regsplit, match as regmatch, compile as regcompile, search as regsearch from impra.imap import ImapHelper, ImapConfig -from impra.util import __CALLER__, RuTime, formatBytes, randomFrom, bstr, quote_escape, stack, run, file_exists, get_file_content +from impra.util import __CALLER__, RuTime, formatBytes, randomFrom, bstr, quote_escape, stack, run, file_exists, get_file_content, DEBUG, DEBUG_ALL, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN from impra.crypt import Kirmah, ConfigKey, Noiser, Randomiz, hash_sha256 -DEBUG = True + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -59,10 +60,9 @@ class FSplitter : """""" self.ck = ck self.wkdir = wkdir - self.DIR_CACHE = self.wkdir+sep+'cache'+sep - self.DIR_INBOX = self.wkdir+sep+'inbox'+sep - self.DIR_OUTBOX = self.wkdir+sep+'outbox'+sep - self.DIR_DEPLOY = self.wkdir+sep+'deploy'+sep + self.DIR_INBOX = join(self.wkdir,'inbox')+sep + self.DIR_OUTBOX = join(self.wkdir,'outbox')+sep + self.DIR_DEPLOY = join(self.wkdir,'deploy')+sep def addFile(self, fromPath, label, fixCount = False): """""" @@ -70,8 +70,8 @@ class FSplitter : fsize = getsize(fromPath) count = ceil(fsize/self.ck.psize) minp, maxp = 52, 62 - if fsize < 4800000 : minp, maxp = 8, 12 - elif fsize < 22200000 : minp, maxp = 12, 22 + if fsize < 4800000 : minp, maxp = 8, 16 + elif fsize < 22200000 : minp, maxp = 16, 22 elif fsize < 48000000 : minp, maxp = 22, 32 elif fsize < 222000000 : minp, maxp = 32, 42 if not fixCount : @@ -91,7 +91,7 @@ class FSplitter : m = mmap(f.fileno(), 0) p = 0 psize = ceil(getsize(fromPath)/hlst['head'][1]) - print(str(psize)) + print(formatBytes(psize)+' '+str(len(hlst['data']))+' parts') while m.tell() < m.size(): self._splitPart(m,p,psize,hlst['data'][p]) p += 1 @@ -217,30 +217,16 @@ class ImpraIndex: `mark` : str appropriate mark to check correct key `encdata` : str - initial content of the index encrypted with rsa + initial content of the index encrypted with Kirmah Algorythm + and representing a dic index as json string """ self.km = Kirmah(key, mark) self.dic = {} self.id = id - if encdata =='' : data = encdata - else : data = self.decrypt(encdata) - data = data.replace(self.QUOTE_REPL, '\'') - ld = regsplit('\n?\r? ?'+self.SEP_CATEGORY+' ?\n\r??',data) - l = regsplit(self.SEP_ITEM,ld[0]) - for row in l: - d = regsplit(self.SEP_TOKEN,row) - del d[7:] - # key : count, hash, ext, usr, cat - if len(d)>5 and d!='': - self.dic[d[1]] = d - if len(ld)>1: - l = regsplit(self.SEP_ITEM,ld[1].lstrip('\n\r')) - for row in l: - d = regsplit(' ?= ?',row,1) - if len(d)> 1 and len(d[0]) > 3 : - self.dic[d[0].lstrip('\n\r')] = d[1] - else: - for k in dicCategory : + if encdata =='' : self.dic = {} + else : self.dic = self.decrypt(encdata) + for k in dicCategory : + if not self.SEP_KEY_INTERN+k in self.dic: self.dic[self.SEP_KEY_INTERN+k] = dicCategory[k] def add(self,key, label, count, ext='', usr='', cat=''): @@ -253,6 +239,22 @@ class ImpraIndex: else : print(label+' already exist') + def addUser(self, nameFrom, hashName): + """""" + if not self.SEP_KEY_INTERN+'users' in self.dic: + self.dic[self.SEP_KEY_INTERN+'users'] = {} + if not hashName in self.dic[self.SEP_KEY_INTERN+'users']: + self.dic[self.SEP_KEY_INTERN+'users'][hashName] = nameFrom + + def getUser(self, hashName): + """""" + usrName = 'Anonymous' + if not str(self.SEP_KEY_INTERN+'users') in self.dic: + self.dic[self.SEP_KEY_INTERN+'users'] = {} + elif hashName in self.dic[self.SEP_KEY_INTERN+'users']: + usrName = self.dic[self.SEP_KEY_INTERN+'users'][hashName] + return usrName + def rem(self,label): """Remove the selected label from the index""" self.dic.pop(label, None) @@ -263,50 +265,63 @@ class ImpraIndex: def searchById(self,sid): """Search the corresponding label in the index""" - rt = RuTime(eval(__CALLER__())) + rt = RuTime(eval(__CALLER__(sid))) l = None - r = [v for i, v in enumerate(self.dic) if self.dic[v][6] == str(sid)] - if len(r)>0: l = r[0] + r = [v for i, v in enumerate(self.dic) if not v.startswith(self.SEP_KEY_INTERN) and self.dic[v][6] == int(sid)] + if len(r)==1: l = r[0] + rt.stop() + return l + + def searchByPattern(self,pattern): + """""" + rt = RuTime(eval(__CALLER__(pattern))) + l = None + r = [ v for i,v in enumerate(self.dic) if not v.startswith(self.SEP_KEY_INTERN) and regsearch(pattern,self.dic[v][1]) is not None ] + l = [self.dic[k][6] for k in r] rt.stop() return l - def toString(self, withoutCatg=False, idFirst=False): + def toString(self,matchIds): """Make a string representation of the index as it was store on the server""" - data = cdata = '' - for k in sorted(self.dic): + data = '' + r = [k for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)] + for k in r : v = self.dic.get(k) k = k.lstrip('\n\r') - if k[0]==self.SEP_KEY_INTERN and len(k)>1: - cdata += k+'='+v+self.SEP_ITEM - else : - if not idFirst : - for i in v: - data += str(i)+self.SEP_TOKEN - else : + if not k[0]==self.SEP_KEY_INTERN and len(k)>1: + if matchIds==None or v[6] in matchIds: data += str(v[6]).rjust(1+ceil(len(str(v[6]))/10),' ')+' ' - data += str(v[0])[0:42]+'... ' - data += str(v[1]).ljust(30,' ')+' ' - for i in v[2:-1]: - data += str(i)+' ' - data = data.rstrip(self.SEP_TOKEN)+self.SEP_ITEM - if not withoutCatg : - data += self.SEP_CATEGORY+'\n'+cdata + data += str(v[0])[0:12]+'... ' + data += str(v[1]).ljust(42,' ')+' ' + data += str(v[2]).rjust(2,'0')+' ' + data += str(v[3]).ljust(5,' ')+' ' + data += self.getUser(str(v[4])).ljust(15,' ')+' ' + data += str(v[5])+' ' + #~ elif len(k)>1: + #~ print(k,'=',v) + data = data+self.SEP_ITEM return data; def encrypt(self): """""" - return self.km.encrypt(self.toString().replace('\'', self.QUOTE_REPL),'.index',22) + #~ print('encrypting index :') + jdata = jdumps(self.dic) + #~ print(jdata) + return self.km.encrypt(jdata,'.index',22) def decrypt(self,data): """""" - if data!='': data = self.km.decrypt(data,'.index',22) + #~ print('decrypting index : ') + jdata = self.km.decrypt(data,'.index',22) + #~ print(jdata) + data = jloads(jdata) return data - def print(self,withoutCatg=False, header=''): + def print(self,header='', matchIds=None): """Print index content as formated bloc""" - data = self.toString(withoutCatg,True).split(';') + data = self.toString(matchIds).split(self.SEP_ITEM) print(header) - print('id'+' '*2+'hash'+' '*43+'label'+' '*26+'part'+' '*2+'type'+' '*3+'from'+' '*11+'category') + print('id'+' '*2+'hash'+' '*13+'label'+' '*40+'part'+' '*2+'type'+' '*2+'owner'+' '*12+'category') print('-'*120) for row in data: if row.rstrip('\n') != '': print(row) @@ -366,25 +381,27 @@ class ImpraStorage: def getIndex(self): """""" - rt = RuTime(eval(__CALLER__())) + from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_WARN, DEBUG_INFO + rt = RuTime(eval(__CALLER__()),DEBUG_INFO) index = None encData = '' uid = self.conf.get('uid' ,'index') date = self.conf.get('date','index') nid = self.conf.get('nid' ,'index') if nid==None : nid = 0 - if uid !=None : print(uid+' - '+date+' - ['+(str(nid))+']') self._getIdIndex() if self.idx : # getFromFile if uid != None and int(self.idx) == int(uid) and file_exists(self.pathInd): encData = get_file_content(self.pathInd) - print('cache') + print(' index in cache') else: encData = self._getCryptIndex() with open(self.pathInd, mode='w', encoding='utf-8') as o: o.write(encData) - index = ImpraIndex(self.conf.get('key','keys'),self.conf.get('mark','keys'), encData, {'catg':self.conf.get('types','catg')}, int(nid)) + usrName = self.conf.get('name','infos') + usrHash = self.mb.getHashName(usrName) + index = ImpraIndex(self.conf.get('key','keys'),self.conf.get('mark','keys'), encData, {'catg':self.conf.get('types','catg'), 'users':{ ('%s' % self.mb.getHashName('all')) : 'all', ('%s' % usrHash) : usrName}}, int(nid)) rt.stop() return index @@ -397,14 +414,14 @@ class ImpraStorage: def saveIndex(self): """""" - global DEBUG - rt = RuTime(eval(__CALLER__())) + from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO + rt = RuTime(eval(__CALLER__()),DEBUG_INFO) if self.idx != None : self.ih.delete(self.idx, True) for i in self.delids : self.ih.delete(i, True) encData = self.index.encrypt() msgIndex = self.mb.buildIndex(encData) - if DEBUG: print(msgIndex.as_string()) + if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : print(msgIndex.as_string()) ids = self.ih.send(msgIndex.as_string(), self.rootBox) date = self.ih.headerField('date', ids[1], True) self.conf.set('uid',ids[1],'index') @@ -417,8 +434,8 @@ class ImpraStorage: def addFile(self, path, label, usr='all', catg=''): """""" - global DEBUG - rt = RuTime(eval(__CALLER__('"%s","%s","%s"' % (path[:13]+'...',label,usr)))) + from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO + rt = RuTime(eval(__CALLER__('"%s","%s","%s"' % (path[:13]+'...',label,usr))),DEBUG_INFO) #~ hlst = self.fsplit.addFile(path,label) #~ self.fsplit.deployFile(hlst,True) @@ -426,11 +443,13 @@ class ImpraStorage: try: if self.index.search(label)==None : hlst = self.fsplit.addFile(path,label) - if DEBUG : + if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : print(hlst['head']) for v in hlst['data']: print(v) - nameFrom = self.conf.ini.get('name',self.conf.profile+'.infos') + nameFrom = self.conf.get('name','infos') + self.index.addUser(nameFrom, self.mb.getHashName(nameFrom)) + for row in hlst['data'] : msg = self.mb.build(nameFrom,usr,hlst['head'][2],self.fsplit.DIR_OUTBOX+row[1]+'.ipr') self.ih.send(msg.as_string(), self.rootBox) @@ -446,8 +465,9 @@ class ImpraStorage: def getFile(self,label): """""" - global DEBUG - rt = RuTime(eval(__CALLER__('"%s"' % label))) + from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO + + rt = RuTime(eval(__CALLER__('"%s"' % label)),DEBUG_INFO) if label==None : print(str(label)+' unexist') else : @@ -463,7 +483,7 @@ class ImpraStorage: if to == self.mb.getHashName('all')+'@'+self.mb.DOMAIN_NAME or to == self.mb.getHashName(self.conf.ini.get('name',self.conf.profile+'.infos'))+'@'+self.mb.DOMAIN_NAME : for mid in ids : self.ih.downloadAttachment(mid,self.fsplit.DIR_INBOX) - if DEBUG : + if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : print(hlst['head']) for v in hlst['data']: print(v) diff --git a/impra/crypt.py b/impra/crypt.py index a5f8eb9..e28364d 100644 --- a/impra/crypt.py +++ b/impra/crypt.py @@ -26,7 +26,7 @@ # You should have received a copy of the GNU General Public License # along with ImpraStorage. If not, see . -from impra.util import RuTime, __CALLER__, stack +from impra.util import RuTime, __CALLER__, stack, DEBUG from base64 import urlsafe_b64encode, b64decode from binascii import b2a_base64, a2b_base64 from hashlib import sha256 @@ -219,19 +219,21 @@ class Kirmah: dataEnc = '' psize = ceil(len(data)/cpart) cp = 0 - for row in hlst['data']: + #~ print(row) + #~ print('ns:%i - dataLength:%i - dataEncLength:%i - ne:%i' % (row[2],len(data), len(dataEnc), row[3])) + #~ print(data[cp*psize:cp*psize+psize]) dataEnc += self.ck.noiser.getNoise(row[2],True)+data[cp*psize:cp*psize+psize]+self.ck.noiser.getNoise(row[3],True) cp += 1 dataEnc = str(b2a_base64(bytes(dataEnc,'utf-8')),'utf-8') - rt.stop() + #~ dataEnc = self.subenc(odata) with open('./.KirmahENC', mode='w') as o: o.write(dataEnc) - + rt.stop() return dataEnc - + def decrypt(self, data, label, cpart): """""" rt = RuTime(eval(__CALLER__())) @@ -244,24 +246,31 @@ class Kirmah: rsz += row[2]+row[3] psize = ceil((len(data)-rsz)/cpart) - for row in hlst['data']: si = ni + row[2] ei = si + psize + #~ print(row) + #~ print('si:%i - ei:%i - datalength:%i - dataDeclength:%i' % (si,ei,len(data),len(dataDec))) if cp == cpart-1 : - ei = -row[3] - if si > len(data)+ei : + ei = -row[3] + if not si > len(data)+ei : pass + ### to delete #si = len(data)+ei + #~ print('si:%i - ei:%i' % (si,ei)) + else : + #~ print('si: - ei:%i' % (len(data)+ei-si)) dataDec=dataDec[:len(data)+ei-si] break - si = len(data)+ei + #~ print(data[si:ei]) dataDec += data[si:ei] ni = ei + row[3] cp += 1 dataDec = self.subdec(dataDec) - with open('./.KirmahDEC', mode='w') as o: - o.write(dataDec) - rt.stop() + #~ dataDec = self.subdec(data) + + with open('./.KirmahDEC', mode='w') as o: + o.write(dataDec) + rt.stop() return dataDec # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/impra/imap.py b/impra/imap.py index 042f88c..a225f03 100755 --- a/impra/imap.py +++ b/impra/imap.py @@ -298,20 +298,23 @@ class ImapHelper: def deleteBin(self): """""" + from impra.util import DEBUG_NOTICE, DEBUG, DEBUG_LEVEL rt = RuTime(eval(__CALLER__())) self.srv.select(self.BOX_BIN) ids = self.search('ALL',True) if len(ids) > 0 and ids[0]!='' and ids[0]!=None: #print(str(ids[0],'utf-8').split()) - for mid in ids: - print('deleting msg '+str(mid)) + for mid in ids: #~ uid = bytes(mid) #~ print(type(mid)) #~ print(mid) #status, resp = self.srv.store(mid, '+FLAGS', '\\Deleted') status, resp = self.srv.uid('store', mid, '+FLAGS', '\\Deleted' ) - print(status) - print(resp) + + if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE: + print('deleting msg '+str(mid)) + print(status) + print(resp) self.srv.expunge() self.srv.select(self.rootBox) rt.stop() @@ -331,12 +334,13 @@ class ImapHelper: def downloadAttachment(self, msg, toDir='./'): """""" - rt = RuTime(eval(__CALLER__('%i' % int(msg)))) + from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_INFO + rt = RuTime(eval(__CALLER__('%i' % int(msg))),DEBUG_INFO) if not isinstance(msg, Message) : msg = self.email(msg) for part in msg.walk(): filename = part.get_filename() - if filename != None : print(filename) + if filename != None and DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : print(filename) if part.get_content_maintype() == 'multipart' or not filename : continue fp = open(join(toDir, filename), 'wb') #print(part.get_payload(decode=True)[::-1]) @@ -346,7 +350,8 @@ class ImapHelper: def send(self, msg, box='INBOX'): """""" - rt = RuTime(eval(__CALLER__())) + from impra.util import DEBUG_INFO + rt = RuTime(eval(__CALLER__()),DEBUG_INFO) mid = None date = Time2Internaldate(time()) status, resp = self.srv.append(box, '\Draft', date, bytes(msg,'utf-8')) diff --git a/impra/util.py b/impra/util.py index b075c7a..a7e31f6 100755 --- a/impra/util.py +++ b/impra/util.py @@ -41,7 +41,13 @@ from inspect import stack from subprocess import PIPE, Popen from sys import stderr, executable as pyexec +DEBUG_ALL = 0 +DEBUG_WARN = 1 +DEBUG_NOTICE = 2 +DEBUG_INFO = 3 +DEBUG = True +DEBUG_LEVEL = DEBUG_INFO # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~ methods ~~ @@ -149,93 +155,43 @@ def __CALLER__(args=''): eval(__CALLER('"%s","%s"' % (arg1,arg2))) :Returns: `str` - """ + """ + global DEBUG_LEVEL, DEBUG, DEBUG_WARN #~ print(inspect.stack()[1][3]) #~ print(print(args)) #~ print('-----') #~ print(inspect.stack()) #~ print('---------------') - val = "self.__class__.__name__+'.%s' % stack()[1][3]+'("+quote_escape(args)+") l:'+str(stack()[1][2])" + val = "self.__class__.__name__+'.%s' % stack()[1][3]+'("+quote_escape(args)+") " + if DEBUG and DEBUG_LEVEL<=DEBUG_WARN : val += "l:'+str(stack()[1][2])" + else: val += "'" return val -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# ~~ class Noiser ~~ - -class Noiser: - """""" - - KEY_LEN = 256 - """""" - - def __init__(self, key, part=0): - """""" - - #~ if len(key)!=self.KEY_LEN : - #~ raise Exception('Invalid Pass length') - #~ else : - self.key = key - self.build(part) - - def build(self, part): - """""" - if not part < self.KEY_LEN-1 : raise Exception('part exceed limit') - else : - self.part, v = part, 0 - v = int(ceil((self.key[22]+v)/4.20583)) - self.lns = int(ceil(v/2))-self.key[self.part]+self.key[7] - self.lne = int(v-self.lns-self.key[self.part+2]-self.key[44]/2.1934) - - def getNoise(self, l): - """""" - return urandom(l) - - -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# ~~ class Randomiz ~~ - -class Randomiz: - """""" - - def __init__(self,count,chl=None): - """""" - if chl ==None : self.lst = list(range(0,count)) - else: self.lst = chl - self.count = len(self.lst) - - def new(self,count=None, chl=None): - """""" - if count : self.count = count - self.__init__(self.count,chl) - - def get(self,single=True): - """""" - pos = choice(self.lst) - if single: del self.lst[self.lst.index(pos)] - return pos - - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~ class RuTime ~~ class RuTime: """Give basics time stats""" - def __init__(self,label): + def __init__(self,label,lvl=DEBUG_NOTICE): """Initialize duration with appropriate label""" - self.label = label + from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_INFO + self.debug = DEBUG and DEBUG_LEVEL <= lvl + self.debugStart = self.debug and lvl < DEBUG_INFO + self.lvl = lvl + self.label = label self._start() def _start(self): - from impra.core import DEBUG - if DEBUG :print(' ==> '+self.label) + + if self.debugStart :print(' ==> '+self.label) self.sc = time() def stop(self): """Stop duration and print basics stats duration on console""" - from impra.core import DEBUG self.ec = time() - if DEBUG:self._stats() + if self.debug: self._stats() def _stats(self): print(' <== '+self.label+(' [%.9f s]' % (self.ec - self.sc))) @@ -340,68 +296,6 @@ class IniFile: class BadKeysException(BaseException): pass -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# ~~ class Rsa ~~ - -class Rsa: - """""" - - def __init__(self, prvKey=None, pubKey=None, dpath='./', forceKeyGen=False): - """""" - self.cpath = join(dirname(dirname(realpath(__file__))),'desurveil','scripts')+sep - self.prvKey = prvKey - self.pubKey = pubKey - self.dpath = realpath(dpath)+sep - if prvKey == None or pubKey==None : self.key(forceKeyGen) - - def key(self,force=False): - """""" - cmd = self.cpath+'desurveil key -a '+self.dpath+'.impra_id_rsa -l '+self.dpath+'.impra_id_rsa.pub' - try : - with open(self.dpath+'.impra_id_rsa','rt') as f: pass - if force:d = popen(cmd).read() - except IOError as e: - d = popen(pyexec+' '+cmd).read() - #print(pyexec+' '+cmd) - self.prvKey = get_file_content(self.dpath+'.impra_id_rsa') - self.pubKey = get_file_content(self.dpath+'.impra_id_rsa.pub') - #~ print('pubKey : \n'+self.pubKey) - #~ print('prvKey : \n'+self.prvKey) - - def encrypt(self,data): - """""" - key = '' - #if self.pubKey != None : key = ' -CI "'+self.pubKey+'"' - if self.pubKey != None : key = ' -C "'+self.dpath+'.impra_id_rsa.pub"' - with open(self.dpath+'.tmpdecd', mode='w', encoding='utf-8') as o: - o.write(data) - cmd = self.cpath+'desurveil encrypt "'+self.dpath+'.tmpdecd'+'" '+key - #print(pyexec+' '+cmd) - rs = run(pyexec+' '+cmd) - if rs[0]==1: - print(rs) - raise BadKeysException('bad key to encrypt') - else : - encData = str(rs[1],'utf-8') - return encData - - def decrypt(self,data): - """""" - key = '' - #if self.prvKey != None : key = ' -CI "'+self.prvKey+'"' - if self.prvKey != None : key = ' -C "'+self.dpath+'.impra_id_rsa"' - with open(self.dpath+'.tmpencd', mode='w', encoding='utf-8') as o: - o.write(data) - cmd = self.cpath+'desurveil decrypt "'+self.dpath+'.tmpencd'+'" '+key - #print(pyexec+' '+cmd) - rs = run(pyexec+' '+cmd) - if rs[0]==1: - print(rs) - raise BadKeysException('bad key to decrypt') - else : - decData = str(rs[1],'utf-8') - return decData - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~ class StrIterator ~~ diff --git a/imprastorage.py b/imprastorage.py index 04aee75..e6a5ce6 100755 --- a/imprastorage.py +++ b/imprastorage.py @@ -27,44 +27,12 @@ # along with ImpraStorage. If not, see . from impra.core import ImpraConf, ImpraStorage -from impra.util import IniFile, Rsa, RuTime, get_file_path +from impra.util import IniFile, RuTime, get_file_path from impra.cli import Cli - if __name__ == '__main__': Cli(get_file_path(__file__ )) - #~ rt = RuTime(__name__+'()') - #~ conf = ImpraConf(IniFile('./impra.ini')) - #~ rsa = Rsa(conf.ini.get('prvKey',conf.profile+'.keys'),conf.ini.get('pubKey',conf.profile+'.keys')) - #~ impst = ImpraStorage(rsa, conf) -#~ - #~ print('\n -- INDEX DATA -- ') - #~ impst.index.print() - #~ print('-- LIST BOX --') - #~ lb = impst.ih.listBox('/') - #~ print(lb) -#~ - #~ #print('-- DELETE BIN --') - #~ #impst.ih.deleteBin() -#~ - #~ filePath = '/media/Data/dev/big_toph3.jpg' -#~ - #~ lab = 'Meuf\'bonne aussi4' -#~ - #~ print('\n -- ADD FILE -- ') - #~ impst.addFile(filePath,lab,conf.ini.get('name',conf.profile+'.infos'),'images') -#~ - #~ print('\n -- GET FILE -- ') - #~ impst.getFile(lab) -#~ - #~ print('\n -- INDEX DATA -- ') - #~ impst.index.print() - #~ - #~ print('\n -- CLEAN -- ') - #~ impst.clean() -#~ - #~ rt.stop() #python -O -m compileall impra/*.py