Post
Topic
Board Development & Technical Discussion
Re: What exactly is the midstate? (trying to decode the json data in perl)
by
secmff
on 06/04/2011, 17:07:14 UTC

I found two issues with that perl code. You need the first 80 bytes, not 0x80. Second, calculate back to big-endian to get the same result as the miner:

Code:
#!/usr/bin/perl
use Digest::SHA qw/ sha256 /;

$raw =  pack 'H*', '00000001258124a1e0837367309ed9433af69c741513067793bf1f490000c0c800000000f2a45d9b1294bf78d27fe1d77558fbedf2b1eb37bb5f1808d7b77e33d809b8fb4d1c26d21b04864c01000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000';
$short = substr($raw, 0, 80); # first 80 byte

$short_le = pack "N*", unpack "V*", $short; # to Little Endian

print "short_le:", unpack("H*", $short_le), "\n";

$hash1 = sha256($short_le);
$hash = sha256(sha256($short_le));

$hash1_be = pack "N*", unpack("V*", $hash1); # BE again
$hash_be  = pack "N*", unpack("V*", $hash);
print "hash1:  ", unpack("H*", $hash1_be), "\n";
print "hash:   ", unpack("H*", $hash_be), "\n";