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

[PHP 中的 hash_hmac() 函数]

hash_hmac() 创建一个字母数字键控的秘密哈希值。它利用称为 HMAC 方法的加密身份验证技术。

语法:

hash_hmac($algo, $data, $key, $binary = false);
  1. $algo 是 hash_hmac 参数之一,用作签名秘密键控哈希值 (String)。

  2. $data,一个散列值(通常是用户的密码)。它可以是字母数字。

  3. $key 是从 HMAC 方法生成的,它是你从函数中获得的密钥,以及其他编程用途。

  4. 最后一个参数 $binary 是二进制值。

它在 TRUE 时返回原始数据,并在 FALSE 时抛出小写十六进制。

[在 PHP 中使用 hash_hmac()sha256]

例子:

<?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>";
?>

输出:

Your encrypted signature is: '.46e9757dc98f478c28f035cfa871dbd19b5a2bf8273225191bcca78801304813

[使用 hash_hmac()base64_encode()]

如果你希望你的加密签名更安全,你还可以执行以下代码片段。

例子:

<?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>";
}
?>

输出:

Your base64_decode(hash_hmac()); encrypted signature is is: '.00g0QmqlnluXxijqot60WZf6InDi07b/Mb5lL7eVZ34

[在 PHP 中使用 hash_hmac()sha256 并应用 hex2bin/bin2hex 转换]

  • hex2bin() - 此函数解码已以十六进制编码的二进制字符串。
  • bin2hex() - 使用 bin2hex() 方法将 ASCII 字符串转换为十六进制值。

假设你有要加密的关键财务数据并为用户创建密钥。

例子:

<?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.";

输出:

The secret signature is.33633066323533383537663538323937373536393764396530343661663336666438636435363631383934363864383966316133633433633965343234336233.

这是本教程中所有代码示例的输出。

php 中 hash_hmac() 和 sha256 的签名

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小柴没吃饱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值