Skip to content

Commit 096df69

Browse files
committed
Change: script to pycryptodome package for hashing
1 parent 7cab22a commit 096df69

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

bip38/bip38.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
Type, Union, Optional, Literal, List
99
)
1010
from pyaes import AESModeOfOperationECB
11+
from Crypto.Protocol.KDF import scrypt
1112

12-
import scrypt
1313
import unicodedata
1414
import os
1515

@@ -141,12 +141,16 @@ def intermediate_code(
141141
if not 0 <= sequence <= 4095:
142142
raise Error("Invalid sequence", expected="0 <= sequence <= 4095", got=sequence)
143143

144-
pre_factor: bytes = scrypt.hash(unicodedata.normalize("NFC", passphrase), owner_salt[:4], N=N, r=r, p=p, buflen=32)
144+
pre_factor: bytes = scrypt(
145+
unicodedata.normalize("NFC", passphrase).encode("utf-8"), owner_salt[:4], key_len=32, N=N, r=r, p=p
146+
)
145147
owner_entropy: bytes = owner_salt[:4] + integer_to_bytes((lot * 4096 + sequence), 4)
146148
pass_factor: bytes = double_sha256(pre_factor + owner_entropy)
147149
magic: bytes = integer_to_bytes(MAGIC_LOT_AND_SEQUENCE)
148150
else:
149-
pass_factor: bytes = scrypt.hash(unicodedata.normalize("NFC", passphrase), owner_salt, N=N, r=r, p=p, buflen=32)
151+
pass_factor: bytes = scrypt(
152+
unicodedata.normalize("NFC", passphrase).encode("utf-8"), owner_salt, key_len=32, N=N, r=r, p=p
153+
)
150154
magic: bytes = integer_to_bytes(MAGIC_NO_LOT_AND_SEQUENCE)
151155
owner_entropy: bytes = owner_salt
152156

@@ -219,7 +223,9 @@ def encrypt(
219223
public_key_type=public_key_type
220224
)
221225
address_hash: bytes = get_checksum(get_bytes(address, unhexlify=False))
222-
key: bytes = scrypt.hash(unicodedata.normalize("NFC", passphrase), address_hash, N=N, r=r, p=p)
226+
key: bytes = scrypt(
227+
unicodedata.normalize("NFC", passphrase).encode("utf-8"), address_hash, key_len=64, N=N, r=r, p=p
228+
)
223229
derived_half_1, derived_half_2 = key[0:32], key[32:64]
224230

225231
aes: AESModeOfOperationECB = AESModeOfOperationECB(derived_half_2)
@@ -326,7 +332,7 @@ def create_new_encrypted_wif(
326332
)
327333
address_hash: bytes = get_checksum(get_bytes(address, unhexlify=False))
328334
salt: bytes = address_hash + owner_entropy
329-
scrypt_hash: bytes = scrypt.hash(pass_point, salt, 1024, 1, 1, 64)
335+
scrypt_hash: bytes = scrypt(pass_point, salt, 64, 1024, 1, 1)
330336
derived_half_1, derived_half_2, key = scrypt_hash[:16], scrypt_hash[16:32], scrypt_hash[32:]
331337

332338
aes: AESModeOfOperationECB = AESModeOfOperationECB(key)
@@ -433,15 +439,17 @@ def confirm_code(
433439
else:
434440
owner_salt: bytes = owner_entropy
435441

436-
pass_factor: bytes = scrypt.hash(unicodedata.normalize("NFC", passphrase), owner_salt, N=N, r=r, p=p, buflen=32)
442+
pass_factor: bytes = scrypt(
443+
unicodedata.normalize("NFC", passphrase).encode("utf-8"), owner_salt, key_len=32, N=N, r=r, p=p
444+
)
437445
if lot_and_sequence:
438446
pass_factor: bytes = double_sha256(pass_factor + owner_entropy)
439447
if bytes_to_integer(pass_factor) == 0 or bytes_to_integer(pass_factor) >= NP:
440448
raise Error("Invalid EC encrypted WIF (Wallet Import Format)")
441449

442450
pass_point: bytes = PrivateKey.from_bytes(pass_factor).public_key().raw_compressed()
443451
salt: bytes = address_hash + owner_entropy
444-
scrypt_hash: bytes = scrypt.hash(pass_point, salt, 1024, 1, 1, 64)
452+
scrypt_hash: bytes = scrypt(pass_point, salt, 64, 1024, 1, 1)
445453
derived_half_1, derived_half_2, key = encrypted_point_b[1:17], encrypted_point_b[17:], scrypt_hash[32:]
446454

447455
aes: AESModeOfOperationECB = AESModeOfOperationECB(key)
@@ -556,8 +564,8 @@ def decrypt(
556564
], got=bytes_to_string(flag)
557565
)
558566

559-
key: bytes = scrypt.hash(
560-
unicodedata.normalize("NFC", passphrase), address_hash, N=N, r=r, p=p
567+
key: bytes = scrypt(
568+
unicodedata.normalize("NFC", passphrase).encode("utf-8"), address_hash, key_len=64, N=N, r=r, p=p
561569
)
562570
derived_half_1, derived_half_2 = key[0:32], key[32:64]
563571
encrypted_half_1: bytes = encrypted_wif_decode[7:23]
@@ -611,15 +619,17 @@ def decrypt(
611619
else:
612620
owner_salt: bytes = owner_entropy
613621

614-
pass_factor: bytes = scrypt.hash(unicodedata.normalize("NFC", passphrase), owner_salt, N=N, r=r, p=p, buflen=32)
622+
pass_factor: bytes = scrypt(
623+
unicodedata.normalize("NFC", passphrase).encode("utf-8"), owner_salt, key_len=32, N=N, r=r, p=p
624+
)
615625
if lot_and_sequence:
616626
pass_factor: bytes = double_sha256(pass_factor + owner_entropy)
617627
if bytes_to_integer(pass_factor) == 0 or bytes_to_integer(pass_factor) >= NP:
618628
raise Error("Invalid EC encrypted WIF (Wallet Import Format)")
619629

620630
pre_public_key: PublicKey = PrivateKey.from_bytes(pass_factor).public_key()
621631
salt = address_hash + owner_entropy
622-
encrypted_seed_b: bytes = scrypt.hash(pre_public_key.raw_compressed(), salt, 1024, 1, 1, 64)
632+
encrypted_seed_b: bytes = scrypt(pre_public_key.raw_compressed(), salt, 64, 1024, 1, 1)
623633
key: bytes = encrypted_seed_b[32:]
624634

625635
aes: AESModeOfOperationECB = AESModeOfOperationECB(key)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
pyaes>=1.6.1,<2
2-
scrypt>=0.8.20,<1
2+
pycryptodome>=3.23.0,<4
33
six>=1.16.0,<2
44
ecdsa>=0.18.0,<1

0 commit comments

Comments
 (0)