Skip to content

Commit b73ea7c

Browse files
authored
Update btcrpass.py
1 parent 9e8ac85 commit b73ea7c

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

btcrecover/btcrpass.py

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,27 @@ def prompt_unicode_password(prompt, error_msg):
432432
error_exit(error_msg)
433433
return password
434434

435+
def _dispatch_to_cpu_or_gpu(wallet_instance, passwords):
436+
"""Dispatches password verification to the appropriate CPU or GPU/OpenCL implementation."""
437+
# Legacy GPU path for WalletBitcoinCore
438+
if hasattr(wallet_instance, "_cl_devices"):
439+
return wallet_instance._return_verified_password_or_false_gpu(passwords)
440+
441+
# Determine if OpenCL should be used
442+
use_opencl = False
443+
# The check for self.opencl is for BIP39, Cardano, Yoroi, Brainwallet
444+
if hasattr(wallet_instance, 'opencl') and wallet_instance.opencl:
445+
use_opencl = not isinstance(wallet_instance.opencl_algo, int)
446+
# The check for just opencl_algo is for most other wallets
447+
elif hasattr(wallet_instance, 'opencl_algo'):
448+
use_opencl = not isinstance(wallet_instance.opencl_algo, int)
449+
450+
if use_opencl:
451+
return wallet_instance._return_verified_password_or_false_opencl(passwords)
452+
else:
453+
return wallet_instance._return_verified_password_or_false_cpu(passwords)
454+
455+
435456
############### Bitcoin Core ###############
436457

437458
@register_wallet_class
@@ -582,14 +603,8 @@ def load_from_data_extract(cls, mkey_data):
582603
def difficulty_info(self):
583604
return "{:,} SHA-512 iterations".format(self._iter_count)
584605

585-
# Defer to either the cpu or OpenCL implementation
586606
def return_verified_password_or_false(self, passwords): # Bitcoin Core
587-
if hasattr(self, "_cl_devices"):
588-
return self._return_verified_password_or_false_gpu(passwords)
589-
elif not isinstance(self.opencl_algo,int):
590-
return self._return_verified_password_or_false_opencl(passwords)
591-
else:
592-
return self._return_verified_password_or_false_cpu(passwords)
607+
return _dispatch_to_cpu_or_gpu(self, passwords)
593608

594609
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a password
595610
# is correct return it, else return False for item 0; return a count of passwords checked for item 1
@@ -930,11 +945,7 @@ def difficulty_info(self):
930945
# is correct return it, else return False for item 0; return a count of passwords checked for item 1
931946
assert b"1" < b"9" < b"A" < b"Z" < b"a" < b"z" # the b58 check below assumes ASCII ordering in the interest of speed
932947
def return_verified_password_or_false(self, orig_passwords): # Multibit
933-
# Add OpenCL dispatch like other wallet types
934-
if not isinstance(self.opencl_algo, int):
935-
return self._return_verified_password_or_false_opencl(orig_passwords)
936-
else:
937-
return self._return_verified_password_or_false_cpu(orig_passwords)
948+
return _dispatch_to_cpu_or_gpu(self, orig_passwords)
938949

939950
def _return_verified_password_or_false_cpu(self, orig_passwords): # Multibit
940951
# Copy a few globals into local for a small speed boost
@@ -2057,8 +2068,7 @@ def difficulty_info(self):
20572068
return "1024 PBKDF2-SHA512 iterations + ECC"
20582069

20592070
def return_verified_password_or_false(self, passwords): # Electrum28
2060-
return self._return_verified_password_or_false_opencl(passwords) if (not isinstance(self.opencl_algo,int)) \
2061-
else self._return_verified_password_or_false_cpu(passwords)
2071+
return _dispatch_to_cpu_or_gpu(self, passwords)
20622072

20632073
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a password
20642074
# is correct return it, else return False for item 0; return a count of passwords checked for item 1
@@ -2417,8 +2427,7 @@ def check_blockchain_decrypted_block(self, unencrypted_block, password):
24172427
return False
24182428

24192429
def return_verified_password_or_false(self, passwords): # Blockchain.com Main Password
2420-
return self._return_verified_password_or_false_opencl(passwords) if (not isinstance(self.opencl_algo,int)) \
2421-
else self._return_verified_password_or_false_cpu(passwords)
2430+
return _dispatch_to_cpu_or_gpu(self, passwords)
24222431

24232432
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a password
24242433
# is correct return it, else return False for item 0; return a count of passwords checked for item 1
@@ -2676,8 +2685,7 @@ def difficulty_info(self):
26762685
return ("{:,}".format(self._iter_count) if self._iter_count else "1-10") + " SHA-256 iterations"
26772686

26782687
def return_verified_password_or_false(self, passwords): # Blockchain.com second Password
2679-
return self._return_verified_password_or_false_opencl(passwords) if (not isinstance(self.opencl_algo,int)) \
2680-
else self._return_verified_password_or_false_cpu(passwords)
2688+
return _dispatch_to_cpu_or_gpu(self, passwords)
26812689

26822690
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a password
26832691
# is correct return it, else return False for item 0; return a count of passwords checked for item 1
@@ -3155,8 +3163,7 @@ def check_decrypted_block(self, unencrypted_block, password):
31553163
return False
31563164

