1

I have the following Java code to generate hashes based on input text.

package main;
import java.util.Scanner;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class String_Hash_Generator {
    public static void main(String args[]) throws NoSuchAlgorithmException {
        Scanner inputScanner = new Scanner(System.in);
        System.out.print("Input: ");
        String input = inputScanner.next();

        /* MD2 */
        MessageDigest objMD2 = MessageDigest.getInstance("MD2");
        byte[] bytMD2 = objMD2.digest(input.getBytes());
        BigInteger intNumMD2 = new BigInteger(1, bytMD2);
        String hcMD2 = intNumMD2.toString(16);
        while (hcMD2.length() < 32) {
            hcMD2 = "0" + hcMD2;
        }

        /* MD5 */
        MessageDigest objMD5 = MessageDigest.getInstance("MD5");
        byte[] bytMD5 = objMD5.digest(input.getBytes());
        BigInteger intNumMD5 = new BigInteger(1, bytMD5);
        String hcMD5 = intNumMD5.toString(16);
        while (hcMD5.length() < 32) {
            hcMD5 = "0" + hcMD5;
        }

        /* SHA-1 */
        MessageDigest objSHA1 = MessageDigest.getInstance("SHA-1");
        byte[] bytSHA1 = objSHA1.digest(input.getBytes());
        BigInteger intNumSHA1 = new BigInteger(1, bytSHA1);
        String hcSHA1 = intNumSHA1.toString(16);
        while (hcSHA1.length() < 40) {
            hcSHA1 = "0" + hcSHA1;
        }


        /* SHA-256 */
        MessageDigest objSHA256 = MessageDigest.getInstance("SHA-256");
        byte[] bytSHA256 = objSHA256.digest(input.getBytes());
        BigInteger intNumSHA256 = new BigInteger(1, bytSHA256);
        String hcSHA256 = intNumSHA256.toString(16);
        while (hcSHA256.length() < 64) {
            hcSHA256 = "0" + hcSHA256;
        }

        /* SHA-384 */

        MessageDigest objSHA384 = MessageDigest.getInstance("SHA-384");
        byte[] bytSHA384 = objSHA384.digest(input.getBytes());
        BigInteger intNumSHA384 = new BigInteger(1, bytSHA384);
        String hcSHA384 = intNumSHA384.toString(16);
        while (hcSHA384.length() < 96) {
            hcSHA384 = "0" + hcSHA384;
        }

        /* SHA-512 */
        MessageDigest objSHA512 = MessageDigest.getInstance("SHA-512");
        byte[] bytSHA512 = objSHA512.digest(input.getBytes());
        BigInteger intNumSHA512 = new BigInteger(1, bytSHA512);
        String hcSHA512 = intNumSHA512.toString(16);
        while (hcSHA512.length() < 128) {
            hcSHA512 = "0" + hcSHA512;
        }

        System.out.println("\nMD2: " + hcMD2
                        + "\nMD5: " + hcMD5
                        + "\nSHA-1: " + hcSHA1
                        + "\nSHA-256: " + hcSHA256
                        + "\nSHA-384: " + hcSHA384
                        + "\nSHA-512: " + hcSHA512);
    }
}

The input needs to be Scanner, because it is essential that it is run in a Command Prompt.

How could a file hash generator be created that takes the file path, such as C:\Program Files\WinRAR\Rar.exe and generates the hashes (MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512)?

Edit: The only solutions I have been able to find only use the file's name, not the entire path.

1 Answers1

0

First, you need a mechanism to print a byte[] as hex. From the top answer to How to convert a byte array to a hex string in Java? you can do,

final protected static char[] hexArray = "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] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}

Then, you might use Files.readAllBytes(Path) to read your file(s). Finally, iterate an array of hash algorithms to calculate each hash. Something like,

public static void main(String args[]) {
    Scanner inputScanner = new Scanner(System.in);
    String[] hashAlgos = { "MD2", "MD5", "SHA-1", "SHA-256", "SHA-384", "SHA-512" };
    System.out.print("Input: ");
    String input = inputScanner.next();
    try {
        byte[] fileContents = Files.readAllBytes(new File(input).toPath());
        for (String algo : hashAlgos) {
            MessageDigest md = MessageDigest.getInstance(algo);
            byte[] hash = md.digest(fileContents);
            System.out.printf("%s %s%n", algo, bytesToHex(hash));
        }
    } catch (NoSuchAlgorithmException | IOException e) {
        e.printStackTrace();
    }
}
Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thanks so much, this works perfectly. I have a question though. In my **text** hash generator, it results in hashes that are lowercase, such as: `098f6bcd4621d373cade4e832627b4f6` - while other generators result in hashes that are uppercase, such as: `098F6BCD4621D373CADE4E832627B4F6`. Is there a difference, and which one should be used (or is used more often)? – Eddie T. Nathan Aug 03 '16 at 01:19
  • In base 16, they're equivalent. Use whichever you prefer. But, as posted, you should be getting upper case letters. – Elliott Frisch Aug 03 '16 at 01:26
  • Yep, in the file hash generator I am getting uppercase letters. Thanks. – Eddie T. Nathan Aug 03 '16 at 01:27