0

I am using a Java program to generate file hashes. Here is the code:

package main;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class File_Hash_Generator {
    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);
    }
    public static void main(String args[]) throws IOException {
    Scanner inputScanner = new Scanner(System.in);
    String[] hashAlgos = { "MD2", "MD5", "SHA-1", "SHA-256", "SHA-384", "SHA-512" };
    System.out.print("File Path: ");
    String input = inputScanner.next();
    input = input.replace("\"", "");
    System.out.println();
    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) {
    }
}
}

For some reason when I enter in a path where the file or folder has a space, such as:

"C:\Users\User\Downloads\ToBeScanned\DAY N NIGHT.mp3"

The program does not run correctly and instead just terminates without generating any hashes.

EDIT: It was brought up that my question is a duplicate of this question. However, I don't know how this other answer could help me in this instance. If someone could explain to me how to change my code using this separate answer to fix my problem, I'd be grateful.

Any help to fix this is very much appreciated, thanks.

Community
  • 1
  • 1
  • Possible duplicate of [Java read file with whitespace in its path](http://stackoverflow.com/questions/9128288/java-read-file-with-whitespace-in-its-path) – Aleksandar Đokić Aug 05 '16 at 00:33
  • 1
    This is a not a smart thing to do `} catch (NoSuchAlgorithmException | IOException e) { }` do not silently swallow your exceptions – Scary Wombat Aug 05 '16 at 00:37
  • 1
    Just a side question. How does the FIles.readAllBytes[...} work when you replace all the "\" with ""? – sascha10000 Aug 05 '16 at 00:41
  • 1
    @sascha10000 In some instances when I am copying a file path, the path is surrounded by double quotes. What you're talking about is when I remove those surrounding quotes. It does not impact the function of the program... And also, `replace("\"", "")` does not replace slashes, it replaces double quotes. – Paul J. Cotton Aug 05 '16 at 00:51
  • @AleksandarĐokić If you could explain how I could fix my code using that question's chosen answer, I would appreciate it. – Paul J. Cotton Aug 05 '16 at 01:01
  • No sorry it was just for my understanding but as you stated I just overlooked that the backslash was follwed by TWO " therefore I assumed that you replace the \ which would make no sense. – sascha10000 Aug 05 '16 at 01:09
  • java should handle whietapce in filenames OK, so the issue is either an exception is thrown and you are not logging it, or the value of `toPath()` needs to be logged and investigated. – Scary Wombat Aug 05 '16 at 01:19
  • @ScaryWombat How can I "log and investigate" the value of toPath()? – Paul J. Cotton Aug 05 '16 at 01:45
  • like you do with with other things `System.out.print(new File(input).toPath());` – Scary Wombat Aug 05 '16 at 01:47

1 Answers1

0

Instead of doing: String input = inputScanner.next();

I can do: String input = inputScanner.nextLine();

This allows for reading the spaces. The solution was pointed out in this question.

Community
  • 1
  • 1