mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Encryption / decryption
This commit is contained in:
parent
c6b86991c4
commit
9c18fdc9b4
1 changed files with 24 additions and 6 deletions
30
jrnl.py
30
jrnl.py
|
@ -12,12 +12,14 @@ import time
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
import readline, glob
|
import readline, glob
|
||||||
|
from Crypto.Cipher import AES
|
||||||
|
import getpass
|
||||||
|
|
||||||
default_config = {
|
default_config = {
|
||||||
'journal': os.path.expanduser("~/journal.txt"),
|
'journal': os.path.expanduser("~/journal.txt"),
|
||||||
'editor': "",
|
'editor': "",
|
||||||
'encrypt': True,
|
'encrypt': True,
|
||||||
|
'key': "",
|
||||||
'default_hour': 9,
|
'default_hour': 9,
|
||||||
'default_minute': 0,
|
'default_minute': 0,
|
||||||
'timeformat': "%Y-%m-%d %H:%M",
|
'timeformat': "%Y-%m-%d %H:%M",
|
||||||
|
@ -74,6 +76,10 @@ class Journal:
|
||||||
self.entries = self.open()
|
self.entries = self.open()
|
||||||
self.sort()
|
self.sort()
|
||||||
|
|
||||||
|
def _block_tail(self, s, b=16):
|
||||||
|
"""Appends spaces to a string until length is a multiple of b"""
|
||||||
|
return s + " "*(b - len(s) % b)
|
||||||
|
|
||||||
def open(self, filename=None):
|
def open(self, filename=None):
|
||||||
"""Opens the journal file defined in the config and parses it into a list of Entries.
|
"""Opens the journal file defined in the config and parses it into a list of Entries.
|
||||||
Entries have the form (date, title, body)."""
|
Entries have the form (date, title, body)."""
|
||||||
|
@ -88,7 +94,15 @@ class Journal:
|
||||||
current_entry = None
|
current_entry = None
|
||||||
|
|
||||||
with open(filename) as f:
|
with open(filename) as f:
|
||||||
journal_plain = f.read()
|
if config['encrypt']:
|
||||||
|
journal_encrypted = f.read()
|
||||||
|
key = config['key'] or getpass.getpass()
|
||||||
|
key = self._block_tail(key)
|
||||||
|
self.crypto = AES.new(key, AES.MODE_ECB)
|
||||||
|
journal_plain = self.crypto.decrypt(journal_encrypted)
|
||||||
|
else:
|
||||||
|
journal_plain = f.read()
|
||||||
|
|
||||||
for line in journal_plain.split(os.linesep):
|
for line in journal_plain.split(os.linesep):
|
||||||
if line:
|
if line:
|
||||||
try:
|
try:
|
||||||
|
@ -126,7 +140,11 @@ class Journal:
|
||||||
filename = filename or self.config['journal']
|
filename = filename or self.config['journal']
|
||||||
journal_plain = os.linesep.join([str(e) for e in self.entries])
|
journal_plain = os.linesep.join([str(e) for e in self.entries])
|
||||||
with open(filename, 'w') as journal_file:
|
with open(filename, 'w') as journal_file:
|
||||||
journal_file.write(journal_plain)
|
if self.crypto:
|
||||||
|
journal_padded = self._block_tail(journal_plain)
|
||||||
|
journal_file.write(self.crypto.encrypt(journal_padded))
|
||||||
|
else:
|
||||||
|
journal_file.write(journal_plain)
|
||||||
|
|
||||||
def sort(self):
|
def sort(self):
|
||||||
"""Sorts the Journal's entries by date"""
|
"""Sorts the Journal's entries by date"""
|
||||||
|
@ -237,9 +255,6 @@ if __name__ == "__main__":
|
||||||
reading.add_argument('-json', dest='json', action="store_true", help='Returns a JSON-encoded version of the Journal')
|
reading.add_argument('-json', dest='json', action="store_true", help='Returns a JSON-encoded version of the Journal')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# open journal
|
|
||||||
journal = Journal(config=config)
|
|
||||||
|
|
||||||
# Guess mode
|
# Guess mode
|
||||||
compose = True
|
compose = True
|
||||||
if args.start_date or args.end_date or args.limit or args.json or args.strict:
|
if args.start_date or args.end_date or args.limit or args.json or args.strict:
|
||||||
|
@ -265,6 +280,9 @@ if __name__ == "__main__":
|
||||||
else:
|
else:
|
||||||
compose = False
|
compose = False
|
||||||
|
|
||||||
|
# open journal
|
||||||
|
journal = Journal(config=config)
|
||||||
|
|
||||||
# Writing mode
|
# Writing mode
|
||||||
if compose:
|
if compose:
|
||||||
raw = " ".join(args.text).strip()
|
raw = " ".join(args.text).strip()
|
||||||
|
|
Loading…
Add table
Reference in a new issue