2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-01 21:40:32 +00:00
Comrad/komrade/backend/crypt.py

109 lines
2.8 KiB
Python
Raw Normal View History

2020-09-03 21:11:58 +00:00
"""
Storage for both keys and data
"""
from simplekv.fs import FilesystemStore
from simplekv.memory.redisstore import RedisStore
import redis
import hashlib,os
from pythemis.skeygen import KEY_PAIR_TYPE, GenerateKeyPair
from pythemis.smessage import SMessage, ssign, sverify
from pythemis.skeygen import GenerateSymmetricKey
from pythemis.scell import SCellSeal
from pythemis.exception import ThemisError
import zlib
2020-09-04 12:46:22 +00:00
from komrade import KomradeException,Logger
2020-09-03 21:11:58 +00:00
LOG_GET_SET = True
2020-09-03 21:11:58 +00:00
2020-09-04 12:46:22 +00:00
class Crypt(Logger):
2020-09-03 21:11:58 +00:00
def __init__(self,name=None,fn=None,cell=None):
if not name and fn: name=os.path.basename(fn).replace('.','_')
self.name,self.fn,self.cell = name,fn,cell
self.store = FilesystemStore(self.fn)
2020-09-04 12:46:22 +00:00
def log(self,*x):
if LOG_GET_SET:
super().log(*x)
2020-09-03 21:11:58 +00:00
def hash(self,binary_data):
return hashlib.sha256(binary_data).hexdigest()
# return zlib.adler32(binary_data)
def force_binary(self,k_b):
if type(k_b)==str: k_b=k_b.encode()
2020-09-06 14:45:40 +00:00
if type(k_b)!=bytes: k_b=k_b.decode()
2020-09-03 21:11:58 +00:00
return k_b
2020-09-04 00:06:47 +00:00
def package_key(self,k,prefix=''):
2020-09-03 21:11:58 +00:00
k_b = self.force_binary(k)
# k_b = self.cell.encrypt(k_b)
2020-09-04 00:06:47 +00:00
prefix_b = self.force_binary(prefix)
k_b = self.hash(prefix_b + k_b)
2020-09-03 21:11:58 +00:00
return k_b
def package_val(self,k):
k_b = self.force_binary(k)
2020-09-04 00:06:47 +00:00
if self.cell is not None: k_b = self.cell.encrypt(k_b)
2020-09-03 21:11:58 +00:00
return k_b
def unpackage_val(self,k_b):
try:
2020-09-04 00:06:47 +00:00
if self.cell is not None: k_b = self.cell.decrypt(k_b)
2020-09-03 21:11:58 +00:00
except ThemisError:
2020-09-04 00:06:47 +00:00
pass
return k_b
2020-09-03 21:11:58 +00:00
2020-09-04 00:06:47 +00:00
def set(self,k,v,prefix=''):
# self.log('set() k -->',prefix,k)
2020-09-04 00:06:47 +00:00
k_b=self.package_key(k,prefix=prefix)
# self.log('set() k_b -->',k_b)
2020-09-03 21:11:58 +00:00
# self.log('set() v -->',v)
2020-09-03 21:11:58 +00:00
v_b=self.package_val(v)
2020-09-06 14:45:40 +00:00
self.log(f'set(\n\t{prefix}{k},\n\t{k_b}\n\t\n\t{v_b}\n)\n')
2020-09-03 21:11:58 +00:00
return self.store.put(k_b,v_b)
2020-09-05 14:09:31 +00:00
def exists(self,k,prefix=''):
return bool(self.get(k,prefix=prefix))
2020-09-04 00:06:47 +00:00
def get(self,k,prefix=''):
# self.log('get() k -->',prefix,k)
2020-09-04 00:06:47 +00:00
k_b=self.package_key(k,prefix=prefix)
# self.log('get() k_b -->',k_b)
2020-09-03 21:11:58 +00:00
2020-09-04 00:06:47 +00:00
try:
v=self.store.get(k_b)
except KeyError:
return None
# self.log('get() v -->',v)
2020-09-03 21:11:58 +00:00
v_b=self.unpackage_val(v)
self.log('get()',prefix,k,'-->',v_b)
2020-09-03 21:11:58 +00:00
return v_b
class KeyCrypt(Crypt):
def __init__(self):
2020-09-06 09:33:46 +00:00
return super().__init__(name=PATH_CRYPT_CA_KEYS.replace('.','_'))
2020-09-03 21:11:58 +00:00
class DataCrypt(Crypt):
def __init__(self):
2020-09-06 09:33:46 +00:00
return super().__init__(name=PATH_CRYPT_CA_DATA.replace('.','_'))
2020-09-03 21:11:58 +00:00
if __name__=='__main__':
crypt = Crypt('testt')
print(crypt.set('hellothere',b'ryan'))
# print(crypt.get(b'hello there'))