forked from Someguy123/-Coin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbtc_address_validator.php
77 lines (65 loc) · 1.92 KB
/
btc_address_validator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
class Btc_address_validator {
/**
* [validate description]
* @param String $address BTC Address string
* @return Boolean validation result
*/
public function validate($address)
{
$addr = $this->decode_base58($address);
if (strlen($addr) != 50)
{
return false;
}
$check = substr($addr, 0, strlen($addr) - 8);
$check = pack("H*", $check);
$check = strtoupper(hash("sha256", hash("sha256", $check, true)));
$check = substr($check, 0, 8);
return $check == substr($addr, strlen($addr) - 8);
}
private function encode_hex($dec)
{
$hexchars = "0123456789ABCDEF";
$return = "";
while (bccomp($dec, 0) == 1)
{
$dv = (string) bcdiv($dec, "16", 0);
$rem = (integer) bcmod($dec, "16");
$dec = $dv;
$return = $return . $hexchars[$rem];
}
return strrev($return);
}
/**
* Convert a Base58-encoded integer into the equivalent hex string representation
*
* @param string $base58
* @return string
* @access private
*/
private function decode_base58($base58)
{
$origbase58 = $base58;
$base58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
$return = "0";
for ($i = 0; $i < strlen($base58); $i++)
{
$current = (string) strpos($base58chars, $base58[$i]);
$return = (string) bcmul($return, "58", 0);
$return = (string) bcadd($return, $current, 0);
}
$return = $this->encode_hex($return);
//leading zeros
for ($i = 0; $i < strlen($origbase58) && $origbase58[$i] == "1"; $i++)
{
$return = "00" . $return;
}
if (strlen($return) % 2 != 0)
{
$return = "0" . $return;
}
return $return;
}
}
?>