Skip to content

Instantly share code, notes, and snippets.

@border
Last active April 16, 2021 03:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save border/0110ee2f98764fc0c656f4986e3178ed to your computer and use it in GitHub Desktop.
Save border/0110ee2f98764fc0c656f4986e3178ed to your computer and use it in GitHub Desktop.
Signature for php and java
The answer is in the documentation for the PHP function hash_hmac.
When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits.
Pass "true" as the final argument. Hashes are binary. When turning them into strings, they are often encoded in hexadecimal. But in this case you are going to base-64 encode it, so you want the raw binary form.
from: http://stackoverflow.com/questions/11002603/base64-encode-different-between-java-and-php
static String generateSignature () {
String encoded = "";
String type = "HmacSHA1";
try {
byte[] key = ("KEY").getBytes("UTF-8");
byte[] Sequence = ("hello").getBytes("UTF-8");
Mac HMAC = Mac.getInstance(type);
SecretKeySpec secretKey = new SecretKeySpec(key, type);
HMAC.init(secretKey);
byte[] Hash = HMAC.doFinal(Sequence);
encoded = Base64.getEncoder().encodeToString(Hash);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(encoded);
return encoded;
}
$key = "KEY";
$sequence = "hello";
$encrypted = hash_hmac('sha1', $sequence, $key, true);
echo base64_encode($encrypted).PHP_EOL;
@lebaminhquang
Copy link

Thank you so much!

@siddharth
Copy link

Man I've been breaking my head on this so long, wondering if its got to do with how bytes are represented internally in java! Should have gone through documentation properly. Cannot upvote this enough!

@amhoho
Copy link

amhoho commented Oct 15, 2018

hello,border!
how to convert this java code to php code.

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class CustomerHttpClient
{
  public static String getMac(String paramString1, String paramString2)
  {
    byte[] arrayOfByte1 = new byte[32];
    arrayOfByte1[0] = 87;
    arrayOfByte1[1] = -77;
//...
    arrayOfByte1[30] = -99;
    arrayOfByte1[31] = 54;
    Object localObject = "";
      Mac localMac = Mac.getInstance("HmacSHA256");
      byte[] arrayOfByte2 = paramString2.getBytes("ASCII");
      localMac.init(new SecretKeySpec(arrayOfByte1, "HMACSHA256"));
      localObject = toHex(localMac.doFinal(arrayOfByte2));
      String str = ((String)localObject).toUpperCase();
      localObject = str;
      return localObject;
  }

  public static String toHex(byte[] paramArrayOfByte)
  {
    StringBuffer localStringBuffer1 = new StringBuffer(2 * paramArrayOfByte.length);
    StringBuffer localStringBuffer2 = new StringBuffer(paramArrayOfByte.length);
    for (int i = 0; ; i++)
    {
      if (i >= paramArrayOfByte.length)
        return localStringBuffer1.toString();
      localStringBuffer1.append(Character.forDigit((0xF0 & paramArrayOfByte[i]) >> 4, 16));
      localStringBuffer1.append(Character.forDigit(0xF & paramArrayOfByte[i], 16));
      localStringBuffer2.append(paramArrayOfByte[i]);
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment