签名步骤:byte2HexStr (HMAC_SHA256(post-content +RequestURI, key), RequestURI 以”/” 开头,不包括域名;post-content ...

文章展示了在PHP7.4中使用Sodium扩展进行密码处理的方法,包括生成和验证哈希(crypto_pwhash_str)以及实现HMACSHA256。还提供了将字节转换为十六进制字符串的辅助函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下是PHP 7.4版本的实现,使用了相同的依赖库apache/commons-codec来进行十六进制编码。

<?php

use function Sodium\crypto_generichash;
use function Sodium\crypto_pwhash_str;
use function Sodium\crypto_pwhash_str_verify;

require_once __DIR__ . '/vendor/autoload.php';

function hmacsha256($plainStr, $key)
{
    $secretKey = new \SecretKeySpec(
        utf8_encode($key),
        'HmacSHA256'
    );

    $mac = hash_init('sha256', HASH_HMAC, $secretKey);
    hash_update($mac, utf8_encode($plainStr));
    $digest = hash_final($mac, true);

    return byte2HexStr($digest);
}

function byte2HexStr($array)
{
    return $array !== null ? \strval(Hex::encode($array)) : null;
}

function generate_password($password, $salt)
{
    return crypto_pwhash_str($password, \SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE, \SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE);
}

function verify_password($hash, $password)
{
    return crypto_pwhash_str_verify($hash, $password);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值