From c8f602c9d7f800e0cffffa96ff2c997a6c0653ea Mon Sep 17 00:00:00 2001 From: ThomasV Date: Fri, 3 Apr 2020 12:29:55 +0200 Subject: [PATCH] pw_decode, pw_encode: separate bytes functions --- electrum/crypto.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/electrum/crypto.py b/electrum/crypto.py index e12af10b4..58f66c2d1 100644 --- a/electrum/crypto.py +++ b/electrum/crypto.py @@ -189,22 +189,17 @@ def _hash_password(password: Union[bytes, str], *, version: int) -> bytes: raise UnexpectedPasswordHashVersion(version) -def pw_encode(data: str, password: Union[bytes, str, None], *, version: int) -> str: - if not password: - return data +def pw_encode_bytes(data: bytes, password: Union[bytes, str], *, version: int) -> str: if version not in KNOWN_PW_HASH_VERSIONS: raise UnexpectedPasswordHashVersion(version) # derive key from password secret = _hash_password(password, version=version) # encrypt given data - ciphertext = EncodeAES_bytes(secret, to_bytes(data, "utf8")) + ciphertext = EncodeAES_bytes(secret, data) ciphertext_b64 = base64.b64encode(ciphertext) return ciphertext_b64.decode('utf8') - -def pw_decode(data: str, password: Union[bytes, str, None], *, version: int) -> str: - if password is None: - return data +def pw_decode_bytes(data: str, password: Union[bytes, str], *, version: int) -> bytes: if version not in KNOWN_PW_HASH_VERSIONS: raise UnexpectedPasswordHashVersion(version) data_bytes = bytes(base64.b64decode(data)) @@ -212,11 +207,21 @@ def pw_decode(data: str, password: Union[bytes, str, None], *, version: int) -> secret = _hash_password(password, version=version) # decrypt given data try: - d = to_string(DecodeAES_bytes(secret, data_bytes), "utf8") + d = DecodeAES_bytes(secret, data_bytes) except Exception as e: raise InvalidPassword() from e return d +def pw_encode(data: str, password: Union[bytes, str, None], *, version: int) -> str: + if not password: + return data + return pw_encode_bytes(to_bytes(data, "utf8"), password, version=version) + +def pw_decode(data: str, password: Union[bytes, str, None], *, version: int) -> str: + if password is None: + return data + return to_string(pw_decode_bytes(data, password, version=version), "utf8") + def sha256(x: Union[bytes, str]) -> bytes: x = to_bytes(x, 'utf8')