java equivalent to php's hmac-SHA1
In fact they do agree.
As Hans Doggen already noted PHP outputs the message digest using hexadecimal notation unless you set the raw output parameter to true.
If you want to use the same notation in Java you can use something like
for (byte b : digest) {
System.out.format("%02x", b);
}
System.out.println();
to format the output accordingly.
hash_hmac analog for Java
Found solution here: java equivalent to php's hmac-SHA1
The problem was in BASE64Encoder. Use
for (byte b : digest) {
System.out.format("%02x", b);
}
System.out.println();
or
new BigInteger(digest).toString(16);
instead.
HMAC-SHA1: How to do it properly in Java?
On your PHP side, use single-quotes around the key so that the $
character is not treated as a variable reference. i.e.,
hash_hmac("sha1", "helloworld", 'PRIE7$oG2uS-Yf17kEnUEpi5hvW/#AFo')
Otherwise, the key you really get is PRIE7-Yf17kEnUEpi5hvW/#AFo
(assuming the variable $oG2uS
is not defined).
How to create OAuth HMAC-SHA1 signature on GAE/J?
public String computeHmac(String baseString, String key)
throws NoSuchAlgorithmException, InvalidKeyException, IllegalStateException, UnsupportedEncodingException
{
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(key.getBytes(), mac.getAlgorithm());
mac.init(secret);
byte[] digest = mac.doFinal(baseString.getBytes());
return Base64.encode(digest);
}
why PHP's hash_hmac('sha256') gives different result than java sha256_HMAC
The output of the php function are lowercase hex digits when the fourth parameter is false. Your second java version however produces uppercase hex digits. Either correct the case difference or you could change the fourth parameter of hash_hmac to true and it will probably match with your first Java version.
ColdFusion equivalent to PHP hash_hmac
I found a solution on this page http://www.isummation.com/blog/calculate-hmac-sha256-digest-using-user-defined-function-in-coldfusion/
Your need to call the function like this
<cfoutput>#LCase(HMAC_SHA256(iv & "." & Encrypted_Data, key))#</cfoutput>
Worked like a charm.
How to generate an HMAC in Java equivalent to a Python example?
HmacSHA1 seems to be the algorithm name you need:
SecretKeySpec keySpec = new SecretKeySpec(
"qnscAdgRlkIhAUPY44oiexBKtQbGY0orf7OV1I50".getBytes(),
"HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(keySpec);
byte[] result = mac.doFinal("foo".getBytes());
BASE64Encoder encoder = new BASE64Encoder();
System.out.println(encoder.encode(result));
produces:
+3h2gpjf4xcynjCGU5lbdMBwGOc=
Note that I've used sun.misc.BASE64Encoder
for a quick implementation here, but you should probably use something that doesn't depend on the Sun JRE. The base64-encoder in Commons Codec would be a better choice, for example.
Related Topics
Differencebetween ? and Object in Java Generics
Prevent Android Activity Dialog from Closing on Outside Touch
How to Allow Users to Check for the Latest App Version from Inside the App
Sort Data to Recyclerview Based on Latest Date from Firebase
Passing a JavaScript Object Using Addjavascriptinterface() on Android
Convert Utc to Current Locale Time
How to Resume an Interrupted Download - Part 2
Math.Random, Only Generating a 0
Mapstruct and Lombok Not Working Together
How to Set Background Color of an Activity to White Programmatically
Android Audio Fft to Retrieve Specific Frequency Magnitude Using Audiorecord
Dateformat Parse - Not Return Date in Utc