Skip to content
This repository has been archived by the owner on Jan 12, 2025. It is now read-only.

Commit

Permalink
Move away from JNA argon2id hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
Quackster committed Sep 10, 2022
1 parent 7342126 commit f63e05d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 48 deletions.
12 changes: 7 additions & 5 deletions Kepler-Server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ java {
mainClassName = 'org.alexdev.kepler.Kepler'

repositories {
flatDir {
dirs 'libs'
}
maven { url 'https://jitpack.io' }
mavenCentral()
}
Expand Down Expand Up @@ -50,9 +47,14 @@ dependencies {
// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.0'

// https://mvnrepository.com/artifact/org.springframework.security/spring-security-crypto
implementation group: 'org.springframework.security', name: 'spring-security-crypto', version: '5.7.3'

// https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on
implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.70'

implementation 'com.github.bhlangonijr:chesslib:1.1.1'
implementation 'com.goterl:lazysodium-java:5.0.1'
implementation "net.java.dev.jna:jna:5.8.0"

}

// Create fat jar with libraries inside of it.
Expand Down
Binary file removed Kepler-Server/libs/bcrypt-0.9.0.jar
Binary file not shown.
Binary file removed Kepler-Server/libs/bcrypt-cli-0.9.0-full.jar
Binary file not shown.
24 changes: 10 additions & 14 deletions Kepler-Server/src/main/java/org/alexdev/kepler/Kepler.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.alexdev.kepler;

import com.goterl.lazysodium.LazySodiumJava;
import com.goterl.lazysodium.SodiumJava;
import io.netty.util.ResourceLeakDetector;
import org.alexdev.kepler.dao.Storage;
import org.alexdev.kepler.dao.mysql.SettingsDao;
Expand Down Expand Up @@ -36,9 +34,7 @@
import org.alexdev.kepler.util.config.writer.GameConfigWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.UnknownHostException;
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;

public class Kepler {

Expand All @@ -59,9 +55,6 @@ public class Kepler {
private static MusServer musServer;
private static RconServer rconServer;
private static Logger log;

private static LazySodiumJava LIB_SODIUM;

public static final String SERVER_VERSION = "v1.4";

/**
Expand Down Expand Up @@ -92,7 +85,7 @@ public static void main(String[] args) {
if (!Storage.connect()) {
return;
}

log.info("Setting up game");
//log.info(REGISTER.createPassword("lol"));

Expand Down Expand Up @@ -121,9 +114,6 @@ public static void main(String[] args) {
// Update players online back to 0
SettingsDao.updateSetting("players.online", "0");

log.info("Using Argon2 password hashing algorithm");
LIB_SODIUM = new LazySodiumJava(new SodiumJava());

setupMus();
setupRcon();
setupServer();
Expand Down Expand Up @@ -308,7 +298,13 @@ public static boolean isShuttingdown() {
return isShutdown;
}

public static LazySodiumJava getLibSodium() {
return LIB_SODIUM;
/**
* Get the Argon2 password encoder instance.
*
* @return
*/
public static Argon2PasswordEncoder getPasswordEncoder() {
var encoder =new Argon2PasswordEncoder(16, 32, 1, 65536, 2);
return encoder;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.alexdev.kepler.dao.mysql;

import com.goterl.lazysodium.interfaces.PwHash;
import org.alexdev.kepler.Kepler;
import org.alexdev.kepler.dao.Storage;
import org.alexdev.kepler.game.player.Player;
import org.alexdev.kepler.game.player.PlayerDetails;
import org.alexdev.kepler.game.player.PlayerManager;
import org.alexdev.kepler.util.DateUtil;

import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -199,17 +199,13 @@ public static boolean login(PlayerDetails player, String username, String passwo
resultSet = preparedStatement.executeQuery();

if (resultSet.next()) {
byte[] hashedPassword = (resultSet.getString("password") + '\0').getBytes(StandardCharsets.UTF_8);
byte[] pass = password.getBytes(StandardCharsets.UTF_8);
String databasePassword = resultSet.getString("password");

PwHash.Native pwHash = (PwHash.Native) Kepler.getLibSodium();
success = pwHash.cryptoPwHashStrVerify(hashedPassword, pass, pass.length);

if (success) {
fill(player, resultSet);
}
if (PlayerManager.getInstance().passwordMatches(databasePassword, password)) {
success = true;
fill(player, resultSet);
}
}

} catch (Exception e) {
Storage.logError(e);
} finally {
Expand Down Expand Up @@ -238,8 +234,6 @@ public static boolean login(String username, String password) {
byte[] hashedPassword = (resultSet.getString("password") + '\0').getBytes(StandardCharsets.UTF_8);
byte[] pass = password.getBytes(StandardCharsets.UTF_8);

PwHash.Native pwHash = (PwHash.Native) Kepler.getLibSodium();
success = pwHash.cryptoPwHashStrVerify(hashedPassword, pass, pass.length);
}

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.alexdev.kepler.game.player;

import com.goterl.lazysodium.interfaces.PwHash;
import org.alexdev.kepler.Kepler;
import org.alexdev.kepler.dao.mysql.PlayerDao;
import org.alexdev.kepler.game.GameScheduler;
Expand All @@ -15,6 +14,7 @@
import org.alexdev.kepler.messages.types.MessageComposer;
import org.alexdev.kepler.util.DateUtil;
import org.alexdev.kepler.util.config.ServerConfiguration;
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;

import java.time.Duration;
import java.time.LocalTime;
Expand Down Expand Up @@ -287,23 +287,14 @@ public Collection<Player> getActivePlayers() {
* @return hashed password
* @throws Exception
*/
public String createPassword(String password) throws Exception {
byte[] pw = password.getBytes();
byte[] outputHash = new byte[PwHash.STR_BYTES];
PwHash.Native pwHash = (PwHash.Native) Kepler.getLibSodium();
boolean success = pwHash.cryptoPwHashStr(
outputHash,
pw,
pw.length,
PwHash.OPSLIMIT_INTERACTIVE,
PwHash.MEMLIMIT_INTERACTIVE
);

if (!success) {
throw new Exception("Password creation was a failure!");
}
public String createPassword(String password) {
return Kepler.getPasswordEncoder().encode(password);
}

return new String(outputHash).replace((char) 0 + "", "");
public boolean passwordMatches(String databasePassword, String enteredPassword) {
System.out.println("raw = " + enteredPassword);
System.out.println("db = " + databasePassword);
return Kepler.getPasswordEncoder().matches(enteredPassword, databasePassword);
}

/**
Expand Down

2 comments on commit f63e05d

@emansom
Copy link

@emansom emansom commented on f63e05d Sep 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Native Java implementation? 🎉 👍🏻

@Quackster
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emansom

Yeah!

Please sign in to comment.