|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Morph\Crypt; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use RuntimeException; |
| 9 | +use Morph\Support\Set; |
| 10 | + |
| 11 | +use function base64_decode; |
| 12 | +use function base64_encode; |
| 13 | +use function define; |
| 14 | +use function defined; |
| 15 | +use function openssl_decrypt; |
| 16 | +use function openssl_encrypt; |
| 17 | +use function random_bytes; |
| 18 | +use function Morph\Cast\arrayify; |
| 19 | +use function Morph\Cast\stringify; |
| 20 | +use function Morph\Json\decode; |
| 21 | +use function Morph\Json\encode; |
| 22 | +use function Morph\Util\extractString; |
| 23 | +use function sprintf; |
| 24 | + |
| 25 | +if (! defined('DEFAULT_CRYPT_KEY')) { |
| 26 | + define('DEFAULT_CRYPT_KEY', base64_encode(stringify(random_bytes(32)))); |
| 27 | +} |
| 28 | + |
| 29 | +if (! function_exists(__NAMESPACE__ . '\encrypt')) { |
| 30 | + function encrypt(string $plaintext, string $key = DEFAULT_CRYPT_KEY): string |
| 31 | + { |
| 32 | + $salt = random_bytes(16); |
| 33 | + $ciphertext = openssl_encrypt($plaintext, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $salt); |
| 34 | + if ($ciphertext === false) { |
| 35 | + throw new RuntimeException(sprintf("Encryption failed: '%s'.", $plaintext)); |
| 36 | + } |
| 37 | + |
| 38 | + $data = group('aes-256-cbc', base64_encode($salt), base64_encode($ciphertext)); |
| 39 | + return base64_encode($data); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +if (! function_exists(__NAMESPACE__ . '\decrypt')) { |
| 44 | + function decrypt(string $encrypted, string $key = DEFAULT_CRYPT_KEY): string |
| 45 | + { |
| 46 | + $set = ungroup($encrypted); |
| 47 | + if ($set->get('algo') !== 'aes-256-cbc') { |
| 48 | + throw new InvalidArgumentException('Unknown encryption algorithm.'); |
| 49 | + } |
| 50 | + |
| 51 | + $salt = base64_decode(stringify($set->get('salt'))); |
| 52 | + $ciphertext = base64_decode(stringify($set->get('data'))); |
| 53 | + |
| 54 | + $decrypted = openssl_decrypt($ciphertext, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $salt); |
| 55 | + if ($decrypted === false) { |
| 56 | + throw new RuntimeException(sprintf("Decryption failed: '%s'.", $encrypted)); |
| 57 | + } |
| 58 | + return $decrypted; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +if (! function_exists(__NAMESPACE__ . '\group')) { |
| 63 | + function group(string $algo, string $salt, string $ciphertext): string |
| 64 | + { |
| 65 | + $data = [ |
| 66 | + 'algo' => $algo, |
| 67 | + 'salt' => $salt, |
| 68 | + 'data' => $ciphertext, |
| 69 | + ]; |
| 70 | + $encoded = encode($data); |
| 71 | + if ($encoded === null) { |
| 72 | + throw new RuntimeException(sprintf("Encryption failed: '%s'.", $ciphertext)); |
| 73 | + } |
| 74 | + return $encoded; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +if (! function_exists(__NAMESPACE__ . '\ungroup')) { |
| 79 | + function ungroup(string $encrypted): Set |
| 80 | + { |
| 81 | + $decoded = arrayify(decode(base64_decode($encrypted))); |
| 82 | + $algo = extractString($decoded, 'algo'); |
| 83 | + $salt = extractString($decoded, 'salt'); |
| 84 | + $data = extractString($decoded, 'data'); |
| 85 | + return ($algo && $salt && $data) |
| 86 | + ? new Set([ |
| 87 | + 'algo' => $algo, |
| 88 | + 'salt' => $salt, |
| 89 | + 'data' => $data, |
| 90 | + ]) |
| 91 | + : throw new InvalidArgumentException(sprintf("Invalid encrypted format: '%s'.", $encrypted)); |
| 92 | + } |
| 93 | +} |
0 commit comments