diff --git a/duniterpy/helpers.py b/duniterpy/helpers.py new file mode 100644 index 0000000000000000000000000000000000000000..851b1cbd9ea4fc141d9ca61ef94a3ba09f4f485c --- /dev/null +++ b/duniterpy/helpers.py @@ -0,0 +1,26 @@ + + +def ensure_bytes(data): + """ + Convert data in bytes if data is a string + + :param data: Data + :rtype bytes: + """ + if isinstance(data, str): + return bytes(data, 'utf-8') + + return data + + +def ensure_str(data): + """ + Convert data in str if data are bytes + + :param data: Data + :rtype str: + """ + if isinstance(data, bytes): + return str(data, 'utf-8') + + return data \ No newline at end of file diff --git a/duniterpy/key/base58.py b/duniterpy/key/base58.py index 5bcb8060a03095cba6914ab97ce4464049bf4231..60ff9514b3c71fc9b50bb84b0c6b7223d82e8bc5 100644 --- a/duniterpy/key/base58.py +++ b/duniterpy/key/base58.py @@ -1,10 +1,12 @@ import base58 +from ..helpers import ensure_str, ensure_bytes + class Base58Encoder(object): @staticmethod def encode(data): - return base58.b58encode(data) + return ensure_str(base58.b58encode(ensure_bytes(data))) @staticmethod def decode(data): - return base58.b58decode(data) \ No newline at end of file + return ensure_str(base58.b58decode(ensure_bytes(data))) diff --git a/duniterpy/key/encryption_key.py b/duniterpy/key/encryption_key.py index f7f27fe5e2884b648e7504b235b8f757f933e940..4caa2aeb6f3c449c967df2bbd1b208dd408f8ecd 100644 --- a/duniterpy/key/encryption_key.py +++ b/duniterpy/key/encryption_key.py @@ -7,7 +7,7 @@ duniter public and private keys import libnacl.public from pylibscrypt import scrypt from .base58 import Base58Encoder -from .signing_key import _ensure_bytes +from ..helpers import ensure_bytes SEED_LENGTH = 32 # Length of the key @@ -20,8 +20,8 @@ SCRYPT_PARAMS = {'N': 4096, class SecretKey(libnacl.public.SecretKey): def __init__(self, salt, password): - salt = _ensure_bytes(salt) - password = _ensure_bytes(password) + salt = ensure_bytes(salt) + password = ensure_bytes(password) seed = scrypt(password, salt, SCRYPT_PARAMS['N'], SCRYPT_PARAMS['r'], SCRYPT_PARAMS['p'], SEED_LENGTH) @@ -30,15 +30,15 @@ class SecretKey(libnacl.public.SecretKey): self.public_key = PublicKey(Base58Encoder.encode(self.pk)) def encrypt(self, pubkey, noonce, text): - text_bytes = _ensure_bytes(text) - noonce_bytes = _ensure_bytes(noonce) + text_bytes = ensure_bytes(text) + noonce_bytes = ensure_bytes(noonce) recipient_pubkey = PublicKey(pubkey) crypt_bytes = libnacl.public.Box(self, recipient_pubkey).encrypt(text_bytes, noonce_bytes) return Base58Encoder.encode(crypt_bytes[24:]) def decrypt(self, pubkey, noonce, text): sender_pubkey = PublicKey(pubkey) - noonce_bytes = _ensure_bytes(noonce) + noonce_bytes = ensure_bytes(noonce) encrypt_bytes = Base58Encoder.decode(text) decrypt_bytes = libnacl.public.Box(self, sender_pubkey).decrypt(encrypt_bytes, noonce_bytes) return decrypt_bytes.decode('utf-8') diff --git a/duniterpy/key/signing_key.py b/duniterpy/key/signing_key.py index 2f2bf5aff08ef7823c3516edc00b5b28f845810b..aac85a22d216bed37ed7b7122d8e7acc1804a199 100644 --- a/duniterpy/key/signing_key.py +++ b/duniterpy/key/signing_key.py @@ -7,7 +7,7 @@ duniter public and private keys import libnacl.sign from pylibscrypt import scrypt from .base58 import Base58Encoder - +from ..helpers import ensure_bytes SEED_LENGTH = 32 # Length of the key crypto_sign_BYTES = 64 @@ -27,19 +27,6 @@ class ScryptParams: self.p = p -def _ensure_bytes(data): - """ - Convert data in bytes if data is a string - - :param data: Data - :rtype bytes: - """ - if isinstance(data, str): - return bytes(data, 'utf-8') - - return data - - class SigningKey(libnacl.sign.Signer): def __init__(self, salt, password, scrypt_params=None): """ @@ -52,8 +39,8 @@ class SigningKey(libnacl.sign.Signer): if scrypt_params is None: scrypt_params = ScryptParams(4096, 16, 1) - salt = _ensure_bytes(salt) - password = _ensure_bytes(password) + salt = ensure_bytes(salt) + password = ensure_bytes(password) seed = scrypt(password, salt, scrypt_params.N, scrypt_params.r, scrypt_params.p, SEED_LENGTH)