vince-scarpa /
responsibleAPI
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * ================================== |
||
| 4 | * Responsible PHP API |
||
| 5 | * ================================== |
||
| 6 | * |
||
| 7 | * @link Git https://github.com/vince-scarpa/responsibleAPI.git |
||
| 8 | * |
||
| 9 | * @api Responible API |
||
| 10 | * @package responsible\core\keys |
||
| 11 | * |
||
| 12 | * @author Vince scarpa <[email protected]> |
||
| 13 | * |
||
| 14 | */ |
||
| 15 | namespace responsible\core\keys; |
||
| 16 | |||
| 17 | class key |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * [accountIdGenerate Generate a user account id] |
||
| 21 | * @return string |
||
| 22 | */ |
||
| 23 | public function accountIdGenerate($length = 8) |
||
| 24 | { |
||
| 25 | $uq = uniqid(mt_rand(), true); |
||
| 26 | $sb = substr($uq, 0, $length); |
||
| 27 | $hd = hexdec($sb); |
||
| 28 | return substr($hd, 0, $length); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * [apiKeyGenerate Generate an API key for the new user account] |
||
| 33 | * @param integer $length |
||
| 34 | * @return string |
||
| 35 | */ |
||
| 36 | public function apiKeyGenerate($length = 32) |
||
| 37 | { |
||
| 38 | $randInt = microtime().rand(1000, 9999); |
||
| 39 | $messageDigest = md5($randInt); |
||
| 40 | return implode( |
||
| 41 | '-', |
||
| 42 | str_split(substr(strtolower($messageDigest), 0, $length), 6) |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 43 | ); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * [secretGenerate Generate a strong secret key] |
||
| 48 | * @return string |
||
| 49 | */ |
||
| 50 | public function secretGenerate($length = 32) |
||
| 51 | { |
||
| 52 | $chars = |
||
| 53 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . |
||
| 54 | '0123456789`-=!@#$%^&*()_+,./?;[]{}\|' |
||
| 55 | ; |
||
| 56 | |||
| 57 | $str = ''; |
||
| 58 | $max = strlen($chars) - 1; |
||
| 59 | |||
| 60 | for ($i = 0; $i < $length; $i++) { |
||
| 61 | $str .= $chars[mt_rand(0, $max)]; |
||
| 62 | } |
||
| 63 | |||
| 64 | return $str; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * [tokenGenerate This is a JWT this is a pseudo token used for the leaky bucket] |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function tokenGenerate($ACCOUNT_ID, $key = '') |
||
| 72 | { |
||
| 73 | return hash_hmac('sha256', $ACCOUNT_ID, $key); |
||
| 74 | } |
||
| 75 | } |
||
| 76 |