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.
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 the HMAC
method, which is the key you get from the function, and for other programming uses. $binary
is the binary value.
It returns raw data when TRUE
and throws lowercase hexadecimal when FALSE
.
hash_hmac()
and sha256
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
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
hash_hmac()
and sha256
in PHP and apply hex2bin
/bin2hex
transformationhex2bin()
- 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 = '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!