-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
67 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .pickler import pickleToString, unpickleFromString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|