-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathApiKeyGenerator.php
More file actions
29 lines (23 loc) · 875 Bytes
/
ApiKeyGenerator.php
File metadata and controls
29 lines (23 loc) · 875 Bytes
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
<?php
namespace Uecode\Bundle\ApiKeyBundle\Util;
/**
* @author Gennady Telegin <gtelegin@gmail.com>
*/
class ApiKeyGenerator implements ApiKeyGeneratorInterface
{
private $characterSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
private $apiKeyLength = 64;
public function generateApiKey()
{
return self::generate($this->characterSet, $this->apiKeyLength);
}
public static function generate($characterSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', $apiKeyLength = 64)
{
$characterSetLength = strlen($characterSet);
$apikey = '';
for ($i = 0; $i < $apiKeyLength; ++$i) {
$apikey .= $characterSet[rand(0, $characterSetLength - 1)];
}
return rtrim(base64_encode(sha1(uniqid('ue' . rand(rand(), rand())) . $apikey)), '=');
}
}