Msgpack to pack any objects in db

pull/24/head^2
Michael Egorov 2017-09-05 16:53:18 -07:00
parent ddd7a6d849
commit 7f03612611
3 changed files with 8 additions and 7 deletions
nkms

View File

@ -1,5 +1,6 @@
import appdirs import appdirs
import lmdb import lmdb
import msgpack
import os.path import os.path
CONFIG_APPNAME = 'nucypher-kms' CONFIG_APPNAME = 'nucypher-kms'
@ -19,7 +20,7 @@ class DB(object):
def __setitem__(self, key, value): def __setitem__(self, key, value):
with self.db.begin(write=True) as tx: with self.db.begin(write=True) as tx:
tx.put(key, value) tx.put(key, msgpack.dumps(value))
def __getitem__(self, key): def __getitem__(self, key):
with self.db.begin(write=False) as tx: with self.db.begin(write=False) as tx:
@ -27,7 +28,7 @@ class DB(object):
if result is None: if result is None:
raise KeyError(key) raise KeyError(key)
else: else:
return result return msgpack.loads(result)
def __delitem__(self, key): def __delitem__(self, key):
with self.db.begin(write=True) as tx: with self.db.begin(write=True) as tx:

View File

@ -39,7 +39,7 @@ class Client(object):
'm-of-n reencryption not yet available') 'm-of-n reencryption not yet available')
rekeys = rekeys[0] rekeys = rekeys[0]
# Should specify and check signature also # Should specify and check signature also
_storage[k] = {'rk': rekeys, 'algorithm': algorithm} _storage[k] = {b'rk': rekeys, b'algorithm': algorithm}
def remove_rekeys(self, pub, k): def remove_rekeys(self, pub, k):
# Should specify and check signature also # Should specify and check signature also
@ -51,8 +51,8 @@ class Client(object):
:param bytes k: Address of the rekey derived from the path/pubkey :param bytes k: Address of the rekey derived from the path/pubkey
:param bytes ekey: Encrypted symmetric key to reencrypt :param bytes ekey: Encrypted symmetric key to reencrypt
""" """
rekey = _storage[k]['rk'] rekey = _storage[k][b'rk']
algorithm = _storage[k]['algorithm'] algorithm = _storage[k][b'algorithm']
pre = crypto.pre_from_algorithm(algorithm) pre = crypto.pre_from_algorithm(algorithm)
return pre.reencrypt(rekey, ekey) return pre.reencrypt(rekey, ekey)

View File

@ -25,6 +25,6 @@ def test_db():
def test_store_dict(): def test_store_dict():
db = DB() db = DB()
db[b'x'] = {'a': 1, 'b': 2} db[b'x'] = {b'a': 1, b'b': 2}
assert db[b'x']['a'] == 1 assert db[b'x'][b'a'] == 1
db.close() db.close()