2016-08-02 70 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

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