Skip to content

Commit

Permalink
Initial Commit with an already fully working Version
Browse files Browse the repository at this point in the history
  • Loading branch information
DManstrator committed Oct 13, 2018
0 parents commit 6bda019
Show file tree
Hide file tree
Showing 17 changed files with 606 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<accessrules>
<accessrule kind="accessible" pattern="javafx/**"/>
</accessrules>
</classpathentry>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" path="src/test/java/"/>
<classpathentry kind="src" path="src/test/resources"/>
<classpathentry kind="lib" path="lib/commons-io-2.6.jar" sourcepath="lib/commons-io-2.6-sources.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/FileHasher/lib/commons-io-2.6-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/bin/
/lib/
/src/main/resources/*
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>FileHasher</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# File-Hasher
A Program for mass-checking the SHA-512 Hash of whole Subdirectories.

## How to use
Go to the [Releases-Tab](../../releases) and download the attached JAR-File. Put the file into a folder wherever you want but **don't let them in the Downloads Folder** since all Output Files will be created in that folder.

## Pictures
Here some pictures that you know how the GUI looks like:

### Program
![Menu on Start](img/menu_start.png)<br />
![Ready to Go](img/menu_ready.png)<br />
![Request Done](img/menu_done.png)

### Icon
![Icon in the Task-Bar](img/icon-taskbar.png)

## FAQ
- Q: How to use it?<br />
A: Read the text above. ;)
Binary file added img/icon-taskbar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/menu_done.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/menu_ready.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/menu_start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
175 changes: 175 additions & 0 deletions src/main/java/tk/dmanstrator/filehasher/Hasher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package tk.dmanstrator.filehasher;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;

/**
* Generates SHA-512 Hashes. Works for all files of a given Folder or for a given File.
*
* @author DManstrator
*
*/
public class Hasher {

/**
* Encoding for the File in case a filename or the Directory has Umlauts.
*/
private static final String ENCODING = "UTF-8";

/**
* Hashing Method. Creates an Output File and returns the Path to it.
*
* @param mainFolderName
* Folder to hash
* @throws IllegalArgumentException
* When the Path is not a folder
* @throws FileNotFoundException
* When the Output File couldn't be created
* @throws UnsupportedEncodingException
* When the Encoding for the Output File is Unsupported
*/
public String hash(String mainFolderName) throws IllegalArgumentException, FileNotFoundException, UnsupportedEncodingException {
File mainPath = new File(mainFolderName);
if (!mainPath.isDirectory()) {
throw new IllegalArgumentException(String.format("Given Folder '%s' is not a folder, re-check that!", mainFolderName));
}

Map<File, String> filesWithHashes = getHashesOfFiles(mainFolderName);

String tmpFolderName = getFolderName(mainFolderName); // tmp for using folderName in Stream#map
String folderName = tmpFolderName != null ? tmpFolderName : "null";

StringBuilder builder = new StringBuilder("Path to scan: " + mainFolderName + System.lineSeparator());
String hashes = filesWithHashes.entrySet().stream()
.map(entry -> String.format("%s: %s", entry.getKey().getPath().substring(mainFolderName.length() - folderName.length()),
entry.getValue()))
.collect(Collectors.joining(System.lineSeparator()));

String output = builder.append(hashes).toString();

String dateTime = getDateTime();
File outputFile = new File(String.format("%s-Hashes_%s.txt", folderName, dateTime));
try {
PrintWriter writer = new PrintWriter(outputFile.getName(), ENCODING);
writer.write(output);
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
throw new FileNotFoundException(String.format("An error occured while creating the output file '%s', "
+ "make sure the program has the rights to do so!", outputFile.getAbsolutePath()));
} catch (UnsupportedEncodingException e) {
throw new UnsupportedEncodingException(String.format("An error occured because '%s' is an Unsupported Encoding!", ENCODING));
}
return outputFile.getAbsolutePath();
}

/**
* Computes a SHA-512 Hash for every file of the given folder path.
*
* @param mainFolderName
* Path to the starting Directory
* @return a Map with the File as the Key and the SHA-512 Hash as the Value
*/
public Map<File, String> getHashesOfFiles(String mainFolderName) {
return getHashesOfFiles(new File(mainFolderName));
}

/**
* Computes a SHA-512 Hash for every file of the given folder path.
*
* @param mainFolder
* Path as File to the starting Directory
* @return a Map with the File as the Key and the SHA-512 Hash as the Value
*/
public Map<File, String> getHashesOfFiles(File mainFolder) {
if (!mainFolder.isDirectory()) {
return new HashMap<>();
}
Collection<File> listFiles = FileUtils.listFiles(mainFolder, null, true);
return listFiles.stream().collect(Collectors.toMap(p -> p, p -> getHashOfFile(p)));
}

/**
* Generates a SHA-512 Hash of a File.
*
* @param filename
* Name of the File to get the Hash from
* @return a SHA-512 Hash of given File or <code>null</code> if something
* went wrong
*/
public String getHashOfFile(String filename) {
return getHashOfFile(new File(filename));
}

/**
* Generates a SHA-512 Hash of a File.
*
* @param file
* File to get the Hash from
* @return a SHA-512 Hash of given File or <code>null</code> if something
* went wrong
* @see <a href=
* "https://stackoverflow.com/a/33085670">https://stackoverflow.com/a/33085670</a>
*/
public String getHashOfFile(File file) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-512");
byte[] digest = md.digest(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
StringBuilder sb = new StringBuilder();
for(int i=0; i< digest.length; i++){
sb.append(Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
} catch (NoSuchAlgorithmException | IOException e) {
return null;
}
}

/**
* Gets the deepest Folder Name from a given Path.
*
* @param mainFolderName
* Main Folder Name
* @return the deepest Folder Name from a given Path or <code>null</code> if
* no folder could be retrieved from the given Folder Name.
*/
private String getFolderName(String mainFolderName) {
int lastIndex = mainFolderName.lastIndexOf('/');
if (lastIndex == -1) { // Maybe Windows
int lastIndexWindows = mainFolderName.lastIndexOf('\\');
if (lastIndexWindows != -1) {
lastIndex = lastIndexWindows;
} else {
return null; // No slashes at all
}
}
return mainFolderName.substring(lastIndex + 1, mainFolderName.length());
}

/**
* Gets the current Time in Format <code>yyyy-MM-dd_HH-mm-ss</code>.
*
* @return the current Time as a readable String
*/
private String getDateTime() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
return new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(cal.getTime()).toString();
}

}
Loading

0 comments on commit 6bda019

Please sign in to comment.