适用 Java / Android

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

// https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}

//https://stackoverflow.com/questions/7124735/hmac-sha256-algorithm-for-signature-calculation
private String HmacSHA256(String content, String salt){
String result = "";
try {
String secret = salt;
String message = content;

Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secret_key);

byte[] temp = sha256_HMAC.doFinal(message.getBytes());
result = bytesToHex(temp);
}
catch (Exception e){
System.out.println("Error");
}
return result;
}

更多参考