private static boolean verifyHashToAll(String filePath, String hashStoragePath) { Map<String, String> storage = parseStorage(hashStoragePath); String apache_sha256=""; File file = null; try { file = new File(filePath); FileInputStream in = new FileInputStream(file); apache_sha256 = DigestUtils.sha256Hex(in); } catch (FileNotFoundException ex) { } catch (IOException ex) { } if(storage.containsKey(file.getPath())) { if(storage.get(file.getPath()).equals(apache_sha256)) { return true; } } return false; } private static Map<String, String> parseStorage(String storage){ HashMap<String, String> data = new HashMap<>(); String line = null; try { BufferedReader bufferedReader = new BufferedReader( new FileReader(storage)); while((line = bufferedReader.readLine()) != null) { String[] items = line.split(","); for (int i = 0; i < items.length; i++) { data.put(items[0], items[1]);} } bufferedReader.close(); } catch(FileNotFoundException ex) { System.out.println( "Unable to open file '" + storage + "'"); } catch(IOException ex) { System.out.println( "Error reading file '" + storage + "'"); } return data; } public static void main(String args[]) { final File root = new File("files"); search(root); System.out.println("Does the file's checksum matches the expected one? " + verifyHash("files/firstFolder/names.txt", "hashfil.txt")); System.out.println("Does the file's checksum matches the expected one? " + verifyHash("files/secondFolder/phones.txt", "hashfil.txt")); System.out.println("Does the file's checksum matches the expected all files? " + verifyHashToAll("files", "hashfil.txt"));} 
  • complete the question, the essence is not clear - Artem Konovalov
  • I have a files folder and there are two folders in it and each has its own text document (files / firstFolder / names.txt and files / secondFolder / phones.txt). The program reads and writes the hash value SHA256 in hashfile.txt. Then I need to check the resulting value of this text document. First of all (verifyHash ("files / firstFolder / names.txt", "hashfil.txt")) - this works, and then verifyHashToAll ("files", "hashfil.txt") - this is the problem - MarinaGB
  • What exactly fails, what error? - Artem Konovalov
  • create and check the 3rd sout verifyHashToAll ("files", "hashfil.txt"). The first two sout give true. Last false - MarinaGB
  • It may be necessary to calculate the hash not for a directory but for each file separately? - Artem Konovalov

0