View difference between Paste ID: nvmQJBAm and vmRQC7ha
SHOW: | | - or go back to the newest paste.
1
<?php
2
// From http://pastebin.com/vmRQC7ha
3
//hex input must be in uppercase, with no leading 0x
4-
define("ADDRESSVERSION","00"); //this is a hex byte
4+
5
class BitCoinValidator {
6-
function decodeHex($hex)
6+
7-
{
7+
    public $addressversion = "00"; //this is a hex byte
8-
	$hex=strtoupper($hex);
8+
9-
	$chars="0123456789ABCDEF";
9+
    public function __construct($testnet = false)
10-
	$return="0";
10+
    {
11-
	for($i=0;$i<strlen($hex);$i++)
11+
        $this->addressversion = ($testnet) ? "6F" : "00"; //TestNet vs ProductionNet
12-
	{
12+
    }
13-
		$current=(string)strpos($chars,$hex[$i]);
13+
    public function decodeHex($hex)
14-
		$return=(string)bcmul($return,"16",0);
14+
    {
15-
		$return=(string)bcadd($return,$current,0);
15+
        $hex=strtoupper($hex);
16-
	}
16+
        $chars="0123456789ABCDEF";
17-
	return $return;
17+
        $return="0";
18-
}
18+
        for($i=0;$i<strlen($hex);$i++)
19
        {
20-
function encodeHex($dec)
20+
            $current=(string)strpos($chars,$hex[$i]);
21-
{
21+
            $return=(string)bcmul($return,"16",0);
22-
	$chars="0123456789ABCDEF";
22+
            $return=(string)bcadd($return,$current,0);
23-
	$return="";
23+
        }
24-
	while (bccomp($dec,0)==1)
24+
        return $return;
25-
	{
25+
    }
26-
		$dv=(string)bcdiv($dec,"16",0);
26+
27-
		$rem=(integer)bcmod($dec,"16");
27+
    public function encodeHex($dec)
28-
		$dec=$dv;
28+
    {
29-
		$return=$return.$chars[$rem];
29+
        $chars="0123456789ABCDEF";
30-
	}
30+
        $return="";
31-
	return strrev($return);
31+
        while (bccomp($dec,0)==1)
32-
}
32+
        {
33
            $dv=(string)bcdiv($dec,"16",0);
34-
function decodeBase58($base58)
34+
            $rem=(integer)bcmod($dec,"16");
35-
{
35+
            $dec=$dv;
36-
	$origbase58=$base58;
36+
            $return=$return.$chars[$rem];
37-
	
37+
        }
38-
	$chars="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
38+
        return strrev($return);
39-
	$return="0";
39+
    }
40-
	for($i=0;$i<strlen($base58);$i++)
40+
41-
	{
41+
    public function decodeBase58($base58)
42-
		$current=(string)strpos($chars,$base58[$i]);
42+
    {
43-
		$return=(string)bcmul($return,"58",0);
43+
        $origbase58=$base58;
44-
		$return=(string)bcadd($return,$current,0);
44+
        
45-
	}
45+
        $chars="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
46-
	
46+
        $return="0";
47-
	$return=encodeHex($return);
47+
        for($i=0;$i<strlen($base58);$i++)
48-
	
48+
        {
49-
	//leading zeros
49+
            $current=(string)strpos($chars,$base58[$i]);
50-
	for($i=0;$i<strlen($origbase58)&&$origbase58[$i]=="1";$i++)
50+
            $return=(string)bcmul($return,"58",0);
51-
	{
51+
            $return=(string)bcadd($return,$current,0);
52-
		$return="00".$return;
52+
        }
53-
	}
53+
        
54-
	
54+
        $return=$this->encodeHex($return);
55-
	if(strlen($return)%2!=0)
55+
        
56-
	{
56+
        //leading zeros
57-
		$return="0".$return;
57+
        for($i=0;$i<strlen($origbase58)&&$origbase58[$i]=="1";$i++)
58-
	}
58+
        {
59-
	
59+
            $return="00".$return;
60-
	return $return;
60+
        }
61-
}
61+
        
62
        if(strlen($return)%2!=0)
63-
function encodeBase58($hex)
63+
        {
64-
{
64+
            $return="0".$return;
65-
	if(strlen($hex)%2!=0)
65+
        }
66-
	{
66+
        
67-
		die("encodeBase58: uneven number of hex characters");
67+
        return $return;
68-
	}
68+
    }
69-
	$orighex=$hex;
69+
70-
	
70+
    public function encodeBase58($hex)
71-
	$chars="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
71+
    {
72-
	$hex=decodeHex($hex);
72+
        if(strlen($hex)%2!=0)
73-
	$return="";
73+
        {
74-
	while (bccomp($hex,0)==1)
74+
            die("encodeBase58: uneven number of hex characters");
75-
	{
75+
        }
76-
		$dv=(string)bcdiv($hex,"58",0);
76+
        $orighex=$hex;
77-
		$rem=(integer)bcmod($hex,"58");
77+
        
78-
		$hex=$dv;
78+
        $chars="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
79-
		$return=$return.$chars[$rem];
79+
        $hex=$this->decodeHex($hex);
80-
	}
80+
        $return="";
81-
	$return=strrev($return);
81+
        while (bccomp($hex,0)==1)
82-
	
82+
        {
83-
	//leading zeros
83+
            $dv=(string)bcdiv($hex,"58",0);
84-
	for($i=0;$i<strlen($orighex)&&substr($orighex,$i,2)=="00";$i+=2)
84+
            $rem=(integer)bcmod($hex,"58");
85-
	{
85+
            $hex=$dv;
86-
		$return="1".$return;
86+
            $return=$return.$chars[$rem];
87-
	}
87+
        }
88-
	
88+
        $return=strrev($return);
89-
	return $return;
89+
        
90-
}
90+
        //leading zeros
91
        for($i=0;$i<strlen($orighex)&&substr($orighex,$i,2)=="00";$i+=2)
92-
function hash160ToAddress($hash160,$addressversion=ADDRESSVERSION)
92+
        {
93-
{
93+
            $return="1".$return;
94-
	$hash160=$addressversion.$hash160;
94+
        }
95-
	$check=pack("H*" , $hash160);
95+
        
96-
	$check=hash("sha256",hash("sha256",$check,true));
96+
        return $return;
97-
	$check=substr($check,0,8);
97+
    }
98-
	$hash160=strtoupper($hash160.$check);
98+
99-
	return encodeBase58($hash160);
99+
    public function hash160ToAddress($hash160)
100-
}
100+
    {
101
        $hash160=$this->addressversion.$hash160;
102-
function addressToHash160($addr)
102+
        $check=pack("H*" , $hash160);
103-
{
103+
        $check=hash("sha256",hash("sha256",$check,true));
104-
	$addr=decodeBase58($addr);
104+
        $check=substr($check,0,8);
105-
	$addr=substr($addr,2,strlen($addr)-10);
105+
        $hash160=strtoupper($hash160.$check);
106-
	return $addr;
106+
        return $this->encodeBase58($hash160);
107-
}
107+
    }
108
109-
function checkAddress($addr,$addressversion=ADDRESSVERSION)
109+
    public function addressToHash160($addr)
110-
{
110+
    {
111-
	$addr=decodeBase58($addr);
111+
        $addr=$this->decodeBase58($addr);
112-
	if(strlen($addr)!=50)
112+
        $addr=substr($addr,2,strlen($addr)-10);
113-
	{
113+
        return $addr;
114-
		return false;
114+
    }
115-
	}
115+
116-
	$version=substr($addr,0,2);
116+
    public function _checkAddress($addr)
117-
	if(hexdec($version)>hexdec($addressversion))
117+
    {
118-
	{
118+
        $addr=$this->decodeBase58($addr);
119-
		return false;
119+
        if(strlen($addr)!=50)
120-
	}
120+
        {
121-
	$check=substr($addr,0,strlen($addr)-8);
121+
            return false;
122-
	$check=pack("H*" , $check);
122+
        }
123-
	$check=strtoupper(hash("sha256",hash("sha256",$check,true)));
123+
        $version=substr($addr,0,2);
124-
	$check=substr($check,0,8);
124+
        if(hexdec($version)>hexdec($this->addressversion))
125-
	return $check==substr($addr,strlen($addr)-8);
125+
        {
126-
}
126+
            return false;
127
        }
128-
function hash160($data)
128+
        $check=substr($addr,0,strlen($addr)-8);
129-
{
129+
        $check=pack("H*" , $check);
130-
	$data=pack("H*" , $data);
130+
        $check=strtoupper(hash("sha256",hash("sha256",$check,true)));
131-
	return strtoupper(hash("ripemd160",hash("sha256",$data,true)));
131+
        $check=substr($check,0,8);
132-
}
132+
        return $check==substr($addr,strlen($addr)-8);
133
    }
134-
function pubKeyToAddress($pubkey)
134+
135-
{
135+
    public function hash160($data)
136-
	return hash160ToAddress(hash160($pubkey));
136+
    {
137-
}
137+
        $data=pack("H*" , $data);
138
        return strtoupper(hash("ripemd160",hash("sha256",$data,true)));
139-
function remove0x($string)
139+
    }
140-
{
140+
141-
	if(substr($string,0,2)=="0x"||substr($string,0,2)=="0X")
141+
    public function pubKeyToAddress($pubkey)
142-
	{
142+
    {
143-
		$string=substr($string,2);
143+
        return $this->hash160ToAddress($this->hash160($pubkey));
144-
	}
144+
    }
145-
	return $string;
145+
146-
}
146+
    public function remove0x($string)
147
    {
148-
?>
148+
        if(substr($string,0,2)=="0x"||substr($string,0,2)=="0X")
149
        {
150
            $string=substr($string,2);
151
        }
152
        return $string;
153
    }
154
    public static function checkAddress($addr, $testnet = false)
155
    {
156
        $obj = new self($testnet);
157
        return $obj->_checkAddress($addr);
158
    }
159
}