61 if (isset($this->key) && ! isset($this->cipher)) {
63 } elseif (! isset($this->key)) {
67 if (! self::isValidKey($this->key, $this->cipher)) {
73 $size = \strlen(
$key);
74 if (isset(self::$acceptedCiphers[$size])) {
75 return self::$acceptedCiphers[$size];
87 protected function hash($iv, $value): string {
88 return \hash_hmac(
'sha256', $iv . $value, $this->key);
98 $payload = \json_decode(\base64_decode($payload),
true);
99 if (! $this->isValidPayload($payload)) {
102 if (! $this->isValidMac($payload)) {
116 return \is_array($payload) && isset($payload[
'iv'], $payload[
'value'], $payload[
'mac']) && \strlen(\base64_decode($payload[
'iv'],
true)) === \openssl_cipher_iv_length($this->cipher);
126 $calculated = $this->calculateMac($payload, $bytes = random_bytes(16));
127 return \hash_equals(\hash_hmac(
'sha256', $payload[
'mac'], $bytes,
true), $calculated);
138 return \hash_hmac(
'sha256', $this->hash($payload[
'iv'], $payload[
'value']), $bytes,
true);
149 public function encrypt($value, $serialize =
true): string {
150 $iv = \random_bytes(\openssl_cipher_iv_length($this->cipher));
151 $value = \openssl_encrypt($serialize ? \serialize($value) : $value, $this->cipher, $this->key, 0, $iv);
152 if ($value ===
false) {
156 $mac = $this->hash($iv = base64_encode($iv), $value);
157 $json = \json_encode(\compact(
'iv',
'value',
'mac'), JSON_UNESCAPED_SLASHES);
158 if (\json_last_error() !== \JSON_ERROR_NONE) {
162 return \base64_encode($json);
172 return $this->encrypt($value, false);
183 public function decrypt(
string $payload, $unserialize =
true) {
184 $payload = $this->getJsonPayload($payload);
185 $iv = base64_decode($payload[
'iv']);
186 $decrypted = \openssl_decrypt($payload[
'value'], $this->cipher, $this->key, 0, $iv);
188 if ($decrypted ===
false) {
192 return $unserialize ? unserialize($decrypted) : $decrypted;
203 return $this->decrypt($payload,
false);
213 public static function isValidKey(
string $key,
string $cipher): bool {
214 $length = \strlen($key);
215 return isset(self::$acceptedCiphers[$length]) && self::$acceptedCiphers[$length] === $cipher;
225 $sizeMethods = \array_flip(self::$acceptedCiphers);
226 return \bin2hex(\random_bytes($sizeMethods[$cipher] / 2));
242 return $this->cipher;
245 public static function getMethods(?
bool $aliases =
null): array {
246 return \openssl_get_cipher_methods($aliases);
Ubiquity\security\data$Encryption This class is part of Ubiquity Inspired from illuminate/encryption ...
static isValidKey(string $key, string $cipher)
Check if the given key and cipher combination is valid.
static generateKey(string $cipher)
Generate a new key for the given cipher.
static getCipherFromKey(string $key)
hash($iv, $value)
Create a MAC for the given value.
isValidMac(array $payload)
Check if the MAC for the given payload is valid.
__construct(?string $key=null, ?string $cipher=null)
Create a new encrypter instance.
getJsonPayload($payload)
Get the JSON array from the given payload.
encryptString(string $value)
Encrypt a string without serialization.
calculateMac($payload, $bytes)
Calculate the hash of the given payload.
encrypt($value, $serialize=true)
Encrypt the given value.
isValidPayload($payload)
Check that the encryption payload is valid.
decrypt(string $payload, $unserialize=true)
Decrypt the given value.
decryptString($payload)
Decrypt the given string without unserialization.
static getMethods(?bool $aliases=null)