Last active
April 16, 2021 03:41
-
-
Save border/0110ee2f98764fc0c656f4986e3178ed to your computer and use it in GitHub Desktop.
Signature for php and java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$key = "KEY"; | |
$sequence = "hello"; | |
$encrypted = hash_hmac('sha1', $sequence, $key, true); | |
echo base64_encode($encrypted).PHP_EOL; |
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!
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
Thank you so much!