1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Laravel Paystack package. |
5
|
|
|
* |
6
|
|
|
* (c) Prosper Otemuyiwa <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Source http://stackoverflow.com/a/13733588/179104 |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Unicodeveloper\Paystack; |
15
|
|
|
|
16
|
|
|
class TransRef |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Get the pool to use based on the type of prefix hash |
20
|
|
|
* @param string $type |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
private static function getPool($type = 'alnum') |
24
|
|
|
{ |
25
|
|
|
switch ($type) { |
26
|
|
|
case 'alnum': |
27
|
|
|
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
28
|
|
|
break; |
29
|
|
|
case 'alpha': |
30
|
|
|
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
31
|
|
|
break; |
32
|
|
|
case 'hexdec': |
33
|
|
|
$pool = '0123456789abcdef'; |
34
|
|
|
break; |
35
|
|
|
case 'numeric': |
36
|
|
|
$pool = '0123456789'; |
37
|
|
|
break; |
38
|
|
|
case 'nozero': |
39
|
|
|
$pool = '123456789'; |
40
|
|
|
break; |
41
|
|
|
case 'distinct': |
42
|
|
|
$pool = '2345679ACDEFHJKLMNPRSTUVWXYZ'; |
43
|
|
|
break; |
44
|
|
|
default: |
45
|
|
|
$pool = (string) $type; |
46
|
|
|
break; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $pool; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Generate a random secure crypt figure |
54
|
|
|
* @param integer $min |
55
|
|
|
* @param integer $max |
56
|
|
|
* @return integer |
57
|
|
|
*/ |
58
|
|
|
private static function secureCrypt($min, $max) |
59
|
|
|
{ |
60
|
|
|
$range = $max - $min; |
61
|
|
|
|
62
|
|
|
if ($range < 0) { |
63
|
|
|
return $min; // not so random... |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$log = log($range, 2); |
67
|
|
|
$bytes = (int) ($log / 8) + 1; // length in bytes |
68
|
|
|
$bits = (int) $log + 1; // length in bits |
69
|
|
|
$filter = (int) (1 << $bits) - 1; // set all lower bits to 1 |
70
|
|
|
do { |
71
|
|
|
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes))); |
72
|
|
|
$rnd = $rnd & $filter; // discard irrelevant bits |
73
|
|
|
} while ($rnd >= $range); |
74
|
|
|
|
75
|
|
|
return $min + $rnd; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Finally, generate a hashed token |
80
|
|
|
* @param integer $length |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public static function getHashedToken($length = 25) |
84
|
|
|
{ |
85
|
|
|
$token = ""; |
86
|
|
|
$max = strlen(static::getPool()); |
|
|
|
|
87
|
|
|
for ($i = 0; $i < $length; $i++) { |
88
|
|
|
$token .= static::getPool()[static::secureCrypt(0, $max)]; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $token; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: