Bitcoin Forum
September 22, 2025, 11:40:06 AM *
News: Latest Bitcoin Core release: 29.0 [Torrent]
 
  Home Help Search Login Register More  
  Show Posts
Pages: [1]
1  Other / Beginners & Help / Re: How to hash the midstate? on: June 05, 2015, 11:42:41 PM
It appears that King changed the data string into a byte array somewhere else in his program before feeding it to the Sha256.write command, but I never figured out how to make that conversion.  Using the Sha256.print command to take the String directly does not seem to work.  Any suggestions?  The part of my code pertaining to midstate calc is below.  Alternately, pointing me to a code example that correctly makes a byte array out of the getwork data string without using a bunch of libraries that don't work in Arduino IDE would help a lot. 

As with King, just doing this for fun and learning, no dillusions that an Arduino is effective mining hardware.

Thanks.

Code:
#include "sha256.h"

void setup() {
    Serial.begin(57600);
}

void loop() {
    String data = "00000001c570c4764aadb3f09895619f549000b8b51a789e7f58ea750000709700000000103ca064f8c76c390683f8203043e91466a7fcc40e6ebc428fbcc2d89b574a864db8345b1b00b5ac00000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000";  
    data = RemovePadding(data);
    data = EndianFlip32BitChunks(data);  //flips from little endian to big endian

    Sha256.init();
    Sha256.print(data.substring(0,80)); //hash first half to make midstate
    uint8_t* midstatearray = Sha256.result();
    Serial.println(F("Calc midstate is:"));
    printHash(midstatearray);
    Serial.println(F("Expected:"));
    Serial.println(F("e772fc6964e7b06d8f855a6166353e48b2562de4ad037abc889294cea8ed1070"));
}

void printHash(uint8_t* hash) {
  int i;
  for (i=0; i<32; ++i) {
    Serial.print("0123456789abcdef"[hash[i]>>4]);
    Serial.print("0123456789abcdef"[hash[i]&0xf]);
  }
  Serial.println();
}

static String EndianFlip32BitChunks(String input)
        {
            //32 bits = 4*4 bytes = 4*4*2 chars
            String result = "";
            for (int i = 0; i < input.length(); i += 8)
                for (int j = 0; j < 8; j += 2)
                {
                    //append byte (2 chars)
                    result += input[i - j + 6];
                    result += input[i - j + 7];
                }
            return result;        
        }
        
 static String RemovePadding(String input)
        {
            //payload length: final 64 bits in big-endian - 0x0000000000000280 = 640 bits = 80 bytes = 160 chars
            return input.substring(0, 160);
        }
Pages: [1]
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!