user6915: I'm trying to figure out the internals of hashing. I've read through a lot of the documentation but unfortunately most of it tends to gloss over the details. In fact I've yet to find a complete and accurate portrayal of a hashing procedure in an easy-to-read language like Python. So I thought I would try to do it myself. I am starting off by trying to verify the "midstate" that comes in every call to getwork (although soon to be removed completely as I understand).
I have been referencing this example in Go: https://stackoverflow.com/questions/9245235/golang-midstate-sha-256-hash/9322486#9322486
So I just hardcoded in the first half of the "data" field which would be returned by the getwork call
So here's what I've got so far:
#!/usr/bin/env python
import json
import binascii
import hashlib

#This function takes a hex string and reverses the order of bytes in every slice of 4 bytes
def hexbyteswap(string):
  swapped_string = []
  n = 8
  split_string = [string[i:i+n] for i in range(0, len(string),n)]
  for piece in split_string:
    binary = binascii.unhexlify(piece)
    binary_r = binary[::-1]
    swapped_string.append(binascii.hexlify(binary_r))
  return "".join(swapped_string)

#This is the first half of the data string, i.e. the first 64 bytes of the data field
fhalf="00000001c570c4764aadb3f09895619f549000b8b51a789e7f58ea750000709700000000103ca064f8c76c390683f8203043e91466a7fcc40e6ebc428fbcc2d8"

# run the byteswap
fhalf_bin_r_hex = hexbyteswap(fhalf)

#These following steps are where I'm having the problem
print hashlib.sha256(binascii.unhexlify(fhalf_bin_r_hex)).hexdigest()
print hashlib.sha256(fhalf_bin_r_hex).hexdigest()

So according to the Go article referenced earlier, I should get this out of my SHA function:
69FC72E76DB0E764615A858F483E3566E42D56B2BC7A03ADCE9492887010EDA8

Then if I run the byte swap again I will get the real midstate which is:
e772fc6964e7b06d8f855a6166353e48b2562de4ad037abc889294cea8ed1070

But I am NOT getting anything resembling that at all. I've tried two ways first putting the hex string into the Sha function and getting:
d3092f90cd1940d050b7a6f43063b081a82bc43c7fe202a5920721b858462a49

Then I also tried running unhexlify first and putting it into Sha (shown in the code above) which yields:
b55c86ea0314d1dea431923d46a18feed832f15ad535256516b796f6909e0dd0

Neither one of these is correct. What am I missing?

user10643: Trying to find this answer myself.  Noticed that the stratum_proxy is written in python and thus has the answer we are looking for.
https://github.com/slush0/stratum-mining-proxy/blob/master/mining_libs/midstate.py

6915: <p>I'm trying to figure out the internals of hashing. I've read through a lot of the documentation but unfortunately most of it tends to gloss over the details. In fact I've yet to find a complete and accurate portrayal of a hashing procedure in an easy-to-read language like Python. So I thought I would try to do it myself. I am starting off by trying to verify the "midstate" that comes in every call to getwork (although soon to be removed completely as I understand).</p>

<p>I have been referencing this example in Go: <a href="https://stackoverflow.com/questions/9245235/golang-midstate-sha-256-hash/9322486#9322486">https://stackoverflow.com/questions/9245235/golang-midstate-sha-256-hash/9322486#9322486</a></p>

<p>So I just hardcoded in the first half of the "data" field which would be returned by the getwork call</p>

<p>So here's what I've got so far:</p>

<pre><code>#!/usr/bin/env python
import json
import binascii
import hashlib

#This function takes a hex string and reverses the order of bytes in every slice of 4 bytes
def hexbyteswap(string):
  swapped_string = []
  n = 8
  split_string = [string[i:i+n] for i in range(0, len(string),n)]
  for piece in split_string:
    binary = binascii.unhexlify(piece)
    binary_r = binary[::-1]
    swapped_string.append(binascii.hexlify(binary_r))
  return "".join(swapped_string)

#This is the first half of the data string, i.e. the first 64 bytes of the data field
fhalf="00000001c570c4764aadb3f09895619f549000b8b51a789e7f58ea750000709700000000103ca064f8c76c390683f8203043e91466a7fcc40e6ebc428fbcc2d8"

# run the byteswap
fhalf_bin_r_hex = hexbyteswap(fhalf)

#These following steps are where I'm having the problem
print hashlib.sha256(binascii.unhexlify(fhalf_bin_r_hex)).hexdigest()
print hashlib.sha256(fhalf_bin_r_hex).hexdigest()
</code></pre>

<p>So according to the Go article referenced earlier, I should get this out of my SHA function:</p>

<pre><code>69FC72E76DB0E764615A858F483E3566E42D56B2BC7A03ADCE9492887010EDA8
</code></pre>

<p>Then if I run the byte swap again I will get the real midstate which is:</p>

<pre><code>e772fc6964e7b06d8f855a6166353e48b2562de4ad037abc889294cea8ed1070
</code></pre>

<p>But I am NOT getting anything resembling that at all. I've tried two ways first putting the hex string into the Sha function and getting:</p>

<pre><code>d3092f90cd1940d050b7a6f43063b081a82bc43c7fe202a5920721b858462a49
</code></pre>

<p>Then I also tried running unhexlify first and putting it into Sha (shown in the code above) which yields:</p>

<pre><code>b55c86ea0314d1dea431923d46a18feed832f15ad535256516b796f6909e0dd0
</code></pre>

<p>Neither one of these is correct. What am I missing?</p>
 10643: <p>Trying to find this answer myself.  Noticed that the stratum_proxy is written in python and thus has the answer we are looking for.
<a href="https://github.com/slush0/stratum-mining-proxy/blob/master/mining_libs/midstate.py" rel="nofollow">https://github.com/slush0/stratum-mining-proxy/blob/master/mining_libs/midstate.py</a></p>
