encrypt($plain, $key); $hmac = hash_hmac('sha256', $ciphertext, $key); return $hmac . $ciphertext; } /** * Check the encryption key for proper length. * * @param string $key Key to check. * @param string $method The method the key is being checked for. * @return void * @throws \InvalidArgumentException When key length is not 256 bit/32 bytes */ protected static function _checkKey(string $key, string $method): void { if (mb_strlen($key, '8bit') < 32) { throw new InvalidArgumentException( sprintf('Invalid key for %s, key must be at least 256 bits (32 bytes) long.', $method) ); } } /** * Decrypt a value using AES-256. * * @param string $cipher The ciphertext to decrypt. * @param string $key The 256 bit/32 byte key to use as a cipher key. * @param string|null $hmacSalt The salt to use for the HMAC process. * Leave null to use value of Security::getSalt(). * @return string|null Decrypted data. Any trailing null bytes will be removed. * @throws \InvalidArgumentException On invalid data or key. */ public static function decrypt(string $cipher, string $key, ?string $hmacSalt = null): ?string { self::_checkKey($key, 'decrypt()'); if (empty($cipher)) { throw new InvalidArgumentException('The data to decrypt cannot be empty.'); } if ($hmacSalt === null) { $hmacSalt = static::getSalt(); } // Generate the encryption and hmac key. $key = mb_substr(hash('sha256', $key . $hmacSalt), 0, 32, '8bit'); // Split out hmac for comparison $macSize = 64; $hmac = mb_substr($cipher, 0, $macSize, '8bit'); $cipher = mb_substr($cipher, $macSize, null, '8bit'); $compareHmac = hash_hmac('sha256', $cipher, $key); if (!static::constantEquals($hmac, $compareHmac)) { return null; } $crypto = static::engine(); return $crypto->decrypt($cipher, $key); } /** * A timing attack resistant comparison that prefers native PHP implementations. * * @param mixed $original The original value. * @param mixed $compare The comparison value. * @return bool * @since 3.6.2 */ public static function constantEquals($original, $compare): bool { return is_string($original) && is_string($compare) && hash_equals($original, $compare); } /** * Gets the HMAC salt to be used for encryption/decryption * routines. * * @return string The currently configured salt */ public static function getSalt(): string { if (static::$_salt === null) { throw new RuntimeException( 'Salt not set. Use Security::setSalt() to set one, ideally in `config/bootstrap.php`.' ); } return static::$_salt; } /** * Sets the HMAC salt to be used for encryption/decryption * routines. * * @param string $salt The salt to use for encryption routines. * @return void */ public static function setSalt(string $salt): void { static::$_salt = $salt; } }