-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: steevenz | ||
* Date: 05/04/18 | ||
* Time: 10.11 | ||
*/ | ||
|
||
namespace O2System\Security\Generators; | ||
|
||
use O2System\Kernel\Http\Message\Uri; | ||
|
||
/** | ||
* Class MachineId | ||
* | ||
* Generate Machine ID based on System Metadata information in UUID (Universally Unique Identifier) format. | ||
* | ||
* @package O2System\Security\Generators | ||
*/ | ||
class MachineId | ||
{ | ||
/** | ||
* MachineId::generate | ||
* | ||
* @return string | ||
*/ | ||
public static function generate() | ||
{ | ||
if(class_exists('O2System\Filesystem\System')) { | ||
$system = new \O2System\Filesystem\System(); | ||
|
||
$metadata = [ | ||
'machine' => $system->getMachine(), | ||
'operatingSystem' => [ | ||
'name' => $system->getName(), | ||
'version' => $system->getVersion(), | ||
'release' => $system->getRelease() | ||
], | ||
'hostname' => $system->getHostname(), | ||
'cpuCores' => $system->getCpuCores(), | ||
'macAddress' => $system->getMacAddress() | ||
]; | ||
} else { | ||
$metadata = [ | ||
'machine' => php_uname('m'), | ||
'operatingSystem' => [ | ||
'name' => php_uname('s'), | ||
'version' => php_uname('v'), | ||
'release' => php_uname('r') | ||
], | ||
'hostname' => php_uname('n'), | ||
'cpuCores' => 1, | ||
'macAddress' => implode(':', str_split(substr(md5('none'), 0, 12), 2)) | ||
]; | ||
} | ||
|
||
$uri = new Uri(); | ||
|
||
$metadata['domain'] = $uri->getHost(); | ||
$metadata['ipAddress'] = $_SERVER['SERVER_ADDR']; | ||
|
||
$string = json_encode($metadata); | ||
$string = md5($string); | ||
|
||
// Converts to UUID (Universally Unique Identifier) | ||
$parts[] = substr($string, 0, 8); | ||
$parts[] = substr($string, 8, 4); | ||
$parts[] = substr($string, 12, 4); | ||
$parts[] = substr($string, 16, 4); | ||
$parts[] = substr($string, 20, 12); | ||
|
||
$parts = array_map('strtoupper', $parts); | ||
|
||
return implode('-', $parts); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
/** | ||
* This file is part of the O2System PHP Framework package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Mohamad Rafi Randoni | ||
* @copyright Copyright (c) Steeve Andrian Salim | ||
*/ | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
namespace O2System\Security\Generators; | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
/** | ||
* Class Token | ||
* | ||
* Security token generator. | ||
* | ||
* @package O2System\Security\Generators | ||
*/ | ||
class Token | ||
{ | ||
/** | ||
* Token::ALPHANUMERIC_STRING | ||
* | ||
* @var int | ||
*/ | ||
const ALPHANUMERIC_STRING = 0; | ||
|
||
/** | ||
* Token::ALPHAUPPERCASE_STRING | ||
* | ||
* @var int | ||
*/ | ||
const ALPHAUPPERCASE_STRING = 1; | ||
|
||
/** | ||
* Token::ALPHALOWERCASE_STRING | ||
* | ||
* @var int | ||
*/ | ||
const ALPHALOWERCASE_STRING = 2; | ||
|
||
/** | ||
* Token::NUMERIC_STRING | ||
* | ||
* @var int | ||
*/ | ||
const NUMERIC_STRING = 3; | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
/** | ||
* Token::generate | ||
* | ||
* @param int $length Token string length. | ||
* @param int $type Token string type. | ||
* | ||
* @return string | ||
*/ | ||
public static function generate($length = 8, $type = self::ALPHANUMERIC_STRING) | ||
{ | ||
switch ($type) { | ||
default: | ||
case self::ALPHANUMERIC_STRING: | ||
$codeAlphabet = implode(range('A', 'Z')); // Uppercase Alphabet | ||
$codeAlphabet .= implode(range('a', 'z')); // Lowercase Alphabet | ||
$codeAlphabet .= implode(range(0, 9)); // Numeric Alphabet | ||
break; | ||
case self::ALPHAUPPERCASE_STRING: | ||
$codeAlphabet = implode(range('A', 'Z')); // Uppercase Alphabet | ||
break; | ||
case self::ALPHALOWERCASE_STRING: | ||
$codeAlphabet = implode(range('a', 'z')); // Lowercase Alphabet | ||
break; | ||
case self::NUMERIC_STRING: | ||
$codeAlphabet = implode(range(0, 9)); // Numeric Alphabet | ||
break; | ||
} | ||
|
||
$token = ""; | ||
$max = strlen($codeAlphabet); | ||
|
||
for ($i = 0; $i < $length; $i++) { | ||
$token .= $codeAlphabet[ random_int(0, $max - 1) ]; | ||
} | ||
|
||
return $token; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* This file is part of the O2System PHP Framework package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Mohamad Rafi Randoni | ||
* @copyright Copyright (c) Steeve Andrian Salim | ||
*/ | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
namespace O2System\Security\Generators; | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
/** | ||
* Class Uid | ||
* @package O2System\Security\Generators | ||
*/ | ||
class Uid | ||
{ | ||
public static function generate($length = 8) | ||
{ | ||
$ids = str_split(time() . mt_rand()); | ||
shuffle($ids); | ||
|
||
$ids = array_slice($ids,0, $length); | ||
|
||
return implode('', $ids); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* This file is part of the O2System PHP Framework package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Mohamad Rafi Randoni | ||
* @copyright Copyright (c) Steeve Andrian Salim | ||
*/ | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
namespace O2System\Security\Generators; | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
/** | ||
* Class Uuid | ||
* | ||
* UUID (Universally Unique Identifier) Generator. | ||
* A UUID is a 16-octet (128-bit) number. | ||
* In its canonical form, a UUID is represented by 32 hexadecimal digits, displayed in five groups separated by hyphens, | ||
* in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens). | ||
* | ||
* @package O2System\Security\Generators | ||
*/ | ||
class Uuid | ||
{ | ||
/** | ||
* Uuid::generate | ||
* | ||
* @return string | ||
*/ | ||
public static function generate() | ||
{ | ||
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', | ||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), | ||
mt_rand(0, 0xffff), | ||
mt_rand(0, 0x0C2f) | 0x4000, | ||
mt_rand(0, 0x3fff) | 0x8000, | ||
mt_rand(0, 0x2Aff), mt_rand(0, 0xffD3), mt_rand(0, 0xff4B) | ||
); | ||
} | ||
} |