Skip to content

Commit

Permalink
Optimize Codes
Browse files Browse the repository at this point in the history
  • Loading branch information
steevenz committed May 29, 2018
1 parent 0c98a7f commit a417d4b
Show file tree
Hide file tree
Showing 26 changed files with 873 additions and 846 deletions.
43 changes: 22 additions & 21 deletions src/Encryptions/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @author Steeve Andrian Salim
* @copyright Copyright (c) Steeve Andrian Salim
*/

// ------------------------------------------------------------------------

namespace O2System\Security\Encryptions;
Expand Down Expand Up @@ -125,14 +126,14 @@ public function __construct()
/**
* Uppercase letter character
*/
$upperLetterCharacter = array_map( 'strtoupper', $lowerLetterCharacter );
$upperLetterCharacter = array_map('strtoupper', $lowerLetterCharacter);

// ------------------------------------------------------------------------

/**
* Binary character
*/
$numericCharacter = range( 0, 9, 1 );
$numericCharacter = range(0, 9, 1);

// ------------------------------------------------------------------------

Expand All @@ -143,22 +144,22 @@ public function __construct()
$numericCharacter
);

if ( class_exists( '\O2System\Framework', false ) ) {
$key = config()->getItem( 'security' )->offsetGet( 'encryptionKey' );
if (class_exists('\O2System\Framework', false)) {
$key = config()->getItem('security')->offsetGet('encryptionKey');
$letters = str_split($key);
$cryptoKey = 0;

foreach ( $letters as $letter ) {
if ( $number = array_search( $letter, static::$charactersMap ) ) {
foreach ($letters as $letter) {
if ($number = array_search($letter, static::$charactersMap)) {
$cryptoKey = $cryptoKey + $number;
}
}

$charactersMap = static::$charactersMap;
static::$charactersMap = [];

if($cryptoKey > 0) {
foreach($charactersMap as $key => $value) {
if ($cryptoKey > 0) {
foreach ($charactersMap as $key => $value) {
static::$charactersMap[ $key * $cryptoKey ] = $value;
}
}
Expand All @@ -176,15 +177,15 @@ public function __construct()
*
* @return string
*/
public function encrypt( $string )
public function encrypt($string)
{
$numbers = [];
$letters = str_split( $this->crypt->encrypt( $string ) );
$letters = str_split($this->crypt->encrypt($string));

$i = 0;
foreach ( $letters as $letter ) {
if ( $number = array_search( $letter, static::$charactersMap ) ) {
if ( $i == 20 ) {
foreach ($letters as $letter) {
if ($number = array_search($letter, static::$charactersMap)) {
if ($i == 20) {
$number = $number . PHP_EOL;
$i = 0;
}
Expand All @@ -195,7 +196,7 @@ public function encrypt( $string )
}
}

return implode( ' ', $numbers );
return implode(' ', $numbers);
}

// ------------------------------------------------------------------------
Expand All @@ -209,18 +210,18 @@ public function encrypt( $string )
*
* @return string
*/
public function decrypt( $numbers )
public function decrypt($numbers)
{
$letters = [];
$numbers = explode( ' ', str_replace( PHP_EOL, '', $numbers ) );
$numbers = explode(' ', str_replace(PHP_EOL, '', $numbers));

foreach ( $numbers as $number ) {
if ( array_key_exists( $number, static::$charactersMap ) ) {
foreach ($numbers as $number) {
if (array_key_exists($number, static::$charactersMap)) {
$letters[] = static::$charactersMap[ $number ];
}
}

return $this->crypt->decrypt( implode( '', $letters ) );
return $this->crypt->decrypt(implode('', $letters));
}

// ------------------------------------------------------------------------
Expand All @@ -234,9 +235,9 @@ public function decrypt( $numbers )
*
* @return static
*/
protected function setKey( $key )
protected function setKey($key)
{
$this->crypt->setKey( $key );
$this->crypt->setKey($key);

return $this;
}
Expand Down
39 changes: 20 additions & 19 deletions src/Encryptions/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @author Steeve Andrian Salim
* @copyright Copyright (c) Steeve Andrian Salim
*/

// ------------------------------------------------------------------------

namespace O2System\Security\Encryptions;
Expand Down Expand Up @@ -56,14 +57,14 @@ public function __construct()
'httpOnly' => false,
];

if ( class_exists( '\O2System\Framework', false ) ) {
$this->options = config()->getItem( 'cookie' )->getArrayCopy();
if (class_exists('\O2System\Framework', false)) {
$this->options = config()->getItem('cookie')->getArrayCopy();
$this->options[ 'expire' ] = time() + $this->options[ 'lifetime' ];
unset( $this->options[ 'lifetime' ] );
unset($this->options[ 'lifetime' ]);
}

$this->options[ 'domain' ] = empty( $this->options[ 'domain' ] )
? isset( $_SERVER[ 'HTTP_HOST' ] )
$this->options[ 'domain' ] = empty($this->options[ 'domain' ])
? isset($_SERVER[ 'HTTP_HOST' ])
? $_SERVER[ 'HTTP_HOST' ]
: $_SERVER[ 'SERVER_NAME' ]
: $this->options[ 'domain' ];
Expand All @@ -80,10 +81,10 @@ public function __construct()
*
* @return static
*/
public function setOptions( array $options )
public function setOptions(array $options)
{
foreach ( $options as $key => $value ) {
if ( array_key_exists( $key, $this->options ) ) {
foreach ($options as $key => $value) {
if (array_key_exists($key, $this->options)) {
$this->options[ $key ] = $value;
}
}
Expand All @@ -103,17 +104,17 @@ public function setOptions( array $options )
*
* @return bool
*/
public function encrypt( $name, $value )
public function encrypt($name, $value)
{
$value = is_array( $value ) || is_object( $value )
? serialize( $value )
$value = is_array($value) || is_object($value)
? serialize($value)
: $value;

$name = isset( $this->options[ 'prefix' ] )
$name = isset($this->options[ 'prefix' ])
? $this->options[ 'prefix' ] . $name
: $name;

$value = $this->crypt->encrypt( $value );
$value = $this->crypt->encrypt($value);

return setcookie(
$name,
Expand All @@ -137,14 +138,14 @@ public function encrypt( $name, $value )
*
* @return string|bool Returns FALSE if cookie is not exists or the decryption failure.
*/
public function decrypt( $name )
public function decrypt($name)
{
$name = isset( $this->options[ 'prefix' ] )
$name = isset($this->options[ 'prefix' ])
? $this->options[ 'prefix' ] . $name
: $name;

if ( $value = input()->cookie( $name ) ) {
return $this->crypt->decrypt( $value );
if ($value = input()->cookie($name)) {
return $this->crypt->decrypt($value);
}

return false;
Expand All @@ -161,9 +162,9 @@ public function decrypt( $name )
*
* @return static
*/
protected function setKey( $key )
protected function setKey($key)
{
$this->crypt->setKey( $key );
$this->crypt->setKey($key);

return $this;
}
Expand Down
Loading

0 comments on commit a417d4b

Please sign in to comment.