-
-
Save vinhjaxt/af8dcf83d622687f57721e19c1cccad2 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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment