Bitcoin Forum
May 07, 2024, 07:15:07 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How to generate bitcoin hashing message to be signed with openssl  (Read 126 times)
patinencomun (OP)
Jr. Member
*
Offline Offline

Activity: 76
Merit: 1


View Profile
October 10, 2019, 09:39:56 AM
Last edit: October 10, 2019, 01:58:03 PM by patinencomun
Merited by hugeblack (1)
 #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
According to NIST and ECRYPT II, the cryptographic algorithms used in Bitcoin are expected to be strong until at least 2030. (After that, it will not be too difficult to transition to different algorithms.)
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715109307
Hero Member
*
Offline Offline

Posts: 1715109307

View Profile Personal Message (Offline)

Ignore
1715109307
Reply with quote  #2

1715109307
Report to moderator
1715109307
Hero Member
*
Offline Offline

Posts: 1715109307

View Profile Personal Message (Offline)

Ignore
1715109307
Reply with quote  #2

1715109307
Report to moderator
1715109307
Hero Member
*
Offline Offline

Posts: 1715109307

View Profile Personal Message (Offline)

Ignore
1715109307
Reply with quote  #2

1715109307
Report to moderator
Red-Apple
Hero Member
*****
Offline Offline

Activity: 1470
Merit: 655


View Profile
October 10, 2019, 10:05:01 AM
 #2

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

--signature space for rent; sent PM--
patinencomun (OP)
Jr. Member
*
Offline Offline

Activity: 76
Merit: 1


View Profile
October 10, 2019, 11:18:33 AM
Last edit: October 10, 2019, 11:51:51 AM by patinencomun
 #3

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
patinencomun (OP)
Jr. Member
*
Offline Offline

Activity: 76
Merit: 1


View Profile
October 10, 2019, 12:56:58 PM
 #4

   $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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!