Merge pull request #11392 from hmdrzsharifi/bael-4604
Bael-4604: HMAC in Java (In Conjnction With SHA-*, MD5, Etc.)
This commit is contained in:
commit
bc58e50b59
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.hmac;
|
||||
|
||||
import org.apache.commons.codec.digest.HmacUtils;
|
||||
import org.bouncycastle.crypto.Digest;
|
||||
import org.bouncycastle.crypto.digests.MD5Digest;
|
||||
import org.bouncycastle.crypto.digests.SHA256Digest;
|
||||
import org.bouncycastle.crypto.digests.SHA384Digest;
|
||||
import org.bouncycastle.crypto.digests.SHA512Digest;
|
||||
import org.bouncycastle.crypto.macs.HMac;
|
||||
import org.bouncycastle.crypto.params.KeyParameter;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
|
||||
public class HMACUtil {
|
||||
|
||||
public static String hmacWithJava(String algorithm, String data, String key)
|
||||
throws NoSuchAlgorithmException, InvalidKeyException {
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), algorithm);
|
||||
Mac mac = Mac.getInstance(algorithm);
|
||||
mac.init(secretKeySpec);
|
||||
return bytesToHex(mac.doFinal(data.getBytes()));
|
||||
}
|
||||
|
||||
public static String hmacWithApacheCommons(String algorithm, String data, String key) {
|
||||
String hmac = new HmacUtils(algorithm, key).hmacHex(data);
|
||||
return hmac;
|
||||
}
|
||||
|
||||
public static String hmacWithBouncyCastle(String algorithm, String data, String key) {
|
||||
Digest digest = getHashDigest(algorithm);
|
||||
HMac hMac = new HMac(digest);
|
||||
hMac.init(new KeyParameter(key.getBytes()));
|
||||
byte[] hmacIn = data.getBytes();
|
||||
hMac.update(hmacIn, 0, hmacIn.length);
|
||||
byte[] hmacOut = new byte[hMac.getMacSize()];
|
||||
hMac.doFinal(hmacOut, 0);
|
||||
return bytesToHex(hmacOut);
|
||||
}
|
||||
|
||||
private static Digest getHashDigest(String algorithm) {
|
||||
switch (algorithm) {
|
||||
case "HmacMD5":
|
||||
return new MD5Digest();
|
||||
case "HmacSHA256":
|
||||
return new SHA256Digest();
|
||||
case "HmacSHA384":
|
||||
return new SHA384Digest();
|
||||
case "HmacSHA512":
|
||||
return new SHA512Digest();
|
||||
}
|
||||
return new SHA256Digest();
|
||||
}
|
||||
|
||||
public static String bytesToHex(byte[] hash) {
|
||||
StringBuilder hexString = new StringBuilder(2 * hash.length);
|
||||
for (byte h : hash) {
|
||||
String hex = Integer.toHexString(0xff & h);
|
||||
if (hex.length() == 1)
|
||||
hexString.append('0');
|
||||
hexString.append(hex);
|
||||
}
|
||||
return hexString.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.hmac;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class HMACUtilUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDataAndKeyAndAlgorithm_whenHmacWithJava_thenSuccess()
|
||||
throws NoSuchAlgorithmException, InvalidKeyException {
|
||||
|
||||
//given
|
||||
String hmacSHA256Value = "5b50d80c7dc7ae8bb1b1433cc0b99ecd2ac8397a555c6f75cb8a619ae35a0c35";
|
||||
String hmacSHA256Algorithm = "HmacSHA256";
|
||||
String data = "baeldung";
|
||||
String key = "123456";
|
||||
|
||||
//when
|
||||
String result = HMACUtil.hmacWithJava(hmacSHA256Algorithm, data, key);
|
||||
|
||||
//then
|
||||
assertEquals(hmacSHA256Value, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataAndKeyAndAlgorithm_whenHmacWithApacheCommons_thenSuccess() {
|
||||
|
||||
//given
|
||||
String hmacMD5Value = "621dc816b3bf670212e0c261dc9bcdb6";
|
||||
String hmacMD5Algorithm = "HmacMD5";
|
||||
String data = "baeldung";
|
||||
String key = "123456";
|
||||
|
||||
//when
|
||||
String result = HMACUtil.hmacWithApacheCommons(hmacMD5Algorithm, data, key);
|
||||
|
||||
//then
|
||||
assertEquals(hmacMD5Value, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataAndKeyAndAlgorithm_whenHmacWithBouncyCastle_thenSuccess() {
|
||||
|
||||
//given
|
||||
String hmacSHA512Value = "b313a21908df55c9e322e3c65a4b0b7561ab1594ca806b3affbc0d769a1" +
|
||||
"290c1922aa6622587bea3c0c4d871470a6d06f54dbd20dbda84250e2741eb01f08e33";
|
||||
String hmacSHA512Algorithm = "HmacSHA512";
|
||||
String data = "baeldung";
|
||||
String key = "123456";
|
||||
|
||||
//when
|
||||
String result = HMACUtil.hmacWithBouncyCastle(hmacSHA512Algorithm, data, key);
|
||||
|
||||
//then
|
||||
assertEquals(hmacSHA512Value, result);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue