【问题标题】:php pack('H*',$securesecret) equivalant in JavaJava 中的 php pack('H*',$securesecret) 等价物
【发布时间】:2016-09-22 09:05:26
【问题描述】:

在 PHP 中没有打包功能

$message = "hello world";
$key = "7E066";
echo hash_hmac('SHA256',$message, $key);

我得到 0315a69471ebe855e9e221a374b30d8de08dcc833857f964737632698c87278e

在 Java 中

String data = "hello world";
String key  = "7E066";
System.out.println(hmacSha(key,data, "HmacSHA256"));

private static String hmacSha(String KEY, String VALUE, String SHA_TYPE) {
        try {

            SecretKeySpec signingKey = new SecretKeySpec(KEY.getBytes("UTF-8"), SHA_TYPE);
            Mac mac = Mac.getInstance(SHA_TYPE);
            mac.init(signingKey);
            byte[] rawHmac = mac.doFinal(VALUE.getBytes("UTF-8"));

            byte[] hexArray = {
                    (byte)'0', (byte)'1', (byte)'2', (byte)'3',
                    (byte)'4', (byte)'5', (byte)'6', (byte)'7',
                    (byte)'8', (byte)'9', (byte)'a', (byte)'b',
                    (byte)'c', (byte)'d', (byte)'e', (byte)'f'
            };
            byte[] hexChars = new byte[rawHmac.length * 2];
            for ( int j = 0; j < rawHmac.length; j++ ) {
                int v = rawHmac[j] & 0xFF;
                hexChars[j * 2] = hexArray[v >>> 4];
                hexChars[j * 2 + 1] = hexArray[v & 0x0F];
            }
            return new String(hexChars);
        }
        catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

我也得到 0315a69471ebe855e9e221a374b30d8de08dcc833857f964737632698c87278e。

在带有打包功能的 PHP 中

$message = "hello world";
$key = "7E066";
echo hash_hmac('SHA256',$message, pack('H*',$key));

我得到 33e97719c1b98f64bd0394e7fe94f43eae927e15f9eda15aeff0830bc3dd2fc3

我不明白 pack 函数是做什么的,我不能用 Java 编写相同的函数。谁能帮帮我?

【问题讨论】:

  • 哇,谢谢兄弟,它就像一个魅力......

标签: java php sha256 pack


【解决方案1】:

试试这个:

public String pack(String hex) {
    String input = hex.length() % 2 == 0 ? hex : hex  + "0";
    StringBuilder output = new StringBuilder();
    for (int i = 0; i < input.length(); i+=2) {
        String str = input.substring(i, i+2);
        output.append((char)Integer.parseInt(str, 16));
    }
    return output.toString();
}

对于这些数据,它会返回您所需要的数据:

    String data = "hello world";
    String key  = "7E066";
    System.out.println(hmacSha(key,data, "HmacSHA256"));
    System.out.println(hmacSha(pack(key), data, "HmacSHA256"));

0315a69471ebe855e9e221a374b30d8de08dcc833857f964737632698c87278e
33e97719c1b98f64bd0394e7fe94f43eae927e15f9eda15aeff0830bc3dd2fc3

诀窍是用于输入奇数长度的十六进制字符串的 pack() PHP 函数将其向左移动,即在值的右侧添加一个零。这是因为对于偶数长度的输入十六进制字符串,只能计算二进制字符串。

【讨论】:

    【解决方案2】:

    在我的情况下,只有这个:

    import org.apache.geronimo.mail.util.Hex;
    
    public class TestEncoding {
    
        public static void main(String[] args) {
            System.out.println(Hex.decode(("48398018")));
        }
    }
    

    结果: H9�

    这是等效的 PHP 代码:

    $nonce = 48398018;
    pack('H*', $nonce);
    echo $nonce;
    

    结果:H9�

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 2019-11-03
      • 2012-09-14
      相关资源
      最近更新 更多