key = hash( 'sha256', wp_salt( 'auth' ) . 'feednetic', true ); } /** * Encrypts a plaintext token. Returns a base64-encoded nonce+ciphertext. */ public function encrypt( string $plaintext ): string { $nonce = random_bytes( SODIUM_CRYPTO_SECRETBOX_NONCEBYTES ); $cipher = sodium_crypto_secretbox( $plaintext, $nonce, $this->key ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode $encoded = base64_encode( $nonce . $cipher ); sodium_memzero( $nonce ); return $encoded; } /** * Decrypts an encrypted token. Returns null on decryption failure (e.g. key mismatch). */ public function decrypt( string $ciphertext ): ?string { // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode $decoded = base64_decode( $ciphertext, true ); if ( false === $decoded || strlen( $decoded ) <= SODIUM_CRYPTO_SECRETBOX_NONCEBYTES ) { return null; } $nonce = substr( $decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES ); $cipher = substr( $decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES ); $result = sodium_crypto_secretbox_open( $cipher, $nonce, $this->key ); return false === $result ? null : $result; } }