31573165
def return_verified_password_or_false(self, passwords): # dogechain.info Main Password
3158-
return self._return_verified_password_or_false_opencl(passwords) if (not isinstance(self.opencl_algo, int)) \
3159-
else self._return_verified_password_or_false_cpu(passwords)
3166+
return _dispatch_to_cpu_or_gpu(self, passwords)
31603167

31613168
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a password
31623169
# is correct return it, else return False for item 0; return a count of passwords checked for item 1
@@ -3482,8 +3489,7 @@ def dump_wallet(self,key):
34823489
logfile.write(json.dumps(decrypted_vault_json))
34833490

34843491
def return_verified_password_or_false(self, passwords): # Metamask
3485-
return self._return_verified_password_or_false_opencl(passwords) if (not isinstance(self.opencl_algo, int)) \
3486-
else self._return_verified_password_or_false_cpu(passwords)
3492+
return _dispatch_to_cpu_or_gpu(self, passwords)
34873493

34883494
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a password
34893495
# is correct return it, else return False for item 0; return a count of passwords checked for item 1
@@ -3931,8 +3937,7 @@ def difficulty_info(self):
39313937
return "sCrypt N=14, r=8, p=8"
39323938

39333939
def return_verified_password_or_false(self, passwords): # BIP38 Encrypted Private Keys
3934-
return self._return_verified_password_or_false_opencl(passwords) if (not isinstance(self.opencl_algo,int)) \
3935-
else self._return_verified_password_or_false_cpu(passwords)
3940+
return _dispatch_to_cpu_or_gpu(self, passwords)
39363941

39373942
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a password
39383943
# is correct return it, else return False for item 0; return a count of passwords checked for item 1
@@ -4065,8 +4070,7 @@ def difficulty_info(self):
40654070
return "2048 PBKDF2-SHA512 iterations + ECC"
40664071

40674072
def return_verified_password_or_false(self, mnemonic_ids_list): # BIP39-Passphrase
4068-
return self._return_verified_password_or_false_opencl(mnemonic_ids_list) if (self.opencl and not isinstance(self.opencl_algo,int)) \
4069-
else self._return_verified_password_or_false_cpu(mnemonic_ids_list)
4073+
return _dispatch_to_cpu_or_gpu(self, mnemonic_ids_list)
40704074

40714075
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a password
40724076
# is correct return it, else return False for item 0; return a count of passwords checked for item 1
@@ -4252,8 +4256,7 @@ def difficulty_info(self):
42524256
return "40,000 PBKDF2-SHA256 iterations + ECC"
42534257

42544258
def return_verified_password_or_false(self, mnemonic_ids_list): # BIP39-Passphrase
4255-
return self._return_verified_password_or_false_opencl(mnemonic_ids_list) if (self.opencl and not isinstance(self.opencl_algo,int)) \
4256-
else self._return_verified_password_or_false_cpu(mnemonic_ids_list)
4259+
return _dispatch_to_cpu_or_gpu(self, mnemonic_ids_list)
42574260

42584261
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a password
42594262
# is correct return it, else return False for item 0; return a count of passwords checked for item 1
@@ -4362,9 +4365,7 @@ def difficulty_info(self):
43624365
return "4096 PBKDF2-SHA512 iterations (2048 for Ledger)"
43634366

43644367
def return_verified_password_or_false(self, mnemonic_ids_list): # BIP39-Passphrase
4365-
return self._return_verified_password_or_false_opencl(mnemonic_ids_list) if (
4366-
self.opencl and not isinstance(self.opencl_algo, int)) \
4367-
else self._return_verified_password_or_false_cpu(mnemonic_ids_list)
4368+
return _dispatch_to_cpu_or_gpu(self, mnemonic_ids_list)
43684369

43694370
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a password
43704371
# is correct return it, else return False for item 0; return a count of passwords checked for item 1
@@ -4633,8 +4634,7 @@ def difficulty_info(self):
46334634
return "19162 PBKDF2-SHA512 iterations + ChaCha20_Poly1305"
46344635

46354636
def return_verified_password_or_false(self, mnemonic_ids_list): # Yoroi Cadano Wallet
4636-
return self._return_verified_password_or_false_opencl(mnemonic_ids_list) if (self.opencl and not isinstance(self.opencl_algo,int)) \
4637-
else self._return_verified_password_or_false_cpu(mnemonic_ids_list)
4637+
return _dispatch_to_cpu_or_gpu(self, mnemonic_ids_list)
46384638

46394639
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a password
46404640
# is correct return it, else return False for item 0; return a count of passwords checked for item 1
@@ -4783,8 +4783,7 @@ def difficulty_info(self):
47834783
return "1 SHA-256 iteration"
47844784

47854785
def return_verified_password_or_false(self, password_list): # Brainwallet
4786-
return self._return_verified_password_or_false_opencl(password_list) if (self.opencl and not isinstance(self.opencl_algo,int)) \
4787-
else self._return_verified_password_or_false_cpu(password_list)
4786+
return _dispatch_to_cpu_or_gpu(self, password_list)
47884787

47894788
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a password
47904789
# is correct return it, else return False for item 0; return a count of passwords checked for item 1

0 commit comments

Comments
 (0)