将以下JAVA代码:public static String hmacsha256(String plainStr, String key) { SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), "HmacSHA256"); Mac mac=null; try { mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); } catch (NoSuchAlgorithmException | InvalidKeyException e) { e.printStackTrace(); } byte digest[] = mac.doFinal(plainStr.getBytes(Charset.forName("UTF- 8"))); return new StringBuilder().append(byte2HexStr(digest)).toString(); } public static String byte2HexStr(byte array[]) { return array != null ? new String(Hex.encodeHex(array)) : null; } 使用 PHP7.4 实现
时间: 2023-03-04 10:33:50 浏览: 50
以下是使用PHP7.4实现等效功能的代码:
```
function hmacsha256($plainStr, $key) {
$secretKey = utf8_encode($key);
$digest = hash_hmac('sha256', $plainStr, $secretKey, true);
return byte2HexStr($digest);
}
function byte2HexStr($array) {
return $array !== null ? bin2hex($array) : null;
}
```
在这个PHP版本中,使用 `hash_hmac` 函数来计算HMAC SHA256,通过将 $key 转换为 UTF-8 编码的字符串来创建密钥,然后传递给该函数。使用 `true` 作为第四个参数,可以获得二进制格式的哈希值。最后,使用 `bin2hex` 将二进制格式的哈希值转换为十六进制字符串。
请注意,由于Java代码使用了Apache Commons Codec库中的Hex.encodeHex方法,因此我们使用了bin2hex而不是PHP内置的hex2bin。如果您需要使用内置的hex2bin,请确保正确调用了参数。
相关问题
public static String hmacsha256(String plainStr, String key) { SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), "HmacSHA256"); Mac mac=null; try { mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); } catch (NoSuchAlgorithmException | InvalidKeyException e) { e.printStackTrace(); } byte digest[] = mac.doFinal(plainStr.getBytes(Charset.forName("UTF- 8"))); return new StringBuilder().append(byte2HexStr(digest)).toString(); } public static String byte2HexStr(byte array[]) { return array != null ? new String(Hex.encodeHex(array)) : null; } java to PHP
Here's the PHP equivalent of the Java code for computing HMAC-SHA256:
```
function hmacsha256($plainStr, $key) {
$secretKey = utf8_encode($key);
$digest = hash_hmac('sha256', utf8_encode($plainStr), $secretKey, true);
return bin2hex($digest);
}
```
Note that in PHP, the `hash_hmac` function is used to compute the HMAC with SHA256. The third parameter of `hash_hmac` is set to `true` to get the output as raw binary data. The `bin2hex` function is then used to convert the binary data to a hexadecimal string, which is the same format as the output of the `byte2HexStr` function in the Java code.
Also note that in PHP, strings are already represented as Unicode, so there's no need to convert the key and plain string to UTF-8 encoding using the `Charset.forName("UTF-8")` method.
签名步骤:byte2HexStr (HMAC_SHA256(post-content +RequestURI, key), RequestURI 以”/” 开头,不包括域名;post-content 是指发送的完整 json 数据;key= AppSecret+Nonce+ timestamp, AppSecret 是和 vendorId 对应的密钥,签名的结果需要放到 name 为”parm-sign” 的 http header 中。 以下代码列出了 java 版本的签名实现: import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.Charset; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.commons.codec.binary.Hex; public class EncryptUtil { /** * * @param plainStr 需要加密的字符串 * @param key key=AppSecret+nonce+ timestamp * @return 加密后的字符串 */ public static String hmacsha256(String plainStr, String key) { SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), "HmacSHA256"); Mac mac=null; try { mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); } catch (NoSuchAlgorithmException | InvalidKeyException e) { e.printStackTrace(); } byte digest[] = mac.doFinal(plainStr.getBytes(Charset.forName("UTF-8"))); return new StringBuilder().append(byte2HexStr(digest)).toString(); } public static String byte2HexStr(byte array[]) { return array != null ? new String(Hex.encodeHex(array)) : null; } } 请以PHP7.4版本实现签名
以下是PHP 7.4版本的实现,使用了相同的依赖库`apache/commons-codec`来进行十六进制编码。
```php
<?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);
}
```
使用方法示例:
```php
$plainStr = '...'; // post-content RequestURI
$key = '...'; // AppSecret Nonce timestamp
$sign = hmacsha256($plainStr, $key);
$headers = [
'parm-sign: ' . $sign,
];
// 发送HTTP请求,携带上述 headers
```