· 9 years ago · Dec 02, 2016, 05:10 PM
1private String getHmac(String textToSign, String apiKey) {
2 String result = "";
3 try {
4 Charset utf8CharSet = Charset.forName("UTF-8");
5 Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
6 SecretKeySpec secret_key = new javax.crypto.spec.SecretKeySpec(utf8CharSet.encode(apiKey).array(), "HmacSHA256");
7 sha256_HMAC.init(secret_key);
8 byte[] mac_data = sha256_HMAC.doFinal(utf8CharSet.encode(textToSign).array());
9 for (byte element : mac_data)
10 {
11 result += Integer.toString((element & 0xff) + 0x100, 16).substring(1);
12 }
13 } catch (Exception e) {
14 result = e.toString();
15 }
16 return result;
17}