|
8 | 8 | Type, Union, Optional, Literal, List |
9 | 9 | ) |
10 | 10 | from pyaes import AESModeOfOperationECB |
| 11 | +from Crypto.Protocol.KDF import scrypt |
11 | 12 |
|
12 | | -import scrypt |
13 | 13 | import unicodedata |
14 | 14 | import os |
15 | 15 |
|
@@ -141,12 +141,16 @@ def intermediate_code( |
141 | 141 | if not 0 <= sequence <= 4095: |
142 | 142 | raise Error("Invalid sequence", expected="0 <= sequence <= 4095", got=sequence) |
143 | 143 |
|
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 | + ) |
145 | 147 | owner_entropy: bytes = owner_salt[:4] + integer_to_bytes((lot * 4096 + sequence), 4) |
146 | 148 | pass_factor: bytes = double_sha256(pre_factor + owner_entropy) |
147 | 149 | magic: bytes = integer_to_bytes(MAGIC_LOT_AND_SEQUENCE) |
148 | 150 | 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 | + ) |
150 | 154 | magic: bytes = integer_to_bytes(MAGIC_NO_LOT_AND_SEQUENCE) |
151 | 155 | owner_entropy: bytes = owner_salt |
152 | 156 |
|
@@ -219,7 +223,9 @@ def encrypt( |
219 | 223 | public_key_type=public_key_type |
220 | 224 | ) |
221 | 225 | 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 | + ) |
223 | 229 | derived_half_1, derived_half_2 = key[0:32], key[32:64] |
224 | 230 |
|
225 | 231 | aes: AESModeOfOperationECB = AESModeOfOperationECB(derived_half_2) |
@@ -326,7 +332,7 @@ def create_new_encrypted_wif( |
326 | 332 | ) |
327 | 333 | address_hash: bytes = get_checksum(get_bytes(address, unhexlify=False)) |
328 | 334 | 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) |
330 | 336 | derived_half_1, derived_half_2, key = scrypt_hash[:16], scrypt_hash[16:32], scrypt_hash[32:] |
331 | 337 |
|
332 | 338 | aes: AESModeOfOperationECB = AESModeOfOperationECB(key) |
@@ -433,15 +439,17 @@ def confirm_code( |
433 | 439 | else: |
434 | 440 | owner_salt: bytes = owner_entropy |
435 | 441 |
|
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 | + ) |
437 | 445 | if lot_and_sequence: |
438 | 446 | pass_factor: bytes = double_sha256(pass_factor + owner_entropy) |
439 | 447 | if bytes_to_integer(pass_factor) == 0 or bytes_to_integer(pass_factor) >= NP: |
440 | 448 | raise Error("Invalid EC encrypted WIF (Wallet Import Format)") |
441 | 449 |
|
442 | 450 | pass_point: bytes = PrivateKey.from_bytes(pass_factor).public_key().raw_compressed() |
443 | 451 | 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) |
445 | 453 | derived_half_1, derived_half_2, key = encrypted_point_b[1:17], encrypted_point_b[17:], scrypt_hash[32:] |
446 | 454 |
|
447 | 455 | aes: AESModeOfOperationECB = AESModeOfOperationECB(key) |
@@ -556,8 +564,8 @@ def decrypt( |
556 | 564 | ], got=bytes_to_string(flag) |
557 | 565 | ) |
558 | 566 |
|
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 |
561 | 569 | ) |
562 | 570 | derived_half_1, derived_half_2 = key[0:32], key[32:64] |
563 | 571 | encrypted_half_1: bytes = encrypted_wif_decode[7:23] |
@@ -611,15 +619,17 @@ def decrypt( |
611 | 619 | else: |
612 | 620 | owner_salt: bytes = owner_entropy |
613 | 621 |
|
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 | + ) |
615 | 625 | if lot_and_sequence: |
616 | 626 | pass_factor: bytes = double_sha256(pass_factor + owner_entropy) |
617 | 627 | if bytes_to_integer(pass_factor) == 0 or bytes_to_integer(pass_factor) >= NP: |
618 | 628 | raise Error("Invalid EC encrypted WIF (Wallet Import Format)") |
619 | 629 |
|
620 | 630 | pre_public_key: PublicKey = PrivateKey.from_bytes(pass_factor).public_key() |
621 | 631 | 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) |
623 | 633 | key: bytes = encrypted_seed_b[32:] |
624 | 634 |
|
625 | 635 | aes: AESModeOfOperationECB = AESModeOfOperationECB(key) |
|
0 commit comments