Below is the method :
void generateHMCSHA1(){Output :
int counter= 1;
String privateKey = "0123456789ABCDEF0123456789ABCDEF";
byte[] result = null;
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-1");
byte[] keyBytes = md.digest(privateKey.getBytes("UTF-8"));
SecretKey sk = new SecretKeySpec(keyBytes, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(sk);
result = mac.doFinal(String.valueOf(counter).getBytes("US_ASCII"));
StringBuilder sb = new StringBuilder();
for (byte b : result) {
sb.append(String.format("%02X ", b));
}
System.out.println(" Key: " + privateKey + "\n");
System.out.println(" Results: " +sb );
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Key: 0123456789ABCDEF0123456789ABCDEF
Results: 38 92 62 29 80 04 C9 83 EE 87 97 B0 C0 FD 77 4C 69 F3 82
No comments:
Post a Comment