PHP openssl_verify() Function Last Updated : 27 Oct, 2021 Comments Improve Suggest changes Like Article Like Report The openssl_verify() function is an inbuilt function in PHP which is used to verifies that if the signature is correct for the specified data using the public key associated with public_key or not. This must be the public key corresponding to the private key used for signing. Syntax: openssl_verify( $data, $signature, $public_key, $algorithm ): int|false Parameters: This function accept four parameters which are listed below - data: The string of data used to generate the signature previously.signature: A raw binary string, generated by openssl_sign() or similar means.public_key: string - a PEM formatted key, example, "-----BEGIN PUBLIC KEY----- MIIBCgK..."algorithm: A valid string returned by openssl_get_md_methods() function. Return Value: It returns 1 if the signature is correct, 0 if it is incorrect, and -1 or false on error. Note: The public key comes from a certificate in any of the support formats. Listed below are examples illustrating the use of openssl_verify() function: Example 1: PHP <?php // Data you want to sign $data = 'geeks for geeks'; // Create a new pair of private and public key $private_key_rsa = openssl_pkey_new(array( "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA, )); $details = openssl_pkey_get_details($private_key_rsa); $public_key_rsa = openssl_pkey_get_public($details['key']); // Create signature for your data openssl_sign($data, $signature, $private_key_rsa, "sha256WithRSAEncryption"); // Verify signature obtained for your data $result = openssl_verify($data, $signature, $public_key_rsa, OPENSSL_ALGO_SHA256); if ($result == 1) { echo "signature is valid for given data."; } elseif ($ok == 0) { echo "signature is invalid for given data."; } else { echo "error: ".openssl_error_string(); } ?> Output: signature is valid for given data. Example 2: PHP <?php // Data you want to sign $data = 'geeks for geeks'; // Create a new pair of private and public key $private_key_rsa = openssl_pkey_new(array( "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA, )); $details = openssl_pkey_get_details($private_key_rsa); $public_key_rsa = openssl_pkey_get_public($details['key']); // Create signature for your data openssl_sign($data, $signature, $private_key_rsa, "sha256WithRSAEncryption"); // Change the data $data = 'geeks and geeks'; // Verify signature obtained for your data $result = openssl_verify($data, $signature, $public_key_rsa, OPENSSL_ALGO_SHA256); if ($result == 1) { echo "signature is valid for given data."; } elseif ($ok == 0) { echo "signature is invalid for given data."; } else { echo "error: ".openssl_error_string(); } ?> Output: signature is invalid for given data. Reference: https://www.php.net/manual/en/function.openssl-verify.php Comment More infoAdvertise with us Next Article PHP openssl_verify() Function S Shivam.Pradhan Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-OpenSSL Similar Reads PHP openssl_spki_verify() Function The openssl_spki_verify() function is a built-in function in PHP and is used to validate the supplied signed public key and challenge. This should be the public key corresponding to the private key used for the signature. It verifies a signed public key and challenge. Syntax: string openssl_spki_ver 2 min read PHP openssl_pkey_new() Function The openssl_pkey_new() function is an inbuilt function in PHP cryptography extension that is used to generate a new private/public key pair using the OpenSSL library. Syntax: openssl_pkey_new(?array $options = null): OpenSSLAsymmetricKey|false Parameters: This function accepts one parameter which is 2 min read PHP openssl_pkey_export() Function The openssl_pkey_export() function is an inbuilt function in PHP which is used to view and manage private keys and public keys. It gets an exportable representation of a key into a string. It exports the key as a PEM encoded string and stores to pass by reference. Syntax: bool openssl_pkey_export( m 2 min read PHP openssl_error_string() Function The openssl_error_string() function is an inbuilt function in PHP which is used to get  the last error from the openSSL library. Error messages are queued, so this function should be called multiple times to collect all of the information. The last error will be the most recent one. Syntax: openssl_ 3 min read PHP openssl_pkcs12_read() Function The openssl_pkcs12_read() function is a built-in function in PHP and is used by the PKCS # 12 certificate store to convert it into an array provided by pkcs12. A PKCS #12 file may be encrypted and signed. Syntax: bool openssl_pkcs12_read( string $pkcs12, array &$certs, string $pass ) Parameters: 2 min read PHP openssl_digest() Function The openssl_digest() function is an inbuilt function in PHP that is used to compute a digest hash value for the given data using a given method and returns a raw or binary hex-encoded string. Syntax: openssl_digest( string $data, string $digest_algo, bool $binary = false): string|false Parameters: T 2 min read PHP openssl_pbkdf2() Function The openssl_pbkdf2() function is an inbuilt function in PHP that implements the Password-Based Key Derivation Function 2 (PBKDF2) algorithm provided by the OpenSSL library. The algorithm is designed to be slow and computationally intensive, making it resistant to brute-force attacks. Syntax: openssl 2 min read PHP openssl_pkcs12_export() Function The opensl_pkcs12_export() function is a built-in function in PHP which is used to store in a string called x509 in a PKCS # 12 file format. Certificate store file corresponding to PKCS # 12 variable. Syntax: bool openssl_pkcs12_export( mixed $x509, string &$out, mixed $priv_key, string $pass [, 2 min read PHP | is_string() Function The is_string() function is an inbuilt function in PHP which is used to check whether the given value is a string or not. Syntax: bool is_string( mixed $var ) Parameters: This function accepts one parameter as mentioned above and described below: $var: It contains the value that need to check. Retur 1 min read PHP | is_real() Function The is_real() function is an inbuilt function in PHP which is used to check the given value is a real number or not. Syntax: bool is_real( mixed $var ) Parameters: This function accepts one parameter as mentioned above and described below: $var: It contains the value of variable that need to be chec 2 min read Like