Author

Topic: How to generate bitcoin hashing message to be signed with openssl (Read 120 times)

jr. member
Activity: 76
Merit: 1
   $messageHash = hash("sha256",hash("sha256",strigToBinary("\x18Bitcoin Signed Message:\n".numToVarIntString(strlen($msg)).$msg)));
   file_put_contents("msg.sha256",$messageHash);

This still not generating the expected signature.
jr. member
Activity: 76
Merit: 1
Still something missing on

   file_put_contents("tmp.msg","Bitcoin Signed Message:\n".$msg);   
   exec("openssl dgst -sha256 tmp.msg > msg.sha256");

Now seems some charachters or any other form to create msg.sha256 to sign is missing... still searching for a solution.
Thank you Red-Apple
hero member
Activity: 1470
Merit: 655
it is probably because you didn't attach the fixed string to the message before hashing it. the message is "Bitcoin Signed Message:\n" (\n is the new line byte) add that before your message and then hash it twice with SHA256
jr. member
Activity: 76
Merit: 1
Hi,

I have the private key 7a01628988d23fae697fa05fcdae5a82fe4f749aa9f24d35d23f81bee917dfc3 that I transform into privkey.pem and pubkey.pem with:

Code:
function pktopu($hash){
$hexadecimal_string="302e0201010420".$hash."a00706052b8104000a";
file_put_contents("$hash.pem","-----BEGIN EC PARAMETERS-----
BgUrgQQACg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
".chunk_split(base64_encode(pack('H*',$hexadecimal_string)),64,"\n")."-----END EC PRIVATE KEY-----\n");
$out = array();
exec("openssl ec -conv_form compressed -in $hash.pem -text -noout",$out);
exec("openssl ec -in privkey.pem -pubout > pubkey.pem");
return str_replace(':', '', str_replace(' ', '', $out[6].$out[7].$out[8]));
  }

Extracted public key is: 03359ac0aa241b1a40fcab68486f8a4b546ad3301d201c3645487093578592ec8f
Seems to work.

Now I wish to sign a message: iSignedThisDataThatIs256BitsLong
In order to recieve the signature.

How I have to process this message and signature?

   file_put_contents("tmp.msg",$msg);
   exec("openssl dgst -sha256 tmp.msg > msg.sha256");

   file_put_contents("tmp.msg",$msg."\n");
   exec("openssl dgst -sha256 tmp.msg > msg.sha256");

   file_put_contents("tmp.msg",$msg."\n");
   exec("openssl dgst -sha256 tmp.msg > msg.sha256");
   $shafile = file_get_contents("msg.sha256");
   $shafile=trim(str_replace("SHA256(tmp.msg)=","",$shafile));
   file_put_contents("msg.sha256",hex2bin($shafile));

ETC.

The question is how I create a file msg.sha256 from message to be signed in order to obtain the right signature?

Any combination don't give me the right signature with:

   exec("openssl dgst -sha256 -sign $hash.pem -out tmp.sig msg.sha256",$out);
   exec("openssl dgst -sha256 -hex -sign $hash.pem -out tmp.sig.hex msg.sha256",$out);   
   exec("openssl dgst -sha256 -verify pubkey.pem -signature tmp.sig msg.sha256",$out);

signature must be: 304402205587dfc87c3227ad37b021c08c873ca4b1faada1a83f666d483711edb2f4f743022004e e40d9fe8dd03e6d42bfc7d0e53f75286125a591ed14b39265978ebf3eea36
Jump to: