使用 java 中的密钥计算 HMAC-SHA512
Compute HMAC-SHA512 with secret key in java
我想准确地构建一个函数来生成一个带有密钥的 HMAC,就像这个站点提供的那样:
http://www.freeformatter.com/hmac-generator.html
Java 8 库仅提供 MessageDigest 和 KeyGenerator,它们都只支持最高 SH256。
此外,Google 没有给我生成 HMAC 的实现的任何结果。
有人知道实现吗?
我有生成普通 SH256 的代码,但我想这对我帮助不大:
public static String get_SHA_512_SecurePassword(String passwordToHash) throws Exception {
String generatedPassword = null;
MessageDigest md = MessageDigest.getInstance("SHA-512");
byte[] bytes = md.digest(passwordToHash.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
System.out.println(generatedPassword);
return generatedPassword;
}
希望对您有所帮助:
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class Test1 {
private static final String HMAC_SHA512 = "HmacSHA512";
public static void main(String[] args) {
Mac sha512Hmac;
String result;
final String key = "Welcome1";
try {
final byte[] byteKey = key.getBytes(StandardCharsets.UTF_8);
sha512Hmac = Mac.getInstance(HMAC_SHA512);
SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA512);
sha512Hmac.init(keySpec);
byte[] macData = sha512Hmac.doFinal("My message".getBytes(StandardCharsets.UTF_8));
// Can either base64 encode or put it right into hex
result = Base64.getEncoder().encodeToString(macData);
//result = bytesToHex(macData);
} catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException e) {
e.printStackTrace();
} finally {
// Put any cleanup here
System.out.println("Done");
}
}
}
要从字节数组转换为十六进制,请参考这个 Whosebug 答案:here
最简单的方法可以是-
private static final String HMAC_SHA512 = "HmacSHA512";
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}
public static String calculateHMAC(String data, String key)
throws SignatureException, NoSuchAlgorithmException, InvalidKeyException
{
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), HMAC_SHA512);
Mac mac = Mac.getInstance(HMAC_SHA512);
mac.init(secretKeySpec);
return toHexString(mac.doFinal(data.getBytes()));
}
public static void main(String[] args) throws Exception {
String hmac = calculateHMAC("data", "key");
System.out.println(hmac);
}
您可以将 HMAC_SHA512 变量更改为任何 Mac 算法,代码将以相同的方式工作。
你也可以使用 Apache Commons codec:
的小包装器
import static org.apache.commons.codec.digest.HmacAlgorithms.HMAC_SHA_512;
import org.apache.commons.codec.digest.HmacUtils;
public class HmacService {
private String sharedSecret;
public HmacService(String sharedSecret) {
this.sharedSecret = sharedSecret;
}
public String calculateHmac(String data) {
return new HmacUtils(HMAC_SHA_512, sharedSecret).hmacHex(data);
}
public boolean checkHmac(String data, String hmacHex) {
return calculateHmac(data).equals(hmacHex);
}
}
您可以使用 Caesar(版本 0.6.0
或更高版本):
int blockSize = 128;
String secretKey = "My secret key";
String message = "Message to hash";
System.out.println(
new Hmac(
new ImmutableMessageDigest(
MessageDigest.getInstance("SHA-512")
),
blockSize,
new PlainText(secretKey),
new PlainText(message)
).asHexString()
);
我想准确地构建一个函数来生成一个带有密钥的 HMAC,就像这个站点提供的那样:
http://www.freeformatter.com/hmac-generator.html
Java 8 库仅提供 MessageDigest 和 KeyGenerator,它们都只支持最高 SH256。
此外,Google 没有给我生成 HMAC 的实现的任何结果。
有人知道实现吗?
我有生成普通 SH256 的代码,但我想这对我帮助不大:
public static String get_SHA_512_SecurePassword(String passwordToHash) throws Exception {
String generatedPassword = null;
MessageDigest md = MessageDigest.getInstance("SHA-512");
byte[] bytes = md.digest(passwordToHash.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
System.out.println(generatedPassword);
return generatedPassword;
}
希望对您有所帮助:
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class Test1 {
private static final String HMAC_SHA512 = "HmacSHA512";
public static void main(String[] args) {
Mac sha512Hmac;
String result;
final String key = "Welcome1";
try {
final byte[] byteKey = key.getBytes(StandardCharsets.UTF_8);
sha512Hmac = Mac.getInstance(HMAC_SHA512);
SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA512);
sha512Hmac.init(keySpec);
byte[] macData = sha512Hmac.doFinal("My message".getBytes(StandardCharsets.UTF_8));
// Can either base64 encode or put it right into hex
result = Base64.getEncoder().encodeToString(macData);
//result = bytesToHex(macData);
} catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException e) {
e.printStackTrace();
} finally {
// Put any cleanup here
System.out.println("Done");
}
}
}
要从字节数组转换为十六进制,请参考这个 Whosebug 答案:here
最简单的方法可以是-
private static final String HMAC_SHA512 = "HmacSHA512";
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}
public static String calculateHMAC(String data, String key)
throws SignatureException, NoSuchAlgorithmException, InvalidKeyException
{
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), HMAC_SHA512);
Mac mac = Mac.getInstance(HMAC_SHA512);
mac.init(secretKeySpec);
return toHexString(mac.doFinal(data.getBytes()));
}
public static void main(String[] args) throws Exception {
String hmac = calculateHMAC("data", "key");
System.out.println(hmac);
}
您可以将 HMAC_SHA512 变量更改为任何 Mac 算法,代码将以相同的方式工作。
你也可以使用 Apache Commons codec:
的小包装器import static org.apache.commons.codec.digest.HmacAlgorithms.HMAC_SHA_512;
import org.apache.commons.codec.digest.HmacUtils;
public class HmacService {
private String sharedSecret;
public HmacService(String sharedSecret) {
this.sharedSecret = sharedSecret;
}
public String calculateHmac(String data) {
return new HmacUtils(HMAC_SHA_512, sharedSecret).hmacHex(data);
}
public boolean checkHmac(String data, String hmacHex) {
return calculateHmac(data).equals(hmacHex);
}
}
您可以使用 Caesar(版本 0.6.0
或更高版本):
int blockSize = 128;
String secretKey = "My secret key";
String message = "Message to hash";
System.out.println(
new Hmac(
new ImmutableMessageDigest(
MessageDigest.getInstance("SHA-512")
),
blockSize,
new PlainText(secretKey),
new PlainText(message)
).asHexString()
);