@ -3,6 +3,7 @@ import sys
import pickle
import pickle
import base64
import base64
import getpass
import getpass
import gzip
# own modules
# own modules
from notebook import Notebook , Note
from notebook import Notebook , Note
# cryptography module
# cryptography module
@ -93,19 +94,12 @@ Notebook Menu
else :
else :
cipher = f . read ( )
cipher = f . read ( )
f . close ( )
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 ) :
def save_notes ( self ) :
''' Encrypt notebook object and store it into notebook safe file '''
''' 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 :
try :
f = open ( self . savefile , ' wb ' )
f = open ( self . savefile , ' wb ' )
except IOError :
except IOError :
@ -114,6 +108,26 @@ Notebook Menu
f . write ( cipher )
f . write ( cipher )
f . close ( )
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 ) :
def _get_password ( self ) :
''' Request passphrase and derive key from it '''
''' Request passphrase and derive key from it '''
passphrase = getpass . getpass ( )
passphrase = getpass . getpass ( )