JIYIK CN >

Current Location:Home > Learning > PROGRAM > PHP >

Creating a signature from Hash_hmac() and Sha256 in PHP

Author:JIYIK Last Updated:2025/04/13 Views:

PHP has one of the best encryption functions for data security. Hash_hmac()The encrypt function is one of the most famous encryptors.

We'll show you how to use hash_hmacand sha256encryptors to create 安全签名one that you can store in a database or use in your login form.


hash_hmac()Functions in PHP

hash_hmac()Creates an alphanumeric keyed secret hash value. It utilizes HMACa cryptographic authentication technique called the method.

grammar:

hash_hmac($algo, $data, $key, $binary = false);
  1. $algoIs one of the hash_hmac parameters and is used as the signature secret keyed hash value ( String).
  2. $data, a hashed value (usually the user's password). It can be alphanumeric.
  3. $keyis generated from HMACthe method, it is the key you get from the function, and for other programmatic uses.
  4. The last parameter $binaryis a binary value.

It TRUEreturns the raw data when , and FALSEthrows out lowercase hex when .


In PHP use hash_hmac()andsha256

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()andbase64_encode()

If you want your cryptographic signature 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

hash_hmac()Use and sha256apply hex2bin/ bin2hexconvert in PHP

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

Suppose you have critical financial data that you want to encrypt and create keys for users.

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 of all the code examples in this article.

hash_hmac() and sha256 signature in php

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if a Post exists in PHP

Publish Date:2025/04/13 Views:170 Category:PHP

PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss

PHP with Ajax

Publish Date:2025/04/13 Views:139 Category:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Returns the article tag with ID from the action page

Publish Date:2025/04/13 Views:80 Category:PHP

Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Displays PHP configuration information on localhost

Publish Date:2025/04/13 Views:107 Category:PHP

phpinfo() is a built-in function in PHP which outputs all the information of PHP configuration on the local host. We have to phpinfo() create a PHP file with a simple function call. Sometimes, the file may not work properly and output a 404

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial