From fc6228ca3edf7faf8d288df4869ebf1f8e729cab Mon Sep 17 00:00:00 2001 From: danai Date: Sat, 15 Jun 2024 17:08:03 +0200 Subject: [PATCH] =?UTF-8?q?Passw=C3=B6rter=20mit=20Hash=20wurge=20erzeugt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TravelBuddyApp/pom.xml | 13 ++++- TravelBuddyApp/src/main/java/domain/User.java | 54 ++++++------------- 2 files changed, 28 insertions(+), 39 deletions(-) diff --git a/TravelBuddyApp/pom.xml b/TravelBuddyApp/pom.xml index 07fc4aa..ea06a2e 100644 --- a/TravelBuddyApp/pom.xml +++ b/TravelBuddyApp/pom.xml @@ -5,7 +5,7 @@ hs-mannheim.de TravelBuddyApp 0.0.1-SNAPSHOT - + UTF-8 21 @@ -24,6 +24,16 @@ 5.8.1 test + + org.json + json + 20210307 + + + commons-codec + commons-codec + 1.15 + @@ -84,6 +94,7 @@ org.apache.maven.plugins maven-pmd-plugin 3.22.0 + verify diff --git a/TravelBuddyApp/src/main/java/domain/User.java b/TravelBuddyApp/src/main/java/domain/User.java index 6e60c4e..fc3daef 100644 --- a/TravelBuddyApp/src/main/java/domain/User.java +++ b/TravelBuddyApp/src/main/java/domain/User.java @@ -1,22 +1,16 @@ package domain; import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; import java.io.PrintWriter; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; +import org.apache.commons.codec.digest.DigestUtils; -public class User implements Serializable{ +public class User { private String username; - private String password; + private String hashedPassword; private Ort heimatstandort; private Auto auto; private double durchschnittsgeschwindigkeitPKW; @@ -24,14 +18,19 @@ public class User implements Serializable{ public User(String username, String password, Ort heimatstandort, Auto auto, double durchschnittsgeschwindigkeitPKW, double durchschnittsgeschwindigkeitFahrrad) { + this.username = username; - this.password = password; + this.hashedPassword = hashPassword(password); this.heimatstandort = heimatstandort; this.auto = auto; this.durchschnittsgeschwindigkeitPKW = durchschnittsgeschwindigkeitPKW; this.durchschnittsgeschwindigkeitFahrrad = durchschnittsgeschwindigkeitFahrrad; } + private String hashPassword(String password) { + return DigestUtils.sha256Hex(password); // Hash-Funktion verwenden + } + public String getUsername() { return username; } @@ -40,12 +39,12 @@ public class User implements Serializable{ this.username = username; } - public String getPassword() { - return password; + public String getHashedPassword() { + return hashedPassword; } public void setPassword(String password) { - this.password = password; + this.hashedPassword = hashPassword(password); // Passwort hashen beim Setzen } public Ort getHeimatstandort() { @@ -83,9 +82,9 @@ public class User implements Serializable{ public void registrieren() { try (PrintWriter writer = new PrintWriter(new FileWriter("users.txt", true))) { - writer.println(username + "," + password + "," + heimatstandort.getPLZ() + "," + heimatstandort.getOrtName() - + "," + auto.getName() + "," + auto.getCO2AusstossProKm() + "," + durchschnittsgeschwindigkeitPKW - + "," + durchschnittsgeschwindigkeitFahrrad); + writer.println(username + "," + hashedPassword + "," + heimatstandort.getPLZ() + "," + + heimatstandort.getOrtName() + "," + auto.getName() + "," + auto.getCO2AusstossProKm() + "," + + durchschnittsgeschwindigkeitPKW + "," + durchschnittsgeschwindigkeitFahrrad); } catch (IOException e) { e.printStackTrace(); } @@ -98,8 +97,7 @@ public class User implements Serializable{ String line; while ((line = reader.readLine()) != null) { String[] parts = line.split(","); - if (parts[0].equals(username) && parts[1].equals(password)) { - // Benutzerdaten laden + if (parts[0].equals(username) && parts[1].equals(hashedPassword)) { this.heimatstandort = new Ort(parts[2], parts[3]); this.auto = new Auto(parts[4], Double.parseDouble(parts[5])); this.durchschnittsgeschwindigkeitPKW = Double.parseDouble(parts[6]); @@ -113,7 +111,6 @@ public class User implements Serializable{ return false; } - public String zeigeWetterHeimatstandort() { try { return WetterService.getAktuellesWetter(heimatstandort.getOrtName()); @@ -122,7 +119,6 @@ public class User implements Serializable{ } } - public String zeigeWettervorhersageHeimatstandort() { try { return WetterService.getWettervorhersage(heimatstandort.getOrtName()); @@ -130,23 +126,5 @@ public class User implements Serializable{ return "Fehler beim Abrufen der Wettervorhersage: " + e.getMessage(); } } - - - private void saveUsers(List users) { - try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("users.ser"))) { - oos.writeObject(users); - } catch (IOException e) { - e.printStackTrace(); - } - } - - - private List loadUsers() { - try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("users.ser"))) { - return (List) ois.readObject(); - } catch (IOException | ClassNotFoundException e) { - return new ArrayList<>(); - } - } }