Create signature from Hash_hmac() and Sha256 in PHP
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);
-
$algo
is one of the hash_hmac parameters and is used as the signature secret keyed hash (String
). -
$data
, a hash value (usually the user's password). It can be alphanumeric. -
$key
is generated from theHMAC
method, which is the key you get from the function, and for other programming uses. -
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= 'ThisIS123PasswORD' ; //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> '.$hash_it.' </b>"; ?>
Output:
Your encrypted signature is: '.46e9757dc98f478c28f035cfa871dbd19b5a2bf8273225191bcca78801304813
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('sha256', $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.' </b>"; } ?>
Output:
Your base64_decode(hash_hmac()); encrypted signature is is: '.00g0QMQlnluXxijqot60WZf6InDi07b/Mb5lL7eVZ34
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 thebin2hex()
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 = 'AC54-A76X'; $last_deposit= $when; $se = hex2bin($createhashmackey); $create_his_signature = $bank_acc . $current_bal. $bank_locker_no . $last_deposit; $enigma = bin2hex(hash_hmac('sha256', $create_his_signature, $se)); echo "The secret signature is.$enigma.";
Output:
The secret signature is.33633066323533383537663538323937373536393764396530343661663336666438636435363631383934363864383966316133633433633965343234336233.
This is the output for all code examples in this article.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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("path/to/file.csv","w"); 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 (

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

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

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

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=[

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["key"]) This function returns a Boolean value, true if the specified key exists, false otherwise. 2. Use array_key_exists() function: array_key_exists("key",$arr

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.

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
