Bitcoin Forum
May 24, 2024, 01:32:06 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Private Key to WIF?  (Read 689 times)
brovine (OP)
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
November 18, 2013, 06:54:51 PM
 #1

https://en.bitcoin.it/wiki/Wallet_import_format

Trying to follow this example here to do this in PHP, but I get stuck at step 3.

I can't seem to get the same SHA256 hash.

I take the extended key from step 2 "800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D"

and SHA256 it.

I should get "8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592" according to step 3 but I just get "E2E4146A36E9C455CF95A4F259F162C353CD419CC3FD0E69AE36D7D1B6CD2C09"

What am I doing wrong?
DannyHamilton
Legendary
*
Offline Offline

Activity: 3402
Merit: 4656



View Profile
November 18, 2013, 07:15:46 PM
 #2

https://en.bitcoin.it/wiki/Wallet_import_format

Trying to follow this example here to do this in PHP, but I get stuck at step 3.

I can't seem to get the same SHA256 hash.

I take the extended key from step 2 "800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D"

and SHA256 it.

I should get "8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592" according to step 3 but I just get "E2E4146A36E9C455CF95A4F259F162C353CD419CC3FD0E69AE36D7D1B6CD2C09"

What am I doing wrong?

As with everyone who comes here asking this question (I think this is the fifth time I've answered it, and I'm sure others have answered it many times as well), you are hashing the string (an ASCII representation of a hex number) instead of the actual hex number itself.
brovine (OP)
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
November 18, 2013, 07:48:47 PM
 #3

https://en.bitcoin.it/wiki/Wallet_import_format

Trying to follow this example here to do this in PHP, but I get stuck at step 3.

I can't seem to get the same SHA256 hash.

I take the extended key from step 2 "800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D"

and SHA256 it.

I should get "8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592" according to step 3 but I just get "E2E4146A36E9C455CF95A4F259F162C353CD419CC3FD0E69AE36D7D1B6CD2C09"

What am I doing wrong?

As with everyone who comes here asking this question (I think this is the fifth time I've answered it, and I'm sure others have answered it many times as well), you are hashing the string (an ASCII representation of a hex number) instead of the actual hex number itself.

Awesome! Thanks!

I've got it working near perfectly, now. The only issue is that it's messing up on the final character.

Do you by chance know PHP?

Code:
$keyHex = $argv[1];

$alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

$exKey = '80' . $keyHex;

$chksum = substr(strtoupper(hash('sha256', hex2bin(strtoupper(hash('sha256', hex2bin($exKey)))))), 0, 8);
echo "Checksum: " . $chksum . "\n";

$divmod = array(gmp_init($exKey . $chksum, 16), 0);

$out = "";

while (gmp_strval($divmod[0]) >= 58)
{
$divmod = divmod($divmod[0], 58);
$out = $alphabet[gmp_strval($divmod[1])] . $out;
}

echo $out . "\n";

function divmod($x, $y)
{
    $div = gmp_div($x, $y);
    $mod = gmp_mod($x, $y);

    return array($div, $mod);
}

If I plug in "0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D", it outputs "HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ", but it should be outputting "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ" (note the prepending 5)
DannyHamilton
Legendary
*
Offline Offline

Activity: 3402
Merit: 4656



View Profile
November 18, 2013, 08:25:12 PM
 #4

As you exit this loop:

Code:
while (gmp_strval($divmod[0]) >= 58)
{
$divmod = divmod($divmod[0], 58);
$out = $alphabet[gmp_strval($divmod[1])] . $out;
}

$divmod[0] = 4

But you never use that value.  You exit the loop and print out the conversion that you've done so far, but you never calculate the final $alphabet[gmp_strval($divmod[0])]
brovine (OP)
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
November 18, 2013, 09:42:22 PM
 #5

As you exit this loop:

Code:
while (gmp_strval($divmod[0]) >= 58)
{
$divmod = divmod($divmod[0], 58);
$out = $alphabet[gmp_strval($divmod[1])] . $out;
}

$divmod[0] = 4

But you never use that value.  You exit the loop and print out the conversion that you've done so far, but you never calculate the final $alphabet[gmp_strval($divmod[0])]

Oh you're right. I don't understand why, though?

I'm porting it from https://github.com/bkkcoins/misc/blob/master/hexwif/hexwif

and it's pretty much the exact same code, isn't it?

Either way, thanks a lot!
DannyHamilton
Legendary
*
Offline Offline

Activity: 3402
Merit: 4656



View Profile
November 18, 2013, 09:46:35 PM
 #6

I'm porting it from https://github.com/bkkcoins/misc/blob/master/hexwif/hexwif

and it's pretty much the exact same code, isn't it?

Note the last line of the code at that link:

Code:
print alphabet[bn] + out

alphabet[bn] in that code is equivalent to the $alphabet[gmp_strval($divmod[0])] that is missing from your code.
brovine (OP)
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
November 18, 2013, 09:50:02 PM
 #7

I'm porting it from https://github.com/bkkcoins/misc/blob/master/hexwif/hexwif

and it's pretty much the exact same code, isn't it?

Note the last line of the code at that link:

Code:
print alphabet[bn] + out

alphabet[bn] in that code is equivalent to the $alphabet[gmp_strval($divmod[0])] that is missing from your code.

Yeah I just figured out that's what you meant, lol.

Once again, thanks!
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!