0

In this assignment, we have to iteratively run data through a SHA256 algorithm replacing a number (X) with the final goal of finding a SHA256 Hash that has 11 zeroes in the beginning.

So here is the code I came up with:

for x in range(8388608000000):
    hash = hashlib.sha256((str(bn) + str(x) + str(d)).encode('utf-8')).hexdigest()
    if hash.find("00000000000", 0, 11) > -1:
        print(x)
        print(hash)

This works, but it's too slow in python. Apparently hashlib is compiled but jumping in and out because of the for loop makes things slow. I allready made multiprocessing work but that just doesn't cut it. Is there a way of getting rid of the for loop and using numpy instead somehow in this case?

  1. Approach:
  • Create large array with numpy.arange
  • using numpy.nditer instead of x within hashlib function
  • writing calculated hashes into another array
  • checking if hashes exist for required conditions, printing these
  • iterating the process with a for loop
  1. Approach: I saw this solution for a similar problem although I do not fully understand what it means. Could it be useful in my case?
Pirmin Oe
  • 1
  • 1
  • I doubt that the for-loop is (relatively) slow here: replace the sha256 calculation with a simple calculation (e.g. a sqrt or even a squaring of x, and run it again (probably want to compare with a much smaller range than 8388608000000); include a (different) if statement for completeness sake. You'll find a notable difference in timings, indicating the sha256 calculation is what's really slowing things down. As it should, since it used to be that passwords and keys were hashed/calculated with sha256. – 9769953 Jul 22 '20 at 17:40
  • Given that there exists bitcoin farms, you shouldn't be surprised this method takes a long time. – 9769953 Jul 22 '20 at 17:42
  • `nditer` is not a speed tool; it doesn't compile your `hashlib` call or pass multiple values to it at once. `numpy` speed depends on using the compiled whole-array methods of numpy; it can't be used if the core calculation is a function that only takes one value at a time. – hpaulj Jul 22 '20 at 18:12
  • Can I somehow run this on the GPU then? Numba and @jit does not work unfortunately... – Pirmin Oe Jul 22 '20 at 19:09
  • Python's hashlib will not work on a GPU. You should find a custom implementation. – 9769953 Jul 25 '20 at 02:34

0 Answers0