|
|
|
|
@ -12,20 +12,17 @@ declare(strict_types=1); |
|
|
|
|
* NOTICE: This file is just for PLAY, not for PRODUCTION system! |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
namespace tts\services; |
|
|
|
|
namespace tts\services\obsolete; |
|
|
|
|
|
|
|
|
|
class crypto { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @var string |
|
|
|
|
*/ |
|
|
|
|
private $nonce; |
|
|
|
|
private string $nonce; |
|
|
|
|
private $random_engine; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Method to construct instance of Crypto |
|
|
|
|
* |
|
|
|
|
* @param string $nonce Nonce to crypt string |
|
|
|
|
* @return void |
|
|
|
|
*/ |
|
|
|
|
public function __construct($nonce = '') { |
|
|
|
|
if (is_array($nonce)) { |
|
|
|
|
@ -33,6 +30,7 @@ class crypto { |
|
|
|
|
} else { |
|
|
|
|
$this->nonce = !empty($nonce) ? base64_decode($nonce) : $this->generateNonce(); |
|
|
|
|
} |
|
|
|
|
$this->random_engine = new \tts\random_engine(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -54,9 +52,8 @@ class crypto { |
|
|
|
|
* |
|
|
|
|
* @param string $key Key to decode |
|
|
|
|
* @param string $input String to decode |
|
|
|
|
* @return mixed |
|
|
|
|
*/ |
|
|
|
|
public function decode(string $key, string $input) { |
|
|
|
|
public function decode(string $key, string $input): mixed { |
|
|
|
|
$keyDecode = base64_decode($key); |
|
|
|
|
$keypair1_public = $this->getPublic($keyDecode); |
|
|
|
|
$keypair1_secret = $this->getSecret($keyDecode); |
|
|
|
|
@ -87,7 +84,7 @@ class crypto { |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
public function generateNonce(): string { |
|
|
|
|
return random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES); |
|
|
|
|
return $this->random_engine->get_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function getNonce(): string { |
|
|
|
|
@ -104,9 +101,8 @@ class crypto { |
|
|
|
|
* @param string $input Input to decode |
|
|
|
|
* @param string $keyPublic Key public |
|
|
|
|
* @param string $keySecret Key secret |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
private function _decode(string $input, string $keyPublic, string $keySecret) { |
|
|
|
|
private function _decode(string $input, string $keyPublic, string $keySecret): false|string { |
|
|
|
|
$decryption_key = sodium_crypto_box_keypair_from_secretkey_and_publickey(base64_decode($keySecret), base64_decode($keyPublic)); |
|
|
|
|
return sodium_crypto_box_open(base64_decode($input), $this->nonce, $decryption_key); |
|
|
|
|
} |
|
|
|
|
@ -119,28 +115,22 @@ class crypto { |
|
|
|
|
* @param string $keySecret Key secret |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
private function _encode(string $input, string $keyPublic, string $keySecret) { |
|
|
|
|
private function _encode(string $input, string $keyPublic, string $keySecret): string { |
|
|
|
|
$encryption_key = sodium_crypto_box_keypair_from_secretkey_and_publickey(base64_decode($keySecret), base64_decode($keyPublic)); |
|
|
|
|
return base64_encode(sodium_crypto_box($input, $this->nonce, $encryption_key)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Method to return secret of key |
|
|
|
|
* |
|
|
|
|
* @param string $key |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
private function getSecret(string $key) { |
|
|
|
|
private function getSecret(string $key): string { |
|
|
|
|
return base64_encode(sodium_crypto_box_secretkey($key)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Method to return public of key |
|
|
|
|
* |
|
|
|
|
* @param string $key |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
private function getPublic(string $key) { |
|
|
|
|
private function getPublic(string $key): string { |
|
|
|
|
return base64_encode(sodium_crypto_box_publickey($key)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|