From 4c72360b5fc71abbb65ef4c4f3cec432bf74b8f6 Mon Sep 17 00:00:00 2001 From: Paul Sterl Date: Sun, 15 Nov 2020 22:48:55 +0100 Subject: [PATCH] added a hash cli tool --- hash-cli/pom.xml | 59 +++++++++++++++++++ .../main/java/org/sterl/hash/HashMain.java | 57 ++++++++++++++++++ pom.xml | 1 + 3 files changed, 117 insertions(+) create mode 100644 hash-cli/pom.xml create mode 100644 hash-cli/src/main/java/org/sterl/hash/HashMain.java diff --git a/hash-cli/pom.xml b/hash-cli/pom.xml new file mode 100644 index 0000000..a354961 --- /dev/null +++ b/hash-cli/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + + org.sterl.hash + hash-root + 0.1.1-SNAPSHOT + ../pom.xml + + + hash-cli + hash-cli + + + + org.sterl.hash + hash-lib + ${project.version} + + + org.projectlombok + lombok + provided + + + commons-cli + commons-cli + 1.4 + + + org.junit.jupiter + junit-jupiter + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 2.4.0 + + + + repackage + + + org.sterl.hash.HashMain + + + + + + + diff --git a/hash-cli/src/main/java/org/sterl/hash/HashMain.java b/hash-cli/src/main/java/org/sterl/hash/HashMain.java new file mode 100644 index 0000000..4d5aa52 --- /dev/null +++ b/hash-cli/src/main/java/org/sterl/hash/HashMain.java @@ -0,0 +1,57 @@ +package org.sterl.hash; + +import java.util.Arrays; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Options; + +public class HashMain { + + private final static Options options = new Options(); + static { + options.addOption("a", true, "Algrorithmus one of " + Arrays.toString(Algorithm.values())); + //options.addOption("m", "method", false, "HTTP method to use."); + //options.addOption("p", "payload", false, "String payload to send."); + options.addOption("p", "password", true, "Password to hash."); + options.addOption("h", "hash", true, "The hash to check the passwort against. Note you may have to escape $ with an \\."); + } + public static void main(String[] args) { + Algorithm algorithm = Algorithm.BCrypt; + String password = ""; + String hash = null; + + if (args.length > 1) { + final CommandLineParser parser = new DefaultParser(); + try { + final CommandLine cmd = parser.parse(options, args); + + if (cmd.hasOption('a')) { + algorithm = Algorithm.valueOf(cmd.getOptionValue('a')); + } + password = cmd.getOptionValue('p'); + + if (cmd.hasOption('h')) { + hash = cmd.getOptionValue('h'); + } + } catch (Exception e) { + System.out.println(e.getMessage()); + System.exit(-1); + } + } else if (args.length == 1) { + password = args[0]; + } else { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp("hash-cli", options); + System.exit(-1); + } + if (hash == null) { + System.out.println(new BCryptPbkdf2PasswordHash(algorithm).encode(password)); + } else { + System.out.println(new BCryptPbkdf2PasswordHash(algorithm).matches(password, hash)); + } + } + +} diff --git a/pom.xml b/pom.xml index deb42a0..821b316 100755 --- a/pom.xml +++ b/pom.xml @@ -56,6 +56,7 @@ hash-lib + hash-cli jee-hash-lib spring-hash-lib