acf_encrypt() │ ACF 5.5.8

Encrypts a string using PHP. https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/

Хуков нет.

Возвращает

string|false. Encrypted string, or false if encryption fails.

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

acf_encrypt( $data, $context );
$data(строка)
The data to encrypt.
По умолчанию: ''
$context(строка)
Optional domain-separation label. When non-empty, the key derivation and HMAC input are bound to this label so a token minted for one context cannot be verified as belonging to another. When empty, the legacy key derivation is used for back-compat with existing callers.
По умолчанию: ''

Список изменений

С версии 5.5.8 Введена.

Код acf_encrypt() ACF 6.8.11

function acf_encrypt( $data = '', string $context = '' ) {

	if ( ! function_exists( 'openssl_encrypt' ) ) {
		return false;
	}

	if ( '' === $context ) {
		$key     = wp_hash( 'acf_encrypt' );
		$mac_key = wp_hash( 'acf_encrypt_mac' );
	} else {
		$key     = wp_hash( 'acf_encrypt|' . $context );
		$mac_key = wp_hash( 'acf_encrypt_mac|' . $context );
	}

	$iv = openssl_random_pseudo_bytes( openssl_cipher_iv_length( 'aes-256-cbc' ) );
	if ( false === $iv ) {
		return false;
	}

	$encrypted_data = openssl_encrypt( $data, 'aes-256-cbc', $key, 0, $iv );
	if ( false === $encrypted_data ) {
		return false;
	}

	$payload  = $encrypted_data . '::' . $iv;
	$mac_data = '' === $context ? $payload : ( $context . '|' . $payload );
	$hmac     = hash_hmac( 'sha256', $mac_data, $mac_key, true );

	return base64_encode( $payload . $hmac );
}