Chet Bil
Chet Bil

Reputation: 21

php pack('H*',$securesecret) equivalant in Java

In PHP Without pack function

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

I get 0315a69471ebe855e9e221a374b30d8de08dcc833857f964737632698c87278e

In 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);
        }
    }

I get 0315a69471ebe855e9e221a374b30d8de08dcc833857f964737632698c87278e too.

In PHP with Pack function

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

I get 33e97719c1b98f64bd0394e7fe94f43eae927e15f9eda15aeff0830bc3dd2fc3

I don't understand what pack function does, I can not write same function in Java. Could anyone help me please?

Upvotes: 2

Views: 5540

Answers (2)

danil cheliadin
danil cheliadin

Reputation: 1

In my case work only this:

import org.apache.geronimo.mail.util.Hex;

public class TestEncoding {

    public static void main(String[] args) {
        System.out.println(Hex.decode(("48398018")));
    }
}

Result: H9�

It was equivalent PHP code:

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

Result: H9�

Upvotes: 0

mv200580
mv200580

Reputation: 732

Try this:

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();
}

for this data it returns exactly that you need:

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

0315a69471ebe855e9e221a374b30d8de08dcc833857f964737632698c87278e
33e97719c1b98f64bd0394e7fe94f43eae927e15f9eda15aeff0830bc3dd2fc3

The trick is that the pack() PHP function for input hexadecimal string of the odd length shift it to the left, i.e. add one zero to the right of the value. This is because it is only possible to calculate binary string for even-length input hexadecimal string.

Upvotes: 3

Related Questions