2016-08-02 31 views
1

我有以下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); 
    } 
} 

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

怎能一个文件散列发生器被创建,是以文件路径,如C:\Program Files\WinRAR\Rar.exe并生成散列(MD2,MD5,SHA-1,SHA-256,SHA-384,SHA-512)?

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

回答

0

首先,您需要一种机制以十六进制形式打印byte[]。从上面的答案How to convert a byte array to a hex string in Java?你可以做,

final protected static char[] hexArray = "ABCDEF".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)阅读您的文件(S)。最后,迭代一个哈希算法数组来计算每个哈希。类似的,

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

非常感谢,这工作完美。但我有个问题。在我的** text **哈希生成器中,它会生成小写的哈希,例如:'098f6bcd4621d373cade4e832627b4f6' - 而其他生成器会生成大写的哈希,例如:'098F6BCD4621D373CADE4E832627B4F6'。是否有区别,哪一个应该使用(或更常用)? –

+0

在基数16中,它们是等价的。使用任何你喜欢的。但是,正如发布的那样,您应该收到大写字母。 –

+0

是的,在文件哈希生成器中,我得到大写字母。谢谢。 –

相关问题