You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在哈希破解程序的for循环中实现多进程处理?

Adding Multiprocessing to Your Hash Cracking Script

Got it, let's get your hash cracker running with multiprocessing to speed things up. First, let's fix a tiny syntax bug in your original code—you're missing a closing quote in hashlib.md5(msg.encode('utf-8)); that should be hashlib.md5(msg.encode('utf-8')).hexdigest(). Then we'll tackle the parallel processing part.

Key Issues in the Original Code for Multiprocessing

  • Global hash_list isn't process-safe: Each process gets its own copy of the list, so when one process removes a hash, the others won't see that change. This leads to duplicate work and incorrect results.
  • Directly modifying a global list in crack: This isn't reliable across processes—we need a shared, thread-safe data structure instead.

Revised Code with Multiprocessing

Here's how to adapt your script using Python's multiprocessing module, with a shared set for hash values and a process pool to distribute the work:

import string
import hashlib
import itertools
from multiprocessing import Pool, Manager

def crack(msg, shared_hashes):
    # Calculate MD5 hash of the candidate message
    temp = hashlib.md5(msg.encode('utf-8')).hexdigest()
    # Check if the hash exists in our shared set
    if temp in shared_hashes:
        print(f"Found match: {msg} -> {temp}")
        # Remove the hash from the shared set so other processes don't waste time on it
        shared_hashes.remove(temp)

if __name__ == '__main__':
    # Replace with your actual hash list
    hash_list = ["d41d8cd98f00b204e9800998ecf8427e", "098f6bcd4621d373cade4e832627b4f6"]
    
    allchar = string.ascii_letters + string.digits + string.punctuation
    
    # Use a Manager to create a process-safe shared set
    with Manager() as manager:
        shared_hashes = manager.set(hash_list)
        
        n = 1
        # Create a process pool (defaults to number of CPU cores)
        with Pool() as pool:
            while shared_hashes:
                # Generate all candidate messages of length n
                candidates = (''.join(result) for result in itertools.product(allchar, repeat=n))
                
                # Map each candidate to the crack function, passing the shared hash set
                # starmap lets us pass multiple arguments to the crack function
                pool.starmap(crack, ((candidate, shared_hashes) for candidate in candidates))
                
                n += 1

What's Changed & Why

  1. Shared Hash Set: We use Manager().set() instead of a regular list. This creates a synchronized set that all processes can access and modify safely—when one process removes a hash, all others see the update immediately.
  2. Process Pool: Pool() creates a pool of worker processes (one per CPU core by default) to handle candidate passwords in parallel. starmap lets us pass multiple arguments to the crack function (the candidate message and the shared set).
  3. Refactored crack: The function now takes the shared hash set as an argument instead of relying on a global variable, making it self-contained and safe for parallel use.
  4. Early Termination: The loop stops as soon as shared_hashes is empty, so we don't waste time generating candidates for longer lengths once all hashes are cracked.

Bonus Tips

  • If you want to limit the number of processes (e.g., use 4 cores instead of all available), pass processes=4 to Pool().
  • For very large hash lists, consider using a more efficient data structure or pre-filtering hashes to reduce lookup time.
  • Be aware that multiprocessing has some overhead—this will shine most when you're cracking multiple hashes or dealing with longer candidate passwords.

内容的提问来源于stack exchange,提问作者Yufeng Li

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 07:05:01