parent
127a31958d
commit
b8051fba32
@ -1,137 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
declare(strict_types=1); |
|
||||||
|
|
||||||
/** |
|
||||||
* @author Robert Strutts <Robert@TryingToScale.com> |
|
||||||
* @copyright Copyright (c) 2022, Robert Strutts. |
|
||||||
* @license https://mit-license.org/ |
|
||||||
*/ |
|
||||||
|
|
||||||
/** |
|
||||||
* NOTICE: This file is just for PLAY, not for PRODUCTION system! |
|
||||||
*/ |
|
||||||
|
|
||||||
namespace tts\services\obsolete; |
|
||||||
|
|
||||||
class crypto { |
|
||||||
|
|
||||||
private string $nonce; |
|
||||||
private $random_engine; |
|
||||||
|
|
||||||
/** |
|
||||||
* Method to construct instance of Crypto |
|
||||||
* |
|
||||||
* @param string $nonce Nonce to crypt string |
|
||||||
*/ |
|
||||||
public function __construct($nonce = '') { |
|
||||||
if (is_array($nonce)) { |
|
||||||
$this->nonce = $this->generateNonce(); |
|
||||||
} else { |
|
||||||
$this->nonce = !empty($nonce) ? base64_decode($nonce) : $this->generateNonce(); |
|
||||||
} |
|
||||||
$this->random_engine = new \tts\random_engine(); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Method to encode string |
|
||||||
* |
|
||||||
* @param string $key Key to encode |
|
||||||
* @param mixed $input Input to crypt |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
public function encode(string $key, $input): string { |
|
||||||
$keyDecode = base64_decode($key); |
|
||||||
$keypair1_public = $this->getPublic($keyDecode); |
|
||||||
$keypair1_secret = $this->getSecret($keyDecode); |
|
||||||
return $this->_encode(serialize($input), $keypair1_public, $keypair1_secret); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Method to decode string |
|
||||||
* |
|
||||||
* @param string $key Key to decode |
|
||||||
* @param string $input String to decode |
|
||||||
*/ |
|
||||||
public function decode(string $key, string $input): mixed { |
|
||||||
$keyDecode = base64_decode($key); |
|
||||||
$keypair1_public = $this->getPublic($keyDecode); |
|
||||||
$keypair1_secret = $this->getSecret($keyDecode); |
|
||||||
return unserialize($this->_decode($input, $keypair1_public, $keypair1_secret)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Method to generate new key |
|
||||||
* |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
public function generateKey(): string { |
|
||||||
return base64_encode(sodium_crypto_box_keypair()); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Method to generate new key |
|
||||||
* |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
public static function getKey(): string { |
|
||||||
return base64_encode(sodium_crypto_box_keypair()); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Method to generate new nonce |
|
||||||
* |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
public function generateNonce(): string { |
|
||||||
return $this->random_engine->get_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES); |
|
||||||
} |
|
||||||
|
|
||||||
public function getNonce(): string { |
|
||||||
return base64_encode($this->nonce); |
|
||||||
} |
|
||||||
|
|
||||||
public function setNonce(string $nonce): void { |
|
||||||
$this->nonce = base64_decode($nonce); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Method to decode string |
|
||||||
* |
|
||||||
* @param string $input Input to decode |
|
||||||
* @param string $keyPublic Key public |
|
||||||
* @param string $keySecret Key secret |
|
||||||
*/ |
|
||||||
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); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Method to encode string |
|
||||||
* |
|
||||||
* @param string $input Input to encode |
|
||||||
* @param string $keyPublic Key public |
|
||||||
* @param string $keySecret Key secret |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
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 |
|
||||||
*/ |
|
||||||
private function getSecret(string $key): string { |
|
||||||
return base64_encode(sodium_crypto_box_secretkey($key)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Method to return public of key |
|
||||||
*/ |
|
||||||
private function getPublic(string $key): string { |
|
||||||
return base64_encode(sodium_crypto_box_publickey($key)); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
Loading…
Reference in new issue