From 0a4721e91e020cbbe9d534c410133f9d51b29457 Mon Sep 17 00:00:00 2001 From: Robin Strand Prestmo Date: Tue, 3 Mar 2026 16:49:27 +0100 Subject: [PATCH] Added getHashPassword to PasswordHasher --- .../helpmehelpapplication.iml | 6 +++ .../team6/models/PasswordHasher.java | 52 +++++++++++++++++++ .../sytemutvikling/team6/models/User.java | 6 +-- 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 helpmehelpapplication/helpmehelpapplication.iml create mode 100644 helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/PasswordHasher.java diff --git a/helpmehelpapplication/helpmehelpapplication.iml b/helpmehelpapplication/helpmehelpapplication.iml new file mode 100644 index 0000000..9e3449c --- /dev/null +++ b/helpmehelpapplication/helpmehelpapplication.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/PasswordHasher.java b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/PasswordHasher.java new file mode 100644 index 0000000..8c2c7ce --- /dev/null +++ b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/PasswordHasher.java @@ -0,0 +1,52 @@ +package ntnu.sytemutvikling.team6.models; + +import javax.crypto.SecretKeyFactory; +import javax.crypto.interfaces.PBEKey; +import javax.crypto.spec.PBEKeySpec; +import java.security.SecureRandom; +import java.util.Base64; + +/** + * A password hasher + * + *

+ * Description + *

+ * + * @author Robin Strand Prestmo + */ +public final class PasswordHasher { + private static final SecureRandom RNG = new SecureRandom(); + + /** + * Get the hash of the password + * + * @param password a string password + * @return a hash secured password + */ + public String getHashPassword(String password) { + String hasPass = ""; + + try { + // 1. Create salt + byte[] salt = new byte[16]; + RNG.nextBytes(salt); + + // 2. Create PBKDF2 Hash value + PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 100000, 32*8); + SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); + byte[] hash = factory.generateSecret(spec).getEncoded(); + + // 3. Combine salt and password bytes + byte[] hashBytes = new byte[48]; + System.arraycopy(salt, 0, hashBytes, 0, 16); + System.arraycopy(hash, 0, hashBytes, 16, 32); + + //4. Turn the combined salt+hash into a string. + hasPass = Base64.getEncoder().encodeToString(hashBytes); + } catch (Exception e) { + throw new RuntimeException("Error while hasing password.", e); + } + return hasPass; + } +} diff --git a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/User.java b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/User.java index 283bfa1..afa414a 100644 --- a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/User.java +++ b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/User.java @@ -2,9 +2,9 @@ import java.util.UUID; -// Passord må hashes!!! -// Unntakshåndtering mangler -// Enhetstesting mangler +// TODO: Passord må hashes!!! +// TODO: Unntakshåndtering mangler +// TODO: Enhetstesting mangler /** * Represents a user.