Initial Commit
All checks were successful
Locusworks Team/hash-validator/pipeline/head This commit looks good
All checks were successful
Locusworks Team/hash-validator/pipeline/head This commit looks good
This commit is contained in:
64
src/main/java/net/locusworks/hashvalidator/Args.java
Normal file
64
src/main/java/net/locusworks/hashvalidator/Args.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package net.locusworks.hashvalidator;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import com.beust.jcommander.Parameter;
|
||||
|
||||
import net.locusworks.hashvalidator.enums.HashType;
|
||||
import net.locusworks.hashvalidator.utils.HashTypeConverter;
|
||||
import net.locusworks.hashvalidator.utils.PathConverter;
|
||||
|
||||
public class Args {
|
||||
|
||||
@Parameter(names = {"-t", "--type"}, description = "Hash Type", order = 1, required = true, converter = HashTypeConverter.class)
|
||||
public HashType hashType;
|
||||
|
||||
@Parameter(names = {"-f", "--files"}, description = "List of files to check", order = 2, converter = PathConverter.class, variableArity = true)
|
||||
public List<Path> files;
|
||||
|
||||
@Parameter(names = {"-h", "--hashes"}, description = "List of hashes to go along with the files", order = 3, variableArity = true)
|
||||
public List<String> hashes;
|
||||
|
||||
@Parameter(names = {"-s", "--string"}, description = "String value to hash", order = 4)
|
||||
public String string;
|
||||
|
||||
@Parameter(names = {"--help"}, help = true, order = 99)
|
||||
private boolean help;
|
||||
|
||||
/**
|
||||
* @return the hashType
|
||||
*/
|
||||
public final HashType getHashType() {
|
||||
return hashType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the files
|
||||
*/
|
||||
public final List<Path> getFiles() {
|
||||
return files;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the files
|
||||
*/
|
||||
public final List<String> getHashes() {
|
||||
return hashes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the string
|
||||
*/
|
||||
public final String getString() {
|
||||
return string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the help
|
||||
*/
|
||||
public boolean isHelp() {
|
||||
return help;
|
||||
}
|
||||
|
||||
}
|
||||
90
src/main/java/net/locusworks/hashvalidator/Entry.java
Normal file
90
src/main/java/net/locusworks/hashvalidator/Entry.java
Normal file
@@ -0,0 +1,90 @@
|
||||
package net.locusworks.hashvalidator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import com.beust.jcommander.JCommander;
|
||||
|
||||
import net.locusworks.hashvalidator.enums.HashType;
|
||||
|
||||
public class Entry {
|
||||
|
||||
public static void main(String[] argv) throws IOException {
|
||||
Args args = new Args();
|
||||
JCommander commander = JCommander.newBuilder().addObject(args).programName("Hash Validator").build();
|
||||
try {
|
||||
commander.parse(argv);
|
||||
} catch (Exception ex) {
|
||||
System.err.println(ex.getMessage());
|
||||
commander.usage();
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
if (args.getString() != null && !args.getString().trim().isEmpty()) {
|
||||
String val = args.getString().trim();
|
||||
String hash = "";
|
||||
switch (args.getHashType()) {
|
||||
case MD5:
|
||||
hash = DigestUtils.md5Hex(val);
|
||||
break;
|
||||
case SHA1:
|
||||
hash = DigestUtils.sha1Hex(val);
|
||||
break;
|
||||
case SHA512:
|
||||
hash = DigestUtils.sha512Hex(val);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
System.out.println(String.format("The %s hash for %s is %s", args.getHashType(), val, hash));
|
||||
}
|
||||
|
||||
if (args.getFiles() == null || args.getFiles().size() == 0) return;
|
||||
|
||||
if(args.getHashes() != null && (args.getHashes().size() != args.getFiles().size()))
|
||||
throw new IllegalArgumentException("The number of entered hashes do not match the number of files entered");
|
||||
|
||||
if (args.getHashes() != null) {
|
||||
for (int i = 0; i < args.getFiles().size(); i++) {
|
||||
Path file = args.getFiles().get(i);
|
||||
String expectedHash = args.getHashes().get(i).trim();
|
||||
String actualHash = getHash(file, args.getHashType());
|
||||
if (actualHash.equalsIgnoreCase(expectedHash)) {
|
||||
System.out.println(String.format("%s matches the %s hash of %s", file.getFileName(), args.getHashType(), actualHash));
|
||||
} else {
|
||||
System.out.println(String.format("%s %s hash do not match. Expected: %s -> Actual: %s",
|
||||
file.getFileName(), args.getHashType(), expectedHash, actualHash));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for(Path file : args.getFiles()) {
|
||||
String hash = getHash(file, args.getHashType());
|
||||
System.out.println(String.format("%s %s hash is %s", file.getFileName(), args.getHashType(), hash));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static String getHash(Path file, HashType hashType) throws IOException {
|
||||
String hash = null;
|
||||
switch(hashType) {
|
||||
case MD5:
|
||||
hash = DigestUtils.md5Hex(Files.newInputStream(file));
|
||||
break;
|
||||
case SHA1:
|
||||
hash = DigestUtils.sha1Hex(Files.newInputStream(file));
|
||||
break;
|
||||
case SHA512:
|
||||
hash = DigestUtils.sha512Hex(Files.newInputStream(file));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package net.locusworks.hashvalidator.enums;
|
||||
|
||||
public enum HashType {
|
||||
MD5,
|
||||
SHA1,
|
||||
SHA512
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package net.locusworks.hashvalidator.utils;
|
||||
|
||||
import com.beust.jcommander.IStringConverter;
|
||||
|
||||
import net.locusworks.hashvalidator.enums.HashType;
|
||||
|
||||
public class HashTypeConverter implements IStringConverter<HashType>{
|
||||
|
||||
@Override
|
||||
public HashType convert(String value) {
|
||||
if (value == null || value.trim().isEmpty()) throw new IllegalArgumentException("Hash Type cannot be blank");
|
||||
|
||||
switch(value.trim().toLowerCase()) {
|
||||
case "md5":
|
||||
return HashType.MD5;
|
||||
case "sha1":
|
||||
case "sha-1":
|
||||
return HashType.SHA1;
|
||||
case "sha512":
|
||||
case "sha-512":
|
||||
return HashType.SHA512;
|
||||
default:
|
||||
throw new IllegalArgumentException("Hash Type cannot be blank");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package net.locusworks.hashvalidator.utils;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import com.beust.jcommander.IStringConverter;
|
||||
|
||||
public class PathConverter implements IStringConverter<Path> {
|
||||
|
||||
@Override
|
||||
public Path convert(String value) {
|
||||
Path path = Paths.get(value);
|
||||
|
||||
if (Files.notExists(path)) {
|
||||
throw new IllegalArgumentException(value + " does not exist. Please check entry");
|
||||
}
|
||||
|
||||
if (!Files.isRegularFile(path)) {
|
||||
throw new IllegalArgumentException(value + " is not a file. Please check entry");
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user