Current Location: Home> Latest Articles> PHP hash_final is used for signature generation of network request data

PHP hash_final is used for signature generation of network request data

gitbox 2025-05-19

The hash_final function is a function in PHP that generates hash values. It is usually used with hash_init and hash_update to create a hash context and update the data step by step. hash_final is used to complete the hash operation and return the final hash value.

 string hash_final ( resource $context [, bool $raw_output = false ] )
  • $context : is a hash context initialized by the hash_init function.

  • $raw_output : If set to true , return the hash value in binary format; if set to false , return the hash value in hexadecimal format.

Requirements for generating signatures in network requests

In many APIs, network requests must be accompanied by signature parameters, and signatures are usually the result of the cryptographic hash of the request parameters and keys. The purpose is to prevent requests from being tampered with. By generating signatures using hash functions, the consistency and security of requested content during transmission can be ensured.

Generate signatures using hash_final

Suppose we need to generate a signature through PHP and use the hash_final function to complete this process. The specific steps are as follows:

  1. Initialize the hash context:

First, use hash_init to initialize the hash context and select the appropriate hash algorithm (such as sha256 ).

 $context = hash_init('sha256');
  1. Update the hash context:

Next, we gradually add the request parameters and key to the hash context. Here we take the requested parameters as an example:

 $params = [
    'method' => 'POST',
    'url' => 'https://gitbox.net/api/v1/order',
    'timestamp' => time(),
    'api_key' => 'your_api_key'
];

foreach ($params as $key => $value) {
    hash_update($context, $key . '=' . $value);
}
  1. Generate the final signature:

The final hash value is generated as a signature through the hash_final function. Here we choose to use hexadecimal format:

 $signature = hash_final($context, false);

Complete code example

Here is a complete sample code that demonstrates how to generate a signature and append it to an API request.

 <?php

// Initialize hash context
$context = hash_init('sha256');

// Request parameters
$params = [
    'method' => 'POST',
    'url' => 'https://gitbox.net/api/v1/order',
    'timestamp' => time(),
    'api_key' => 'your_api_key'
];

// 将Request parameters添加到哈希上下文
foreach ($params as $key => $value) {
    hash_update($context, $key . '=' . $value);
}

// Generate a signature
$signature = hash_final($context, false);

// Print signature results
echo 'Generated Signature: ' . $signature;

// Further use generated signatures to build network requests
// Sample Request
$request_data = [
    'method' => 'POST',
    'url' => 'https://gitbox.net/api/v1/order',
    'timestamp' => time(),
    'api_key' => 'your_api_key',
    'signature' => $signature
];

// Code to execute the request
// Used herecURLSend a request if you like,Pass request data

?>

How to use signatures to make network requests?

In practical applications, after a signature is generated, it is usually sent to the API server as part of the request. Here is a simple example showing how to attach a generated signature to a POST request.

 $ch = curl_init();

// ConfigurationcURLask
curl_setopt($ch, CURLOPT_URL, 'https://gitbox.net/api/v1/order');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request_data));

// implementcURLask
$response = curl_exec($ch);

// closurecURLSession
curl_close($ch);

// Output response
echo $response;

In this way, we can ensure that the data in the request has not been tampered with during transmission, and the API server can use the same signature algorithm to verify the legitimacy of the request.