2019-12-06 17:07:51 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-12-06 06:04:09 +05:30
|
|
|
namespace common\components\OAuth2;
|
2019-12-06 17:07:51 +05:30
|
|
|
|
2020-08-23 02:53:36 +05:30
|
|
|
use LogicException;
|
|
|
|
use RangeException;
|
|
|
|
use SodiumException;
|
2019-12-06 17:07:51 +05:30
|
|
|
use Yii;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This trait is intended to override the standard data encryption behavior
|
|
|
|
* with the help of \Defuse\Crypto\Crypto class, because the resultant string
|
|
|
|
* is much larger than the original one.
|
|
|
|
*
|
|
|
|
* The implementation under the hood relies on using libsodium library
|
|
|
|
* that provides more compact result values.
|
|
|
|
*/
|
|
|
|
trait CryptTrait {
|
|
|
|
|
2024-12-06 06:04:09 +05:30
|
|
|
protected function encrypt(string $unencryptedData): string {
|
2019-12-06 17:07:51 +05:30
|
|
|
return Yii::$app->tokens->encryptValue($unencryptedData);
|
|
|
|
}
|
|
|
|
|
2024-12-06 06:04:09 +05:30
|
|
|
protected function decrypt(string $encryptedData): string {
|
2020-08-23 02:53:36 +05:30
|
|
|
try {
|
|
|
|
return Yii::$app->tokens->decryptValue($encryptedData);
|
2024-12-02 15:40:55 +05:30
|
|
|
} catch (SodiumException|RangeException $e) {
|
2020-08-23 02:53:36 +05:30
|
|
|
throw new LogicException($e->getMessage(), 0, $e);
|
|
|
|
}
|
2019-12-06 17:07:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|