Initial Commit
All checks were successful
Locusworks Team/hash-validator/pipeline/head This commit looks good

This commit is contained in:
2020-06-11 12:33:45 -05:00
commit c7334711f9
9 changed files with 546 additions and 0 deletions

View 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;
}
}

View 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;
}
}

View File

@@ -0,0 +1,7 @@
package net.locusworks.hashvalidator.enums;
public enum HashType {
MD5,
SHA1,
SHA512
}

View File

@@ -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");
}
}
}

View File

@@ -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;
}
}