Home Backend Development PHP Tutorial Create signature from Hash_hmac() and Sha256 in PHP

Create signature from Hash_hmac() and Sha256 in PHP

Feb 29, 2024 pm 02:34 PM
php programming Backend Development secure transmission

In PHP, it is a common security practice to use the Hash_hmac() function in conjunction with the Sha256 algorithm to create signatures. This method can help verify the integrity and authenticity of data during data transmission and prevent data from being tampered with or forged. By combining Hash_hmac() and Sha256, a secure key-based hash can be generated, ensuring secure transmission and processing of data. In this article, we’ll take a deep dive into how to use Hash_hmac() and Sha256 in PHP to create signatures, and how to apply this approach to enhance data security.

We'll show you how to use the hash_hmac and sha256 ciphers to create a secure signature that you can store in a database or Use in your login form.


hash_hmac() function in php

hash_hmac() Creates an alphanumeric keyed secret hash. It utilizes a cryptographic authentication technology called the HMAC method.

grammar:

hash_hmac($alGo, $data, $key, $binary = false);
Copy after login
  1. $algo is one of the hash_hmac parameters and is used as the signature secret keyed hash (String).
  2. $data, a hash value (usually the user's password). It can be alphanumeric.
  3. $key is generated from the HMAC method, which is the key you get from the function, and for other programming uses.
  4. The last parameter $binary is the binary value.

It returns raw data when TRUE and throws lowercase hexadecimal when FALSE.


Using hash_hmac() and sha256

in PHP

example:

<?php
//sha256 is a $algo
$PassWord= &#39;ThisIS123PasswORD&#39; ; //data
$secret ="ABCabc"; //$key
//false is bool value to check whether the method works or not
$hash_it = hash_hmac("sha256", utf8_encode($Password), $secret, false);//all four parameters
if(!$hash_it){ //checks true or false
	echo "Password was not encrypted!";
}
echo "Your encrypted signature is:<b> &#39;.$hash_it.&#39; </b>";
?>
Copy after login

Output:

Your encrypted signature is: &#39;.46e9757dc98f478c28f035cfa871dbd19b5a2bf8273225191bcca78801304813
Copy after login

Use hash_hmac() and base64_encode()

If you want your cryptographic signatures to be more secure, you can also execute the following code snippet.

example:

<?php
$my_sign = "abc123";
$key = TRUE;
 $base64_encode = base64_encode(hash_hmac(&#39;sha256&#39;, $my_sign, $key, true));
if(!$base64_encode){
echo "Your signature with the base64_decode(hash_hmac()); failed";
}
else{
echo "Your base64_decode(hash_hmac()); encrypted signature is is:<b> $base64_encode.&#39; </b>";
}
?>
Copy after login

Output:

Your base64_decode(hash_hmac()); encrypted signature is is: &#39;.00g0QMQlnluXxijqot60WZf6InDi07b/Mb5lL7eVZ34
Copy after login

Use hash_hmac() and sha256 in PHP and apply hex2bin/bin2hex transformation

  • hex2bin() - This function decodes a binary string that has been encoded in hexadecimal.
  • bin2hex() - Convert an ASCII string to a hexadecimal value using the bin2hex() method.

Let's say you have critical financial data that you want to encrypt and create a key for the user.

example:

<?php
$createhashmackey = "2974924872949278487322348237749823";
$bank_acc = "029480247299262947328749287";
$current_bal = $sum * 10000 / 450;
$bank_locker_no = &#39;AC54-A76X&#39;;
$last_deposit= $when;
$se = hex2bi&#110;($createhashmackey);
$create_his_signature = $bank_acc . $current_bal. $bank_locker_no . $last_deposit;
$enigma = bin2he&#120;(hash_hmac(&#39;sha256&#39;, $create_his_signature, $se));
echo "The secret signature is.$enigma.";
Copy after login

Output:

The secret signature is.33633066323533383537663538323937373536393764396530343661663336666438636435363631383934363864383966316133633433633965343234336233.
Copy after login

This is the output for all code examples in this article.

在 PHP 中从 Hash_hmac() 和 Sha256 创建签名

The above is the detailed content of Create signature from Hash_hmac() and Sha256 in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP format rows to CSV and write file pointer PHP format rows to CSV and write file pointer Mar 22, 2024 am 09:00 AM

This article will explain in detail how PHP formats rows into CSV and writes file pointers. I think it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Format rows to CSV and write to file pointer Step 1: Open file pointer $file=fopen(&quot;path/to/file.csv&quot;,&quot;w&quot;); Step 2: Convert rows to CSV string using fputcsv( ) function converts rows to CSV strings. The function accepts the following parameters: $file: file pointer $fields: CSV fields as an array $delimiter: field delimiter (optional) $enclosure: field quotes (

PHP changes current umask PHP changes current umask Mar 22, 2024 am 08:41 AM

This article will explain in detail about changing the current umask in PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Overview of PHP changing current umask umask is a php function used to set the default file permissions for newly created files and directories. It accepts one argument, which is an octal number representing the permission to block. For example, to prevent write permission on newly created files, you would use 002. Methods of changing umask There are two ways to change the current umask in PHP: Using the umask() function: The umask() function directly changes the current umask. Its syntax is: intumas

Three secrets for deploying large models in the cloud Three secrets for deploying large models in the cloud Apr 24, 2024 pm 03:00 PM

Compilation|Produced by Xingxuan|51CTO Technology Stack (WeChat ID: blog51cto) In the past two years, I have been more involved in generative AI projects using large language models (LLMs) rather than traditional systems. I'm starting to miss serverless cloud computing. Their applications range from enhancing conversational AI to providing complex analytics solutions for various industries, and many other capabilities. Many enterprises deploy these models on cloud platforms because public cloud providers already provide a ready-made ecosystem and it is the path of least resistance. However, it doesn't come cheap. The cloud also offers other benefits such as scalability, efficiency and advanced computing capabilities (GPUs available on demand). There are some little-known aspects of deploying LLM on public cloud platforms

PHP calculates MD5 hash of file PHP calculates MD5 hash of file Mar 21, 2024 pm 01:42 PM

This article will explain in detail about PHP calculating the MD5 hash of files. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP calculates the MD5 hash of a file MD5 (MessageDigest5) is a one-way encryption algorithm that converts messages of arbitrary length into a fixed-length 128-bit hash value. It is widely used to ensure file integrity, verify data authenticity and create digital signatures. Calculating the MD5 hash of a file in PHP PHP provides multiple methods to calculate the MD5 hash of a file: Use the md5_file() function. The md5_file() function directly calculates the MD5 hash value of the file and returns a 32-character

PHP returns an array with key values ​​flipped PHP returns an array with key values ​​flipped Mar 21, 2024 pm 02:10 PM

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values ​​in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values ​​swapped. $original_array=[

PHP determines whether a specified key exists in an array PHP determines whether a specified key exists in an array Mar 21, 2024 pm 09:21 PM

This article will explain in detail how PHP determines whether a specified key exists in an array. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP determines whether a specified key exists in an array: In PHP, there are many ways to determine whether a specified key exists in an array: 1. Use the isset() function: isset($array[&quot;key&quot;]) This function returns a Boolean value, true if the specified key exists, false otherwise. 2. Use array_key_exists() function: array_key_exists(&quot;key&quot;,$arr

PHP 401 response: Resolve Unauthorized errors and enhance security PHP 401 response: Resolve Unauthorized errors and enhance security Apr 09, 2024 pm 03:15 PM

In web development, a 401 Unauthorized error means that the client is not authorized to access a specific resource. PHP provides multiple processing methods: 1. Use 401 HTTP status code; 2. Output JSON response; 3. Redirect to the login page. To enhance security, you can take the following measures: 1. Use HTTPS; 2. Enable CSRF protection; 3. Implement input validation; 4. Use an authorization framework.

PHP returns the numeric encoding of the error message in the previous MySQL operation PHP returns the numeric encoding of the error message in the previous MySQL operation Mar 22, 2024 pm 12:31 PM

This article will explain in detail the numerical encoding of the error message returned by PHP in the previous Mysql operation. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. . Using PHP to return MySQL error information Numeric Encoding Introduction When processing mysql queries, you may encounter errors. In order to handle these errors effectively, it is crucial to understand the numerical encoding of error messages. This article will guide you to use php to obtain the numerical encoding of Mysql error messages. Method of obtaining the numerical encoding of error information 1. mysqli_errno() The mysqli_errno() function returns the most recent error number of the current MySQL connection. The syntax is as follows: $erro

See all articles