tcrypto: trivial clean-up of pw_encode/pw_decode functions - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 789b78cab5218630b0c8e1c16494611860f101d4 DIR parent b31efdc3e7bfc763551423ce030250d22889da43 HTML Author: SomberNight <somber.night@protonmail.com> Date: Wed, 8 Apr 2020 12:38:38 +0200 crypto: trivial clean-up of pw_encode/pw_decode functions Diffstat: M electrum/crypto.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) --- DIR diff --git a/electrum/crypto.py b/electrum/crypto.py t@@ -190,6 +190,7 @@ def _hash_password(password: Union[bytes, str], *, version: int) -> bytes: def pw_encode_bytes(data: bytes, password: Union[bytes, str], *, version: int) -> str: + """plaintext bytes -> base64 ciphertext""" if version not in KNOWN_PW_HASH_VERSIONS: raise UnexpectedPasswordHashVersion(version) # derive key from password t@@ -199,7 +200,9 @@ def pw_encode_bytes(data: bytes, password: Union[bytes, str], *, version: int) - ciphertext_b64 = base64.b64encode(ciphertext) return ciphertext_b64.decode('utf8') + def pw_decode_bytes(data: str, password: Union[bytes, str], *, version: int) -> bytes: + """base64 ciphertext -> plaintext bytes""" if version not in KNOWN_PW_HASH_VERSIONS: raise UnexpectedPasswordHashVersion(version) data_bytes = bytes(base64.b64decode(data)) t@@ -212,15 +215,22 @@ def pw_decode_bytes(data: str, password: Union[bytes, str], *, version: int) -> raise InvalidPassword() from e return d + def pw_encode(data: str, password: Union[bytes, str, None], *, version: int) -> str: + """plaintext str -> base64 ciphertext""" if not password: return data - return pw_encode_bytes(to_bytes(data, "utf8"), password, version=version) + plaintext_bytes = to_bytes(data, "utf8") + return pw_encode_bytes(plaintext_bytes, password, version=version) + def pw_decode(data: str, password: Union[bytes, str, None], *, version: int) -> str: + """base64 ciphertext -> plaintext str""" if password is None: return data - return to_string(pw_decode_bytes(data, password, version=version), "utf8") + plaintext_bytes = pw_decode_bytes(data, password, version=version) + plaintext_str = to_string(plaintext_bytes, "utf8") + return plaintext_str def sha256(x: Union[bytes, str]) -> bytes: