使用文件路径在命令提示符中的文件哈希生成器

时间:2016-08-02 23:48:08

标签: java file hash command-prompt

我有以下Java代码根据输入文本生成哈希。

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);
    }
}

输入需要是Scanner,因为它必须在命令提示符下运行。

如何创建一个文件哈希生成器来获取文件路径,例如C:\Program Files\WinRAR\Rar.exe并生成哈希值(MD2,MD5,SHA-1,SHA-256,SHA- 384,SHA-512)?

编辑:我能找到的唯一解决方案只使用文件的名称,而不是整个路径。

1 个答案:

答案 0 :(得分:0)

首先,您需要一种机制来将byte[]打印为十六进制。从最快的答案How to convert a byte array to a hex string in Java?,你可以做到,

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);
}

然后,您可以使用Files.readAllBytes(Path)来读取您的文件。最后,迭代一组哈希算法来计算每个哈希值。像,

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();
    }
}