package org.rmatil.sync.event.aggregator.core.hashing;

import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.google.common.io.Files;
import org.rmatil.sync.event.aggregator.config.HashingAlgorithm;

import java.io.File;
import java.io.IOException;

/**
 * Hashing utilities for files
 */
public class Hash {

    /**
     * Hashes the given file with the given hashing algorithm
     *
     * @param hashingAlgorithm The hashing algorithm to use
     * @param file             The file to hash
     *
     * @return The hash
     *
     * @throws IOException If the hashing fails
     */
    public static String hash(HashingAlgorithm hashingAlgorithm, File file)
            throws IOException {
        String hash = null;
        HashCode hc = null;

        switch (hashingAlgorithm) {
            case SHA_1:
                hc = Files.hash(file, Hashing.sha1());
            case SHA_256:
                hc = Files.hash(file, Hashing.sha256());
                break;
            case SHA_512:
                hc = Files.hash(file, Hashing.sha512());
                break;
            default:
                hc = Files.hash(file, Hashing.sha256());
                break;
        }

        // TODO: how is this applied to directories

        hash = hc.toString();

        return hash;
    }
}

@@ -250,16 +250,54 @@
 o.File;%0A
+import java.io.FileNotFoundException;%0A
 import j
@@ -627,16 +627,26 @@
 ception 
+          
 If the h
@@ -658,16 +658,86 @@
 g fails%0A
+     * @throws FileNotFoundException If the given file does not exist%0A
      */%0A
@@ -845,24 +845,25 @@
 ption %7B%0A
+%0A
         
 String h
@@ -858,27 +858,142 @@
     
-String hash = null;
+if (!file.exists()) %7B%0A            throw new FileNotFoundException(file.getAbsolutePath() + %22 (No such file or directory)%22);%0A        %7D%0A
 %0A   
@@ -1129,24 +1129,47 @@
 ng.sha1());%0A
+                break;%0A
             
@@ -1372,109 +1372,8 @@
 ak;%0A
-            default:%0A                hc = Files.hash(file, Hashing.sha256());%0A                break;%0A
     
@@ -1444,14 +1444,14 @@
     
-hash =
+return
  hc.
@@ -1465,30 +1465,8 @@
 g();
-%0A%0A        return hash;
 %0A   

Fix missing break statement