Finding Hash of the message using HmacSHA1 algorithm

Description

Hash of input message is generated using the HmacSHA1 algorithm that is available in javax.crypto.Mac package. Generated hash is not irreversible. This code can be used in the process of integrity verification process of message (checking for alteration of message) while transmitting data over the network which is vulnerable to various attacks.

  • Filename: FindHash.java

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Random ;
import javax.swing.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.Formatter;

class FindHash {

private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

public FindHash() {
try {
String inputMessage=JOptionPane.showInputDialog(null,"Enter message to hash");
String hmac_message = calculateRFC2104HMAC(inputMessage, "key1");
JOptionPane.showMessageDialog(null,"Hash of the message: "+hmac_message);
}
catch(Exception e) {
System.out.println(e);
}
}

//************* Coding for Hashing *******************

private static String toHexString(byte[] bytes)
{
Formatter formatter = new Formatter();
for (byte b : bytes)
{
formatter.format("%02x", b);

}
return formatter.toString();
}

public static String calculateRFC2104HMAC(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException
{
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
return toHexString(mac.doFinal(data.getBytes()));
}

public static void main(String args[]) {
FindHash fh = new FindHash();
}

}