-
Notifications
You must be signed in to change notification settings - Fork 0
/
Random.php
168 lines (137 loc) · 3.66 KB
/
Random.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace Ketut\RandomString;
use Exception;
class Random
{
public const CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public const LENGTH = 12;
public const COUNT = 5;
private ?string $char = null;
private ?int $length = null;
private ?int $count = null;
/**
* Generate block of random string.
*
* @return array
*
* @throws Exception
*/
public function generateBlock(): array
{
$count = $this->count ?: self::COUNT;
$block = [];
for ($i = 0; $i < $count; $i++) {
$block [] = self::generate();
}
return $block;
}
/**
* Generate random string.
*
* @return string
*
* @throws Exception
*/
public function generate(): string
{
$char = $this->char ?: self::CHAR;
$length = $this->length ?: self::LENGTH;
$pieces = self::strBaseTime($char, 6);
$max = mb_strlen($char, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
$pieces .= $char[random_int(0, $max)];
}
return substr($pieces, 0, $length);
}
/**
* @throws Exception
*/
private function strBaseTime(string $char, int $length): string
{
$grpInt = self::grpIntBaseTime($length);
$result = '';
for ($i = 0; $i < $length; ++$i) {
$result .= $char[(int)$grpInt[$i]] ?? '';
}
return $result;
}
/**
* @throws Exception
*/
private function grpIntBaseTime(int $length): string
{
$timecodes = time() - 60;
$result = [];
while ($timecodes !== 0) {
$result[] = chr($timecodes & 0xFF);
$timecodes >>= 8;
}
$data= str_pad(implode('', array_reverse($result)), 8, "\000", STR_PAD_LEFT);
$hash = hash_hmac('sha256', $data, $data, true);
$unpacked = unpack('C*', $hash);
$unpacked !== false || throw new \Exception('Invalid data.');
$hmac = array_values($unpacked);
$offset = ($hmac[count($hmac) - 1] & 0xF);
$code = ($hmac[$offset] & 0x7F) << 24 | ($hmac[$offset + 1] & 0xFF) << 16 | ($hmac[$offset + 2] & 0xFF) << 8 | ($hmac[$offset + 3] & 0xFF);
$otp = $code % (10 ** $length);
return str_pad((string) $otp, $length, '0', STR_PAD_LEFT);
}
/**
* Set lowercase character to this char.
*
* @return $this
*/
public function lowercase(): self
{
$this->char = $this->char . "abcdefghijklmnopqrstuvwxyz";
return $this;
}
/**
* Set uppercase character to this char.
*
* @return $this
*/
public function uppercase(): self
{
$this->char = $this->char . "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return $this;
}
/**
* Set numeric character to this char.
*
* @return $this
*/
public function numeric(): self
{
$this->char = $this->char . "0123456789";
return $this;
}
/**
* Set length result of generate random string.
*
* @return $this
*
* @throws Exception
*/
public function length(int $length): self
{
if (1 > $length)
throw new Exception("Length of random string must be a positive integer");
$this->length = $length;
return $this;
}
/**
* Set count of random string in a block.
*
* @return $this
*
* @throws Exception
*/
public function block(int $count): self
{
if (1 > $count)
throw new Exception("Count of block must be a positive integer");
$this->count = $count;
return $this;
}
}