Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Added authorization, SQLCipher passphrase now AES encrypted
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandtM committed Feb 4, 2016
1 parent e4096a2 commit 5126fa7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
22 changes: 18 additions & 4 deletions journal.py
Expand Up @@ -16,8 +16,10 @@
import datetime
import os

import hashlib
from peewee import *
from playhouse.sqlcipher_ext import SqlCipherDatabase
from Crypto.Cipher import AES

try:
input = raw_input # for python2 compatibility
Expand All @@ -26,8 +28,16 @@

path = os.getenv('HOME', os.path.expanduser('~')) + '/.tnote'

with open('cipher_key.txt', 'r') as cipher_file:
db = SqlCipherDatabase(path + '/diary.db', passphrase=cipher_file.read())
# Makes sure that the length of a string is a multiple of 32. Otherwise it is padded with the '^' character
pad_string = lambda s: s + (32 - len(s) % 32) * '^'

password = input('Please enter your key: ')
key = hashlib.sha256(password.encode('utf-8')).digest()
cryptor = AES.new(key)
passphrase = input('Please enter your passphrase: ')
crypted_pass = cryptor.encrypt(pad_string(passphrase))

db = SqlCipherDatabase(path + '/diary.db', passphrase=str(crypted_pass))

class DiaryEntry(Model):

Expand All @@ -49,8 +59,12 @@ def initialize():

if not os.path.exists(path):
os.makedirs(path)
db.connect()
db.create_tables([DiaryEntry], safe=True)
try:
db.connect()
db.create_tables([DiaryEntry], safe=True)
except DatabaseError:
print('Your key and/or passphrase were incorrect.\nPlease restart the application and try again!')
exit(0)


def menu_loop():
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
@@ -1,2 +1,3 @@
peewee==2.8.0
pysqlcipher3==1.0.1
pysqlcipher3==1.0.1
pycrypto==2.6.1

0 comments on commit 5126fa7

Please sign in to comment.