public static String HMACSHA256(final String strText, final String strKey) {
String strResult = null;
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(strKey.getBytes(), "HmacSHA256");
sha256_HMAC.init(secretKey);
byte[] hash = sha256_HMAC.doFinal(strText.getBytes());
//strResult = new String(Hex.encodeHex(hash));
// strResult = byte2hex(hash);
strResult = toHexString(hash);
return strResult;
} catch (NoSuchAlgorithmException e) {
return strResult;
} catch (InvalidKeyException e) {
return strResult;
}
}
private static String toHexString(byte[]b){
StringBuilder hs = new StringBuilder();
String stmp;
for(int n = 0;b!=null&&n<b.length;n++){
stmp = Integer.toHexString(b[n]&0XFF);
if (stmp.length() ==1)
hs.append('0');
hs.append(stmp);
}
return hs.toString().toLowerCase();
}