secret = $secret; } /** * Encrypts a plaintext string, picking sodium when available and * falling back to openssl otherwise. Output is ASCII-safe (prefix + * base64), storable directly in wp_options. * * @param string $plaintext Value to encrypt. * @return string Self-describing encrypted blob. */ public function encrypt( string $plaintext ): string { if ( function_exists( 'sodium_crypto_secretbox' ) ) { $nonce = random_bytes( SODIUM_CRYPTO_SECRETBOX_NONCEBYTES ); $ciphertext = sodium_crypto_secretbox( $plaintext, $nonce, $this->secret ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- binary-safe storage encoding of ciphertext for wp_options, not code obfuscation. return self::PREFIX_SODIUM . base64_encode( $nonce . $ciphertext ); } $iv = random_bytes( self::OPENSSL_IV_BYTES ); $tag = ''; $ciphertext = openssl_encrypt( $plaintext, 'aes-256-gcm', $this->secret, OPENSSL_RAW_DATA, $iv, $tag, '', self::OPENSSL_TAG_BYTES ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- binary-safe storage encoding of ciphertext for wp_options, not code obfuscation. return self::PREFIX_OPENSSL . base64_encode( $iv . $tag . $ciphertext ); } /** * Decrypts a blob produced by encrypt(). Returns null (never throws) * when the blob is malformed, was tampered with, or was encrypted with * a different secret — callers treat null as "no key stored" and ask * the user to re-enter it. * * @param string $blob Encrypted blob from encrypt(). * @return string|null Plaintext, or null on any failure. */ public function decrypt( string $blob ): ?string { if ( 0 === strpos( $blob, self::PREFIX_SODIUM ) ) { return $this->decrypt_sodium( substr( $blob, strlen( self::PREFIX_SODIUM ) ) ); } if ( 0 === strpos( $blob, self::PREFIX_OPENSSL ) ) { return $this->decrypt_openssl( substr( $blob, strlen( self::PREFIX_OPENSSL ) ) ); } return null; } /** * Masks an API key for display: first 3 + "..." + last 4 characters * (e.g. "sk-...abcd"), or a fixed placeholder when the key is too short * for that to hide anything. * * @param string $key Raw API key. * @return string Masked representation, safe to send to the browser. */ public static function mask_key( string $key ): string { if ( strlen( $key ) < 10 ) { return '••••'; } return substr( $key, 0, 3 ) . '...' . substr( $key, -4 ); } /** * Decrypts the sodium payload variant. * * @param string $encoded Base64 part of the blob (nonce + ciphertext). * @return string|null Plaintext, or null on failure. */ protected function decrypt_sodium( string $encoded ): ?string { if ( ! function_exists( 'sodium_crypto_secretbox_open' ) ) { return null; } // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- decodes this class's own encrypt() output, not obfuscated code. $raw = base64_decode( $encoded, true ); if ( false === $raw || strlen( $raw ) <= SODIUM_CRYPTO_SECRETBOX_NONCEBYTES ) { return null; } $nonce = substr( $raw, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES ); $ciphertext = substr( $raw, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES ); $plaintext = sodium_crypto_secretbox_open( $ciphertext, $nonce, $this->secret ); return false !== $plaintext ? $plaintext : null; } /** * Decrypts the openssl AES-256-GCM payload variant. * * @param string $encoded Base64 part of the blob (iv + tag + ciphertext). * @return string|null Plaintext, or null on failure. */ protected function decrypt_openssl( string $encoded ): ?string { // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- decodes this class's own encrypt() output, not obfuscated code. $raw = base64_decode( $encoded, true ); if ( false === $raw || strlen( $raw ) <= self::OPENSSL_IV_BYTES + self::OPENSSL_TAG_BYTES ) { return null; } $iv = substr( $raw, 0, self::OPENSSL_IV_BYTES ); $tag = substr( $raw, self::OPENSSL_IV_BYTES, self::OPENSSL_TAG_BYTES ); $ciphertext = substr( $raw, self::OPENSSL_IV_BYTES + self::OPENSSL_TAG_BYTES ); $plaintext = openssl_decrypt( $ciphertext, 'aes-256-gcm', $this->secret, OPENSSL_RAW_DATA, $iv, $tag ); return false !== $plaintext ? $plaintext : null; } }