forked from chaintool/funga-eth
Compare commits
No commits in common. "3a94f7e5a2fb3bc76d54460999fdce8f895b5b57" and "39435b67ba7441f86f4703e24a962f6ab1fd607d" have entirely different histories.
3a94f7e5a2
...
39435b67ba
@ -15,18 +15,17 @@ import sha3
|
||||
from funga.error import (
|
||||
DecryptError,
|
||||
KeyfileError,
|
||||
)
|
||||
)
|
||||
from funga.eth.encoding import private_key_to_address
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
algo_keywords = [
|
||||
'aes-128-ctr',
|
||||
]
|
||||
]
|
||||
hash_keywords = [
|
||||
'scrypt',
|
||||
'pbkdf2'
|
||||
]
|
||||
'scrypt'
|
||||
]
|
||||
|
||||
default_kdfparams = {
|
||||
'dklen': 32,
|
||||
@ -34,14 +33,8 @@ default_kdfparams = {
|
||||
'p': 1,
|
||||
'r': 8,
|
||||
'salt': os.urandom(32).hex(),
|
||||
}
|
||||
}
|
||||
|
||||
pbkdf2_kdfparams = {
|
||||
'c': 100000,
|
||||
'dklen': 32,
|
||||
'prf': 'sha256',
|
||||
'salt': os.urandom(32).hex(),
|
||||
}
|
||||
|
||||
def to_mac(mac_key, ciphertext_bytes):
|
||||
h = sha3.keccak_256()
|
||||
@ -60,29 +53,11 @@ class Hashes:
|
||||
r = int(kdfparams['r'])
|
||||
salt = bytes.fromhex(kdfparams['salt'])
|
||||
|
||||
return hashlib.scrypt(passphrase.encode('utf-8'), salt=salt, n=n, p=p, r=r, maxmem=1024 * 1024 * 1024,
|
||||
dklen=dklen)
|
||||
|
||||
@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'])
|
||||
|
||||
derived_key = hashlib.pbkdf2_hmac(
|
||||
hash_name=hashname,
|
||||
password=pwd,
|
||||
salt=salt,
|
||||
iterations=itr,
|
||||
dklen=dklen
|
||||
)
|
||||
|
||||
return derived_key
|
||||
return hashlib.scrypt(passphrase.encode('utf-8'), salt=salt,n=n, p=p, r=r, maxmem=1024*1024*1024, dklen=dklen)
|
||||
|
||||
|
||||
class Ciphers:
|
||||
|
||||
aes_128_block_size = 1 << 7
|
||||
aes_iv_len = 16
|
||||
|
||||
@ -93,6 +68,7 @@ class Ciphers:
|
||||
plaintext = cipher.decrypt(ciphertext)
|
||||
return plaintext
|
||||
|
||||
|
||||
@staticmethod
|
||||
def encrypt_aes_128_ctr(plaintext, encryption_key, iv):
|
||||
ctr = Counter.new(Ciphers.aes_128_block_size, initial_value=iv)
|
||||
@ -101,19 +77,11 @@ class Ciphers:
|
||||
return ciphertext
|
||||
|
||||
|
||||
def to_dict(private_key_bytes, kdf :str, passphrase=''):
|
||||
def to_dict(private_key_bytes, passphrase=''):
|
||||
|
||||
private_key = coincurve.PrivateKey(secret=private_key_bytes)
|
||||
|
||||
if kdf == 'scrypt':
|
||||
encryption_key = Hashes.from_scrypt(passphrase=passphrase)
|
||||
kdfparams = default_kdfparams
|
||||
|
||||
elif kdf == 'pbkdf2':
|
||||
encryption_key = Hashes.from_pbkdf2(passphrase=passphrase)
|
||||
kdfparams = pbkdf2_kdfparams
|
||||
|
||||
else:
|
||||
raise NotImplementedError("KDF not implemented: {0}".format(kdf))
|
||||
|
||||
address_hex = private_key_to_address(private_key)
|
||||
iv_bytes = os.urandom(Ciphers.aes_iv_len)
|
||||
@ -128,8 +96,8 @@ def to_dict(private_key_bytes, kdf :str, passphrase=''):
|
||||
'cipherparams': {
|
||||
'iv': iv_bytes.hex(),
|
||||
},
|
||||
'kdf': kdf,
|
||||
'kdfparams': kdfparams,
|
||||
'kdf': 'scrypt',
|
||||
'kdfparams': default_kdfparams,
|
||||
'mac': mac.hex(),
|
||||
}
|
||||
|
||||
@ -144,6 +112,7 @@ def to_dict(private_key_bytes, kdf :str, passphrase=''):
|
||||
|
||||
|
||||
def from_dict(o, passphrase=''):
|
||||
|
||||
cipher = o['crypto']['cipher']
|
||||
if cipher not in algo_keywords:
|
||||
raise NotImplementedError('cipher "{}" not implemented'.format(cipher))
|
||||
@ -176,6 +145,7 @@ def from_dict(o, passphrase=''):
|
||||
|
||||
|
||||
def from_file(filepath, passphrase=''):
|
||||
|
||||
f = open(filepath, 'r')
|
||||
try:
|
||||
o = json.load(f)
|
||||
|
@ -16,10 +16,6 @@ from funga.eth.keystore.keyfile import (
|
||||
from_file,
|
||||
to_dict,
|
||||
)
|
||||
# from testkeyfile import (
|
||||
# from_file,
|
||||
# to_dict
|
||||
# )
|
||||
from funga.eth.encoding import (
|
||||
private_key_to_address,
|
||||
private_key_from_bytes,
|
||||
@ -81,7 +77,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, passphrase)
|
||||
r = json.dumps(o)
|
||||
|
||||
print(r)
|
||||
|
1
tests/testdata/foo2.json
vendored
1
tests/testdata/foo2.json
vendored
@ -1 +0,0 @@
|
||||
{"address": "a1De08A738F0bD3B261dC41d7E2102599bf648CA", "version": 3, "crypto": {"cipher": "aes-128-ctr", "ciphertext": "2240149943557906e6ee9bcb864d6148dce0c0d8245d1da8d466dcb39edcde6a", "cipherparams": {"iv": "a0c366fdfe86f21a4168b4f12059e7fa"}, "kdf": "pbkdf2", "kdfparams": {"c": 100000, "dklen": 32, "prf": "sha256", "salt": "73cb66c8cf0e60e36f7ec835a057638a17ed315cebb3b691574c32cfed827b6f"}, "mac": "6760ad0841da7976a41f134e021be450baa453efdec8fe5ba7790cfa79d0c14b"}, "id": "22e597ea-777e-11ec-be42-a8a7956d3851"}
|
Loading…
Reference in New Issue
Block a user