Source of file AtRestCryptoService.php
Size: 3,301 Bytes - Last Modified: 2021-12-23T10:50:52+00:00
/var/www/docs.ssmods.com/process/src/code/AtRestCryptoService.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 | <?php use Defuse\Crypto\Crypto; use Defuse\Crypto\Exception\CannotPerformOperationException; use Defuse\Crypto\Exception\CryptoTestFailedException; use Defuse\Crypto\Exception\InvalidCiphertextException; use Defuse\Crypto\Exception\InvalidInput; use Defuse\Crypto\File; use Defuse\Crypto\Key; use Ex\CryptoException; class AtRestCryptoService {// extends Object { /** * @param string $raw * @param null|Key $key * @return string * @throws CannotPerformOperationException * @throws CryptoTestFailedException */ public function encrypt($raw, $key = null) { $key = $this->getKey($key); return Crypto::Encrypt($raw, $key); } /** * @param string $ciphertext * @param null|Key $key * @return string * @throws CannotPerformOperationException * @throws InvalidCiphertextException */ public function decrypt($ciphertext, $key = null) { $key = $this->getKey($key); return Crypto::Decrypt($ciphertext, $key); } /** * @param \File $file * @param null|Key $key * @return bool|\File * @throws InvalidInput * @throws CryptoException */ public function encryptFile($file, $key = null) { $key = $this->getKey($key); $encryptedFilename = $file->getFullPath() . '.enc'; try { File::encryptFile($file->getFullPath(), $encryptedFilename, $key); unlink($file->getFullPath()); $file->Filename = $file->Filename . '.enc'; $file->write(); return $file; } catch (Exception $e) { print_r($e->getMessage()); SS_Log::log(sprintf('Encryption exception while parsing "%s": %s', $file->Name, $e->getMessage()), SS_Log::ERR); return false; } } /** * @param \File $file * @param null|Key $key * @return bool|\File * @throws InvalidInput * @throws CryptoException */ public function decryptFile($file, $key = null) { $key = $this->getKey($key); $decryptedFilename = str_replace('.enc', '', $file->getFullPath()); try { File::decryptFile($file->getFullPath(), $decryptedFilename, $key); unlink($file->getFullPath()); $file->Filename = str_replace('.enc', '', $file->Filename); $file->Name = str_replace('.enc', '', $file->Name); $file->write(); return $file; } catch (Exception $e) { SS_Log::log(sprintf('Decryption exception while parsing "%s": %s', $file->Name, $e->getMessage()), SS_Log::ERR); return false; } } public function getKey($rawKey) { // If this is already a \Defuse\Crypto\Key object, just return it if ($rawKey instanceof Key) { return $rawKey; } if ($rawKey === null && defined('ENCRYPT_AT_REST_KEY')) { // Retrieve key from _ss_env, if set $rawKey = ENCRYPT_AT_REST_KEY; } if ($rawKey === null) { throw new \InvalidArgumentException('Can\'t encrypt without a key. Define ENCRYPT_AT_REST_KEY, or pass the $key argument.'); } $key = Key::LoadFromAsciiSafeString($rawKey); return $key; } } |