Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
EncryptionManager.php
Go to the documentation of this file.
1<?php
3
4use Ubiquity\config\Configuration;
7
17
18 const ENCRYPTION_KEY_NAME = 'encryption_key';
19
24 private static $encryptionInstance;
25
26 private static function getInstance(?string $key, ?string $cypher = Encryption::AES128): Encryption {
27 return self::$encryptionInstance ??= new Encryption($key, $cypher);
28 }
29
37 public static function start(array &$config, ?string $cipher = Encryption::AES128) {
38 $oldKey = $config[self::ENCRYPTION_KEY_NAME] ?? null;
39 self::getInstance($oldKey, $cipher);
40 self::$encryptionInstance->initializeKeyAndCipher();
41 $key = self::$encryptionInstance->getKey();
42
43 if ($oldKey !== $key) {
44 $oConfig = Configuration::loadConfigWithoutEval('config');
45 $oConfig[self::ENCRYPTION_KEY_NAME] = "getenv('".self::ENCRYPTION_KEY_NAME."')";
46 $envArray=EnvFile::load();
47 $envArray[self::ENCRYPTION_KEY_NAME]=$key;
48 EnvFile::save($envArray);
49 Startup::saveConfig($oConfig);
50 }
51 }
52
59 public static function startProd(array $config, ?string $cypher = null) {
60 $key = $config[self::ENCRYPTION_KEY_NAME];
61 self::getInstance($key, $cypher ?? Encryption::getCipherFromKey($key));
62 }
63
70 public static function encrypt($data): string {
71 if (is_string($data)) {
72 return self::$encryptionInstance->encryptString($data);
73 }
74 return self::$encryptionInstance->encrypt($data);
75 }
76
83 public static function decryptString(string $data): string {
84 return self::$encryptionInstance->decryptString($data);
85 }
86
94 public static function decrypt(string $data, $unserialize = true) {
95 return self::$encryptionInstance->decrypt($data, $unserialize);
96 }
97
104 public static function generateKey(?string $cipher = Encryption::AES128): string {
105 return self::getInstance(null)->generateKey($cipher ?? Encryption::AES128);
106 }
107
108 public static function getKey() {
109 return self::getInstance(self::getKey())->getKey();
110 }
111
112 public static function getEncryptionInstance(): ?Encryption {
113 return self::$encryptionInstance;
114 }
115
116 public static function isStarted(): bool {
117 return isset(self::$encryptionInstance);
118 }
119}
120
static load(?string $path=null, string $filename='.env')
Loads an env file an returns an array of key/value pairs.
Definition EnvFile.php:87
static save(array $content,?string $path=null, string $filename='.env')
Saves a content array on disk.
Definition EnvFile.php:42
Starts the framework.
Definition Startup.php:19
Ubiquity\security\data$Encryption This class is part of Ubiquity Inspired from illuminate/encryption ...
static getCipherFromKey(string $key)
Ubiquity\security\data$EncryptionManager This class is part of Ubiquity.
static decrypt(string $data, $unserialize=true)
Decrypt the given data with possible unserialization.
static decryptString(string $data)
Decrypt the given string.
static getInstance(?string $key, ?string $cypher=Encryption::AES128)
static start(array &$config, ?string $cipher=Encryption::AES128)
Start the manager and generate the encryption key.
static startProd(array $config, ?string $cypher=null)
Start the encryption manager for production.
static generateKey(?string $cipher=Encryption::AES128)
Generate a new encryption key.
static encrypt($data)
Encrypt the given data.