Browse Source

added compression of notebook safefile using gzip module; moved encoding & decoding of ntoebook dafefile into seperate internal methods _decode_notefile() & _encode_notefile()

master
T. Meissner 10 years ago
parent
commit
8fdbf8d53c
1 changed files with 24 additions and 10 deletions
  1. +24
    -10
      python_3_oop/chapter02/menu.py

+ 24
- 10
python_3_oop/chapter02/menu.py View File

@ -3,6 +3,7 @@ import sys
import pickle
import base64
import getpass
import gzip
# own modules
from notebook import Notebook, Note
# cryptography module
@ -93,19 +94,12 @@ Notebook Menu
else:
cipher = f.read()
f.close()
crypt = Fernet(self._get_password())
try:
plain = crypt.decrypt(cipher)
except InvalidToken:
print("Wrong password")
else:
self.notebook = pickle.loads(plain)
self.notebook = self._decode_notefile(cipher)
self.notebook._set_id()
def save_notes(self):
'''Encrypt notebook object and store it into notebook safe file'''
plain = pickle.dumps(self.notebook, pickle.HIGHEST_PROTOCOL)
crypt = Fernet(self._get_password())
cipher = crypt.encrypt(plain)
cipher = self._encode_notefile()
try:
f = open(self.savefile, 'wb')
except IOError:
@ -114,6 +108,26 @@ Notebook Menu
f.write(cipher)
f.close()
def _decode_notefile(self, cipher):
crypt = Fernet(self._get_password())
try:
plain = crypt.decrypt(cipher)
except InvalidToken:
print("Wrong password")
else:
try:
plain = gzip.decompress(plain)
except OSError:
print("File not valid")
else:
return pickle.loads(plain)
def _encode_notefile(self):
plain = pickle.dumps(self.notebook, pickle.HIGHEST_PROTOCOL)
crypt = Fernet(self._get_password())
plain = gzip.compress(plain)
return crypt.encrypt(plain)
def _get_password(self):
'''Request passphrase and derive key from it'''
passphrase = getpass.getpass()


Loading…
Cancel
Save