Bitcoin Forum
April 25, 2024, 02:30:34 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: « 1 [2]  All
  Print  
Author Topic: how to generate a valid private-key + recv address in PHP?  (Read 15453 times)
kjj
Legendary
*
Offline Offline

Activity: 1302
Merit: 1024



View Profile
October 14, 2012, 05:44:29 AM
 #21

Someone asked in a PM, so I wrote an example.

Code:
<?php
$bits_desired
=256;
$bytes_desired=ceil($bits_desired/8);
echo 
"Asking for ".$bits_desired." bits of random (".$bytes_desired." bytes)\n";
if(
TRUE==($fp_ent=fopen("/proc/sys/kernel/random/entropy_avail","r"))){
 
$ent=trim(fgets($fp_ent));
 echo 
"Entropy available: ".$ent."\n";
 if(
$ent>$bits_desired){
  if(
TRUE==($fp_rand=fopen("/dev/random","r"))){
   
$r=fread($fp_rand,$bytes_desired);
   echo 
bin2hex($r)."\n";
  }else echo 
"Failed to open /dev/random.\n";
 }else echo 
"Not enough bits available.\n";
}else echo 
"Unable to get status of entropy pool.\n";
?>


17Np17BSrpnHCZ2pgtiMNnhjnsWJ2TMqq8
I routinely ignore posters with paid advertising in their sigs.  You should too.
There are several different types of Bitcoin clients. The most secure are full nodes like Bitcoin Core, but full nodes are more resource-heavy, and they must do a lengthy initial syncing process. As a result, lightweight clients with somewhat less security are commonly used.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
BCB
CTG
VIP
Legendary
*
Offline Offline

Activity: 1078
Merit: 1002


BCJ


View Profile
October 14, 2012, 05:58:21 AM
 #22

I've been testing importing and extracting and paying key and all seems to be working fine.
Richy_T
Legendary
*
Offline Offline

Activity: 2422
Merit: 2113


1RichyTrEwPYjZSeAYxeiFBNnKC9UjC5k


View Profile
November 25, 2013, 06:58:15 PM
Last edit: November 25, 2013, 07:20:22 PM by Richy_T
 #23

This code generates a mini-key if anyone's interested. I was looking for the next step, priv->pub key when I ran across this thread. It needs tidying of course and I just remembered I'm using the non-crypto rand() so that will need to be fixed too. Maybe I should just delete it Cheesy

Edit: OK, fixed to use /dev/urandom. Use /dev/random if you'd prefer.

Code:
<?php
  
#Below is full list of available characters.
  #"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
  
$fp=fopen("/dev/urandom","r") or die;
  
$available_chars="23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";
  do{
    
$minikey='S';
    for(
$i=0;$i<29;$i++){
      while((
$c=ord(fgetc($fp)))>=strlen($available_chars));
      
$minikey.=substr($available_chars,$c,1);
    }
    
$check=hash('sha256',$minikey '?') . "\n";
  }while(
substr($check,0,2)!='00');
  
fclose($fp);
  
$priv=hash('sha256',$minikey);
  print 
"Minikey: $minikey\n";
  print 
"Privkey: $priv\n";
?>


Note that I deliberately exclude "1' and 'o' but you may add them back in if needed.

1RichyTrEwPYjZSeAYxeiFBNnKC9UjC5k
Richy_T
Legendary
*
Offline Offline

Activity: 2422
Merit: 2113


1RichyTrEwPYjZSeAYxeiFBNnKC9UjC5k


View Profile
November 26, 2013, 03:14:21 PM
 #24

For the challenge and to learn more about Bitcoin, I implemented this in pure PHP (bcmath extension required, but I think that's pretty standard. Edit: Goes much faster with GMP extension!)  Please check it over yourself before using it!  I checked several keypairs with Casascius' address utility and they look good, but I'm not an expert, and there aren't real solid tests of the code.  Most of the work is by a pre-written ECC lib I found.

Anyway, the code, with a demo embedded, is at https://gist.github.com/3549107.  It is a little slow right now but could be sped up by using GMP instead of bcmath.  I don't know if anyone still cares or if the bounty has been claimed, but it'd be nice to have. Smiley

If there's interest, let me know, and I will extend and/or test it better.

Edit: Cleaned up the code and made it use GMP if possible, as it is much much faster.  Also found ways to use the ECC lib's helper functions more, so there is less code.

This disappeared (link no good). Anyone have a copy anywhere?

1RichyTrEwPYjZSeAYxeiFBNnKC9UjC5k
scintill
Sr. Member
****
Offline Offline

Activity: 448
Merit: 252


View Profile WWW
November 30, 2013, 02:00:28 AM
 #25

This disappeared (link no good). Anyone have a copy anywhere?

I removed it, for being embarrassed about its weak key generation (!), and not wanting to enable people to unwittingly generate weak keys.  Also, someone has made a more flexible fork called PHPCoinAddress, but I would recommend at least applying my patch here, in addition to ideally studying secure key generation for your configuration/platform (at least if you are doing something high-risk/high-value in production.)  Feedback on my pull requests to PHPCoinAddress is welcome.  Thanks.

1SCiN5kqkAbxxwesKMsH9GvyWnWP5YK2W | donations
Pages: « 1 [2]  All
  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!