Skip to content

Commit

Permalink
added a hash cli tool
Browse files Browse the repository at this point in the history
  • Loading branch information
sterlp committed Nov 15, 2020
1 parent 79578aa commit 4c72360
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
59 changes: 59 additions & 0 deletions hash-cli/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.sterl.hash</groupId>
<artifactId>hash-root</artifactId>
<version>0.1.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>hash-cli</artifactId>
<name>hash-cli</name>

<dependencies>
<dependency>
<groupId>org.sterl.hash</groupId>
<artifactId>hash-lib</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.0</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>org.sterl.hash.HashMain</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
57 changes: 57 additions & 0 deletions hash-cli/src/main/java/org/sterl/hash/HashMain.java
Original file line number Diff line number Diff line change
@@ -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));
}
}

}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

<modules>
<module>hash-lib</module>
<module>hash-cli</module>
<module>jee-hash-lib</module>
<module>spring-hash-lib</module>
</modules>
Expand Down

0 comments on commit 4c72360

Please sign in to comment.