Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix composite key computation for BytesIO #388

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pykeepass/kdbx_parsing/common.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import base64
import codecs
import hashlib
import io
import logging
import re
import zlib
from binascii import Error as BinasciiError
from collections import OrderedDict
from copy import deepcopy
from io import BytesIO

from construct import (
Adapter,
Expand Down Expand Up @@ -128,6 +128,8 @@ def compute_key_composite(password=None, keyfile=None):
# hash the keyfile
if keyfile:
if hasattr(keyfile, "read"):
if hasattr(keyfile, "seekable") and keyfile.seekable():
keyfile.seek(0)
keyfile_bytes = keyfile.read()
else:
with open(keyfile, 'rb') as f:
Expand Down Expand Up @@ -194,7 +196,7 @@ class XML(Adapter):

def _decode(self, data, con, path):
parser = etree.XMLParser(remove_blank_text=True)
return etree.parse(BytesIO(data), parser)
return etree.parse(io.BytesIO(data), parser)

def _encode(self, tree, con, path):
return etree.tostring(tree)
Expand Down
14 changes: 14 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,20 @@ def test_fields(self):


class PyKeePassTests3(KDBX3Tests):
def test_consecutives_saves_with_stream(self):
janbrummer marked this conversation as resolved.
Show resolved Hide resolved
# https://github.com/libkeepass/pykeepass/pull/388
self.setUp()

with open(base_dir / self.keyfile_tmp, 'rb') as f:
keyfile = BytesIO(f.read())

for _i in range(5):
with PyKeePass(
base_dir / self.database_tmp,
password=self.password,
keyfile=keyfile,
) as kp:
kp.save()

def test_set_credentials(self):
self.kp_tmp.password = 'f00bar'
Expand Down
Loading