Skip to content

Commit 4e37db0

Browse files
committed
Add secure random generation methods and enhance pool selection for hashed tokens
1 parent 42c6080 commit 4e37db0

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/Concerns/PerformsCharges.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,58 @@ public function refund(string $transaction, array $options = []): mixed
5050
return $response;
5151
}
5252

53+
/**
54+
* Get the pool to use based on the type of prefix hash.
55+
*/
56+
private static function getPool(string $type = 'alnum'): string
57+
{
58+
switch ($type) {
59+
case 'alnum':
60+
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
61+
break;
62+
case 'alpha':
63+
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
64+
break;
65+
case 'hexdec':
66+
$pool = '0123456789abcdef';
67+
break;
68+
case 'numeric':
69+
$pool = '0123456789';
70+
break;
71+
case 'nozero':
72+
$pool = '123456789';
73+
break;
74+
case 'distinct':
75+
$pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
76+
break;
77+
default:
78+
$pool = (string) $type;
79+
break;
80+
}
81+
82+
return $pool;
83+
}
84+
85+
/**
86+
* Generate a random secure crypt figure.
87+
*/
88+
private static function secureCrypt(int $min, int $max): int
89+
{
90+
$range = $max - $min;
91+
92+
if ($range <= 0) {
93+
return $min; // not so random...
94+
}
95+
96+
// Use PHP's cryptographically secure random_int to generate an integer
97+
// in the half-open interval [min, max), matching the previous behavior.
98+
return random_int($min, $max - 1);
99+
}
100+
53101
/**
54102
* Generate a hashed token for use as a unique identifier for a charge or subscription.
55103
*/
56-
public static function getHashedToken(int $length = 25): string
104+
protected static function getHashedToken(int $length = 25): string
57105
{
58106
$token = "";
59107
$max = strlen(static::getPool());

0 commit comments

Comments
 (0)