-
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
1 parent
f5c2a74
commit 8cbf9a8
Showing
9 changed files
with
353 additions
and
23 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
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
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,60 @@ | ||
<?php | ||
|
||
namespace Mfonte\Base62x\Encryption; | ||
|
||
use Mfonte\Base62x\Exception\CryptException; | ||
use Mfonte\Base62x\Encryption\Cipher\Decrypt; | ||
use Mfonte\Base62x\Encryption\Cipher\Encrypt; | ||
|
||
class Crypt | ||
{ | ||
protected $method = 'aes-128-ctr'; // default cipher method if none supplied. see: http://php.net/openssl_get_cipher_methods for more. | ||
|
||
private $key; | ||
|
||
private $data; | ||
|
||
public function __construct($options = []) | ||
{ | ||
//Set default encryption key if none supplied | ||
$key = isset($options['key']) ? $options['key'] : \php_uname(); | ||
|
||
$method = isset($options['method']) ? $options['method'] : false; | ||
|
||
// convert ASCII keys to binary format | ||
$this->key = \ctype_print($key) ? \openssl_digest($key, 'SHA256', true) : $key; | ||
|
||
if ($method) { | ||
if (\in_array(\mb_strtolower($method), \openssl_get_cipher_methods(), true)) { | ||
$this->method = $method; | ||
} else { | ||
throw new CryptException("unrecognised cipher method: {$method}"); | ||
} | ||
} | ||
} | ||
|
||
public function cipher($data) | ||
{ | ||
$this->data = $data; | ||
|
||
return $this; | ||
} | ||
|
||
public function encrypt() | ||
{ | ||
return Encrypt::token( | ||
$this->data, | ||
$this->method, | ||
$this->key | ||
); | ||
} | ||
|
||
public function decrypt() | ||
{ | ||
return Decrypt::token( | ||
$this->data, | ||
$this->method, | ||
$this->key | ||
); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace Mfonte\Base62x\Encryption\Cipher; | ||
|
||
class Bytes | ||
{ | ||
public static function iv($method) | ||
{ | ||
return \openssl_cipher_iv_length($method); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace Mfonte\Base62x\Encryption\Cipher; | ||
|
||
class Decrypt | ||
{ | ||
public static function token($data, $method, $key) | ||
{ | ||
$iv_strlen = 2 * Bytes::iv($method); | ||
if (\preg_match('/^(.{'.$iv_strlen.'})(.+)$/', $data, $regs)) { | ||
list(, $iv, $crypted_string) = $regs; | ||
if (\ctype_xdigit($iv) && \mb_strlen($iv) % 2 == 0) { | ||
return \openssl_decrypt($crypted_string, $method, $key, 0, \hex2bin($iv)); | ||
} | ||
} | ||
|
||
return false; // failed to decrypt | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Mfonte\Base62x\Encryption\Cipher; | ||
|
||
class Encrypt | ||
{ | ||
public static function token($data, $method, $key) | ||
{ | ||
$iv = \openssl_random_pseudo_bytes(Bytes::iv($method)); | ||
|
||
return \bin2hex($iv).\openssl_encrypt($data, $method, $key, 0, $iv); | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace Mfonte\Base62x\Exception; | ||
|
||
class CryptException extends \RuntimeException | ||
{ | ||
const REASON = 'Crypt Exception'; | ||
const CODE = 0; | ||
|
||
public function __construct(string $reason) | ||
{ | ||
parent::__construct('Exception in encryption algorithm : '.$reason, static::CODE); | ||
} | ||
} |
Oops, something went wrong.