From 76c9006ed39ddabb4fa6a854eabc6af3e9cd1235 Mon Sep 17 00:00:00 2001 From: Manuel Ebert Date: Sun, 5 Apr 2015 06:05:46 +0400 Subject: [PATCH] Python 3 fixes :) --- jrnl/EncryptedJournal.py | 7 +++---- jrnl/cli.py | 1 - jrnl/util.py | 9 ++++++++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/jrnl/EncryptedJournal.py b/jrnl/EncryptedJournal.py index c5154a96..3fbffa69 100644 --- a/jrnl/EncryptedJournal.py +++ b/jrnl/EncryptedJournal.py @@ -9,13 +9,12 @@ import base64 def make_key(password): - if type(password) is unicode: - password = password.encode('utf-8') + password = util.bytes(password) kdf = PBKDF2HMAC( algorithm=hashes.SHA256(), length=32, # Salt is hard-coded - salt='\xf2\xd5q\x0e\xc1\x8d.\xde\xdc\x8e6t\x89\x04\xce\xf8', + salt=b'\xf2\xd5q\x0e\xc1\x8d.\xde\xdc\x8e6t\x89\x04\xce\xf8', iterations=100000, backend=default_backend() ) @@ -34,7 +33,7 @@ class EncryptedJournal(Journal.Journal): and otherwise ask the user to enter a password up to three times. If the password is provided but wrong (or corrupt), this will simply return None.""" - with open(filename) as f: + with open(filename, 'rb') as f: journal_encrypted = f.read() def validate_password(password): diff --git a/jrnl/cli.py b/jrnl/cli.py index 6492e85f..389afc0a 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -13,7 +13,6 @@ from . import util from . import install from . import plugins import jrnl -import os import argparse import sys import logging diff --git a/jrnl/util.py b/jrnl/util.py index bf3bb3c8..6d5ba528 100644 --- a/jrnl/util.py +++ b/jrnl/util.py @@ -75,10 +75,17 @@ def u(s): def py2encode(s): - """Encode in Python 2, but not in python 3.""" + """Encodes to UTF-8 in Python 2 but not r.""" return s.encode("utf-8") if PY2 and type(s) is unicode else s +def bytes(s): + """Returns bytes, no matter what.""" + if PY3: + return s.encode("utf-8") if type(s) is not bytes else s + return s.encode("utf-8") if type(s) is unicode else s + + def prnt(s): """Encode and print a string""" STDOUT.write(u(s + "\n"))