Skip to content
Snippets Groups Projects
Commit 137136ed authored by inso's avatar inso
Browse files

Fix base58 types

parent 9b037ffd
Branches
Tags
No related merge requests found
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
import base58 import base58
from ..helpers import ensure_str, ensure_bytes
class Base58Encoder(object): class Base58Encoder(object):
@staticmethod @staticmethod
def encode(data): def encode(data):
return base58.b58encode(data) return ensure_str(base58.b58encode(ensure_bytes(data)))
@staticmethod @staticmethod
def decode(data): def decode(data):
return base58.b58decode(data) return ensure_str(base58.b58decode(ensure_bytes(data)))
\ No newline at end of file
...@@ -7,7 +7,7 @@ duniter public and private keys ...@@ -7,7 +7,7 @@ duniter public and private keys
import libnacl.public import libnacl.public
from pylibscrypt import scrypt from pylibscrypt import scrypt
from .base58 import Base58Encoder from .base58 import Base58Encoder
from .signing_key import _ensure_bytes from ..helpers import ensure_bytes
SEED_LENGTH = 32 # Length of the key SEED_LENGTH = 32 # Length of the key
...@@ -20,8 +20,8 @@ SCRYPT_PARAMS = {'N': 4096, ...@@ -20,8 +20,8 @@ SCRYPT_PARAMS = {'N': 4096,
class SecretKey(libnacl.public.SecretKey): class SecretKey(libnacl.public.SecretKey):
def __init__(self, salt, password): def __init__(self, salt, password):
salt = _ensure_bytes(salt) salt = ensure_bytes(salt)
password = _ensure_bytes(password) password = ensure_bytes(password)
seed = scrypt(password, salt, seed = scrypt(password, salt,
SCRYPT_PARAMS['N'], SCRYPT_PARAMS['r'], SCRYPT_PARAMS['p'], SCRYPT_PARAMS['N'], SCRYPT_PARAMS['r'], SCRYPT_PARAMS['p'],
SEED_LENGTH) SEED_LENGTH)
...@@ -30,15 +30,15 @@ class SecretKey(libnacl.public.SecretKey): ...@@ -30,15 +30,15 @@ class SecretKey(libnacl.public.SecretKey):
self.public_key = PublicKey(Base58Encoder.encode(self.pk)) self.public_key = PublicKey(Base58Encoder.encode(self.pk))
def encrypt(self, pubkey, noonce, text): def encrypt(self, pubkey, noonce, text):
text_bytes = _ensure_bytes(text) text_bytes = ensure_bytes(text)
noonce_bytes = _ensure_bytes(noonce) noonce_bytes = ensure_bytes(noonce)
recipient_pubkey = PublicKey(pubkey) recipient_pubkey = PublicKey(pubkey)
crypt_bytes = libnacl.public.Box(self, recipient_pubkey).encrypt(text_bytes, noonce_bytes) crypt_bytes = libnacl.public.Box(self, recipient_pubkey).encrypt(text_bytes, noonce_bytes)
return Base58Encoder.encode(crypt_bytes[24:]) return Base58Encoder.encode(crypt_bytes[24:])
def decrypt(self, pubkey, noonce, text): def decrypt(self, pubkey, noonce, text):
sender_pubkey = PublicKey(pubkey) sender_pubkey = PublicKey(pubkey)
noonce_bytes = _ensure_bytes(noonce) noonce_bytes = ensure_bytes(noonce)
encrypt_bytes = Base58Encoder.decode(text) encrypt_bytes = Base58Encoder.decode(text)
decrypt_bytes = libnacl.public.Box(self, sender_pubkey).decrypt(encrypt_bytes, noonce_bytes) decrypt_bytes = libnacl.public.Box(self, sender_pubkey).decrypt(encrypt_bytes, noonce_bytes)
return decrypt_bytes.decode('utf-8') return decrypt_bytes.decode('utf-8')
......
...@@ -7,7 +7,7 @@ duniter public and private keys ...@@ -7,7 +7,7 @@ duniter public and private keys
import libnacl.sign import libnacl.sign
from pylibscrypt import scrypt from pylibscrypt import scrypt
from .base58 import Base58Encoder from .base58 import Base58Encoder
from ..helpers import ensure_bytes
SEED_LENGTH = 32 # Length of the key SEED_LENGTH = 32 # Length of the key
crypto_sign_BYTES = 64 crypto_sign_BYTES = 64
...@@ -27,19 +27,6 @@ class ScryptParams: ...@@ -27,19 +27,6 @@ class ScryptParams:
self.p = p 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): class SigningKey(libnacl.sign.Signer):
def __init__(self, salt, password, scrypt_params=None): def __init__(self, salt, password, scrypt_params=None):
""" """
...@@ -52,8 +39,8 @@ class SigningKey(libnacl.sign.Signer): ...@@ -52,8 +39,8 @@ class SigningKey(libnacl.sign.Signer):
if scrypt_params is None: if scrypt_params is None:
scrypt_params = ScryptParams(4096, 16, 1) scrypt_params = ScryptParams(4096, 16, 1)
salt = _ensure_bytes(salt) salt = ensure_bytes(salt)
password = _ensure_bytes(password) password = ensure_bytes(password)
seed = scrypt(password, salt, seed = scrypt(password, salt,
scrypt_params.N, scrypt_params.r, scrypt_params.p, scrypt_params.N, scrypt_params.r, scrypt_params.p,
SEED_LENGTH) SEED_LENGTH)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment