diff --git a/ankisyncd/sync_app.py b/ankisyncd/sync_app.py index a24f138..aa281e1 100644 --- a/ankisyncd/sync_app.py +++ b/ankisyncd/sync_app.py @@ -30,7 +30,7 @@ import string import unicodedata import zipfile from sqlite3 import dbapi2 as sqlite -from io import StringIO +from io import BytesIO import ankisyncd @@ -133,7 +133,7 @@ class SyncMediaHandler(anki.sync.MediaSyncer): max_zip_size = 100*1024*1024 max_meta_file_size = 100000 - file_buffer = StringIO(zip_data) + file_buffer = BytesIO(zip_data) zip_file = zipfile.ZipFile(file_buffer, 'r') meta_file_size = zip_file.getinfo("_meta").file_size @@ -155,11 +155,11 @@ class SyncMediaHandler(anki.sync.MediaSyncer): according to the data in zip file zipData. """ - file_buffer = StringIO(zip_data) + file_buffer = BytesIO(zip_data) zip_file = zipfile.ZipFile(file_buffer, 'r') # Get meta info first. - meta = json.loads(zip_file.read("_meta")) + meta = json.loads(zip_file.read("_meta").decode()) # Remove media files that were removed on the client. media_to_remove = [] @@ -241,7 +241,7 @@ class SyncMediaHandler(anki.sync.MediaSyncer): flist = {} cnt = 0 sz = 0 - f = StringIO() + f = BytesIO() z = zipfile.ZipFile(f, "w", compression=zipfile.ZIP_DEFLATED) for fname in files: @@ -379,7 +379,7 @@ class SyncApp: import hashlib, time, random, string chars = string.ascii_letters + string.digits - val = ':'.join([username, str(int(time.time())), ''.join(random.choice(chars) for x in range(8))]) + val = ':'.join([username, str(int(time.time())), ''.join(random.choice(chars) for x in range(8))]).encode() return hashlib.md5(val).hexdigest() def create_session(self, username, user_path): @@ -389,13 +389,13 @@ class SyncApp: import gzip if compression: - buf = gzip.GzipFile(mode="rb", fileobj=StringIO(data)) + buf = gzip.GzipFile(mode="rb", fileobj=BytesIO(data)) data = buf.read() buf.close() try: - data = json.loads(data) - except ValueError: + data = json.loads(data.decode()) + except (ValueError, UnicodeDecodeError): data = {'data': data} return data diff --git a/ankisyncd/users.py b/ankisyncd/users.py index 5eebc77..8c64c91 100644 --- a/ankisyncd/users.py +++ b/ankisyncd/users.py @@ -137,7 +137,7 @@ class SqliteUserManager(SimpleUserManager): salt = self._extract_salt(expected_value) hashobj = hashlib.sha256() - hashobj.update(username + password + salt) + hashobj.update((username + password + salt).encode()) actual_value = hashobj.hexdigest() + salt if actual_value == expected_value: @@ -156,8 +156,8 @@ class SqliteUserManager(SimpleUserManager): @staticmethod def _create_pass_hash(username, password): salt = binascii.b2a_hex(os.urandom(8)) - pass_hash = (hashlib.sha256(username + password + salt).hexdigest() + - salt) + pass_hash = (hashlib.sha256((username + password).encode() + salt).hexdigest() + + salt.decode()) return pass_hash def create_auth_db(self): diff --git a/tests/helpers/file_utils.py b/tests/helpers/file_utils.py index 0e6b71a..a90b4e1 100644 --- a/tests/helpers/file_utils.py +++ b/tests/helpers/file_utils.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from io import StringIO +from io import BytesIO import json import logging import logging.config @@ -28,7 +28,7 @@ def create_named_file(filename, file_contents=None): if file_contents is not None: open(file_path, 'w').write(file_contents) - return file_path.decode("utf-8") + return file_path def create_zip_with_existing_files(file_paths): @@ -41,7 +41,7 @@ def create_zip_with_existing_files(file_paths): :return: the data of the created zip file """ - file_buffer = StringIO() + file_buffer = BytesIO() zip_file = zipfile.ZipFile(file_buffer, 'w', compression=zipfile.ZIP_DEFLATED)