Skip to content

Commit

Permalink
Preliminary encryption support
Browse files Browse the repository at this point in the history
  • Loading branch information
langdal committed Apr 20, 2021
1 parent da3586b commit 88df182
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
11 changes: 2 additions & 9 deletions optimizerapi/optimizer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import codecs
import pickle
import json
from json_tricks import dumps
from ProcessOptimizer import Optimizer, expected_minimum
Expand All @@ -10,6 +8,8 @@
import base64
import io
from numbers import Number
from .securepickle import pickleToString, unpickleFromString

import numpy
numpy.random.seed(42)
"""ProcessOptimizer web request handler
Expand All @@ -18,13 +18,6 @@
The handler functions are mapped to the OpenAPI specification through the "operationId" field
in the specification.yml file found in the folder "openapi" in the root of this project.
"""
def pickleToString(obj):
pickled = codecs.encode(pickle.dumps(obj), "base64").decode()
return pickled

def unpickleFromString(pickled):
unpickled = pickle.loads(codecs.decode(pickled.encode(), "base64"))
return unpickled

def run(body) -> dict:
"""Executes the ProcessOptimizer
Expand Down
1 change: 1 addition & 0 deletions optimizerapi/securepickle/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .pickler import pickleToString, unpickleFromString
17 changes: 17 additions & 0 deletions optimizerapi/securepickle/pickler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import codecs
import pickle
from .secure import create_key, load_key
from cryptography.fernet import Fernet

create_key()
key = load_key()
f = Fernet(key)

def pickleToString(obj):
pickled = codecs.encode(f.encrypt(pickle.dumps(obj)), "base64").decode()
return pickled

def unpickleFromString(pickled):
unpickled = pickle.loads(f.decrypt(codecs.decode(pickled.encode(), "base64")))
return unpickled

14 changes: 14 additions & 0 deletions optimizerapi/securepickle/secure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from cryptography.fernet import Fernet

def is_initialized():
pass

def load_key():
with open('mykey.key', 'rb') as mykey:
key = mykey.read()
return key

def create_key():
key = Fernet.generate_key()
with open('mykey.key', 'wb') as mykey:
mykey.write(key)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ connexion==2.7.0
connexion[swagger-ui]
Flask==1.1.2
ProcessOptimizer==0.6.1
json-tricks==3.15.5
json-tricks==3.15.5
cryptography==3.4.7
31 changes: 31 additions & 0 deletions tests/test_securepickle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from optimizerapi.securepickle import *
import pickle

def test_pickleToString():
encoded = pickleToString("myString")
assert encoded != "myString"

def test_unpickleFromString():
encoded = pickleToString("myString")
decoded = unpickleFromString(encoded)
assert decoded == "myString"

# def test_load_key():
# create_key()
# key = load_key()
# assert key != ""
# assert len(key) == 44

# def test_encrypt():
# create_key()
# key = load_key()
# f = Fernet(key)
# encoded = pickle.dumps("myString")
# encrypted = f.encrypt(encoded)
# assert encoded != encrypted
# assert type(encrypted) == str
# decrypted = f.decrypt(encrypted)
# assert encoded == decrypted



0 comments on commit 88df182

Please sign in to comment.