Skip to content

Commit 3b92c6e

Browse files
Replaces rust bcrypt tool with a java implementation (#21)
Replaced rust implementation of bcrypt hasher for NiFi passwords with Java tool due to #20 This uses the same library as NiFi internally (in fact BCryptPasswordEncoder.java has been copied from NiFi) and has been tested to generate working hashes. Co-authored-by: Teo Klestrup Röijezon <teo.roijezon@stackable.de>
1 parent dd7904a commit 3b92c6e

File tree

4 files changed

+182
-4
lines changed

4 files changed

+182
-4
lines changed

stackable-bcrypt/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ Most common use case will be in init containers to hash cleartext passwords from
66

77
The tool reads from stdin.
88

9+
## Building
10+
11+
Maven is used as the build tool, simply running `mvn package` will generate the required jars to run this.
12+
913
## Usage
1014

11-
stackable-bcrypt [OPTIONS]
15+
````
16+
➜ echo password | java -jar stackable-bcrypt-1.0-SNAPSHOT-jar-with-dependencies.jar
17+
$2b$12$esbe7T2nhgkk5hwu2jM4Kuo9RQ4Zwjdl5a6Ir9/gILUJ4swHbZYrK
18+
````
1219

13-
OPTIONS:
14-
-c, --cost <COST>
15-
[default: 10]

stackable-bcrypt/pom.xml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>tech.stackable.nifi</groupId>
8+
<artifactId>stackable-bcrypt</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>hashtester</name>
12+
<url>https://github.com/stackabletech/stackable-utils</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<source>11</source>
17+
<target>11</target>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>at.favre.lib</groupId>
23+
<artifactId>bcrypt</artifactId>
24+
<version>0.9.0</version>
25+
</dependency>
26+
</dependencies>
27+
28+
<build>
29+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
30+
<plugins>
31+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
32+
<plugin>
33+
<artifactId>maven-clean-plugin</artifactId>
34+
<version>3.1.0</version>
35+
</plugin>
36+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
37+
<plugin>
38+
<artifactId>maven-resources-plugin</artifactId>
39+
<version>3.0.2</version>
40+
</plugin>
41+
<plugin>
42+
<artifactId>maven-compiler-plugin</artifactId>
43+
<version>3.8.0</version>
44+
</plugin>
45+
<plugin>
46+
<artifactId>maven-surefire-plugin</artifactId>
47+
<version>2.22.1</version>
48+
</plugin>
49+
<plugin>
50+
<artifactId>maven-jar-plugin</artifactId>
51+
<version>3.0.2</version>
52+
</plugin>
53+
<plugin>
54+
<artifactId>maven-install-plugin</artifactId>
55+
<version>2.5.2</version>
56+
</plugin>
57+
<plugin>
58+
<artifactId>maven-deploy-plugin</artifactId>
59+
<version>2.8.2</version>
60+
</plugin>
61+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
62+
<plugin>
63+
<artifactId>maven-site-plugin</artifactId>
64+
<version>3.7.1</version>
65+
</plugin>
66+
<plugin>
67+
<artifactId>maven-project-info-reports-plugin</artifactId>
68+
<version>3.0.0</version>
69+
</plugin>
70+
</plugins>
71+
</pluginManagement>
72+
<plugins>
73+
<plugin>
74+
<groupId>org.apache.maven.plugins</groupId>
75+
<artifactId>maven-assembly-plugin</artifactId>
76+
<version>3.1.1</version>
77+
78+
<configuration>
79+
<descriptorRefs>
80+
<descriptorRef>jar-with-dependencies</descriptorRef>
81+
</descriptorRefs>
82+
<archive>
83+
<manifest>
84+
<mainClass>tech.stackable.nifi.PasswordHasher</mainClass>
85+
</manifest>
86+
</archive>
87+
</configuration>
88+
89+
<executions>
90+
<execution>
91+
<id>make-assembly</id>
92+
<phase>package</phase>
93+
<goals>
94+
<goal>single</goal>
95+
</goals>
96+
</execution>
97+
</executions>
98+
99+
</plugin>
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-compiler-plugin</artifactId>
103+
<configuration>
104+
<source>11</source>
105+
<target>11</target>
106+
</configuration>
107+
</plugin>
108+
</plugins>
109+
</build>
110+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tech.stackable.nifi;
2+
3+
import at.favre.lib.crypto.bcrypt.BCrypt;
4+
import at.favre.lib.crypto.bcrypt.BCrypt.Hasher;
5+
import at.favre.lib.crypto.bcrypt.BCrypt.Result;
6+
import at.favre.lib.crypto.bcrypt.BCrypt.Verifyer;
7+
import at.favre.lib.crypto.bcrypt.BCrypt.Version;
8+
9+
public class BCryptPasswordEncoder {
10+
private static final int DEFAULT_COST = 12;
11+
private static final Version BCRYPT_VERSION;
12+
private static final Hasher HASHER;
13+
private static final Verifyer VERIFYER;
14+
15+
public BCryptPasswordEncoder() {
16+
}
17+
18+
public String encode(char[] password) {
19+
return HASHER.hashToString(DEFAULT_COST, password);
20+
}
21+
22+
public boolean matches(char[] password, String encodedPassword) {
23+
Result result = VERIFYER.verifyStrict(password, encodedPassword.toCharArray());
24+
return result.verified;
25+
}
26+
27+
static {
28+
BCRYPT_VERSION = Version.VERSION_2B;
29+
HASHER = BCrypt.with(BCRYPT_VERSION);
30+
VERIFYER = BCrypt.verifyer(BCRYPT_VERSION);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tech.stackable.nifi;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.stream.Collectors;
7+
8+
public class PasswordHasher
9+
{
10+
public static void main( String[] args )
11+
{
12+
String input = getInput();
13+
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
14+
String hash = encoder.encode(input.toCharArray());
15+
System.out.println(hash);
16+
}
17+
18+
private static String getInput() {
19+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
20+
try {
21+
if (reader.ready()) {
22+
return reader.lines().collect(Collectors.joining("\n"));
23+
}
24+
} catch (IOException e) {
25+
System.err.println("Failed to read from stdin: " + e.getMessage());
26+
System.exit(-1);
27+
}
28+
// Didn't read anything or an error occurred
29+
System.exit(-1);
30+
return "";
31+
}
32+
}

0 commit comments

Comments
 (0)