review changes with tests

This commit is contained in:
2022-01-24 13:23:12 +03:00
parent b9c6db414b
commit b20853312d
3 changed files with 26 additions and 17 deletions

View File

@@ -28,7 +28,7 @@ hash_keywords = [
'pbkdf2'
]
default_kdfparams = {
default_scrypt_kdfparams = {
'dklen': 32,
'n': 1 << 18,
'p': 1,
@@ -53,7 +53,7 @@ def to_mac(mac_key, ciphertext_bytes):
class Hashes:
@staticmethod
def from_scrypt(kdfparams=default_kdfparams, passphrase=''):
def from_scrypt(kdfparams=default_scrypt_kdfparams, passphrase=''):
dklen = int(kdfparams['dklen'])
n = int(kdfparams['n'])
p = int(kdfparams['p'])
@@ -65,20 +65,16 @@ class Hashes:
@staticmethod
def from_pbkdf2(kdfparams=pbkdf2_kdfparams, passphrase=''):
hashname = kdfparams['prf']
pwd = passphrase.encode('utf-8')
salt = bytes.fromhex(kdfparams['salt'])
itr = int(kdfparams['c'])
dklen = int(kdfparams['dklen'])
if kdfparams['prf'] == 'hmac-sha256':
kdfparams['prf'].replace('sha256')
derived_key = hashlib.pbkdf2_hmac(
hash_name=hashname,
password=pwd,
salt=salt,
iterations=itr,
dklen=dklen
hash_name=kdfparams['prf'],
password=passphrase.encode('utf-8'),
salt=bytes.fromhex(kdfparams['salt']),
iterations=int(kdfparams['c']),
dklen=int(kdfparams['dklen'])
)
return derived_key
@@ -106,7 +102,7 @@ def to_dict(private_key_bytes, kdf :str, passphrase=''):
if kdf == 'scrypt':
encryption_key = Hashes.from_scrypt(passphrase=passphrase)
kdfparams = default_kdfparams
kdfparams = default_scrypt_kdfparams
elif kdf == 'pbkdf2':
encryption_key = Hashes.from_pbkdf2(passphrase=passphrase)

View File

@@ -16,10 +16,15 @@ from funga.eth.keystore.keyfile import (
from_file,
to_dict,
)
# from testkeyfile import (
# from pathlib import Path
# import sys
# path_root = Path(__file__).parents[1]
# sys.path.append(str(path_root))
# from keystore.testkeyfile import (
# from_file,
# to_dict
# )
from funga.eth.encoding import (
private_key_to_address,
private_key_from_bytes,
@@ -81,7 +86,7 @@ def main():
else:
pk_bytes = os.urandom(32)
pk = coincurve.PrivateKey(secret=pk_bytes)
o = to_dict(pk_bytes, 'pbkdf2', passphrase)
o = to_dict(pk_bytes, 'scrypt', passphrase)
r = json.dumps(o)
print(r)