Open In App

PHP openssl_digest() Function

Last Updated : 27 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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: This function accepts four parameters as mentioned above and described below –

  • data: The data for which digest value is to be created.
  • digest_algo: The digest method to use, e.g. “sha256”.
  • binary: Setting to true will return as raw output data, otherwise the return value is binhex encoded.

Return Values: It returns the digested hash value on success or false on failure.

 

Below program illustrate the openssl_digest() function in PHP.

Example 1: 

PHP




<?php
    
// Data for which digest is to be created
$data = 'geeks for geeks';
  
  
// openssl_digest() with sha512 algorithm
// to compute digest value true as
// parameters return the raw input
$fingerPrint = openssl_digest ($data , "sha512", true);
  
// Print the output of openssl_digest output
print_r($fingerPrint);
  
?>


Output :

o��P�R�:`�w�ce�V x���(�eH@\000�7��G�”

Example 2: 

PHP




<?php
    
// Data for which digest is to be created
$data = 'geeks for geeks';
  
  
// openssl_digest() with sha512 algorithm
// to compute digest value false as 
// parameters return the hex value
$fingerPrint = openssl_digest ($data , "sha512", false);
  
// Print the output of openssl_digest output
print_r($fingerPrint);
  
?>


Output:

6e5f36e9cee5cba6ad938977c98e12f3a61fc4d944753ad130116b026b8ab2c895878910fea3b47dba6d760a20d0b23233980a8dab13f04f262c53f25222b416

Reference: https://www.php.net/manual/en/function.openssl-pkey-export.php



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads