Yoast\WP\SEO\MyYoast_Client\Infrastructure\Crypto

Encryption::encrypt │ public │ Yoast 1.0

Encrypts a plaintext string using sodium_crypto_secretbox (XSalsa20-Poly1305).

Метод класса: Encryption{}

Хуков нет.

Возвращает

string. Base64-encoded nonce + ciphertext.

Использование

$Encryption = new Encryption();
$Encryption->encrypt(;

Код Encryption::encrypt() Yoast 28.9

public function encrypt(
	// phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPNativeAttributeFound -- No-op on PHP < 8.2; redacts parameter from stack traces on PHP 8.2+.
	#[SensitiveParameter]
	string $plaintext,
	string $context
): string {
	$key = $this->derive_key( $context );

	try {
		$nonce      = \random_bytes( \SODIUM_CRYPTO_SECRETBOX_NONCEBYTES );
		$ciphertext = \sodium_crypto_secretbox( $plaintext, $nonce, $key );

		// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Binary ciphertext needs safe encoding.
		return \base64_encode( $nonce . $ciphertext );
	} catch ( Exception $e ) {
		// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Internal exception message.
		throw new Encryption_Exception( 'Encryption failed: ' . $e->getMessage(), 0, $e );
	}
	finally {
		// Securely wipe the derived key from memory to prevent leakage via memory dumps or core files.
		try {
			\sodium_memzero( $key );
		} catch ( SodiumException $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- Best-effort cleanup.
			// Best-effort: key will be freed when $key goes out of scope.
		}
	}
}