Passwörter mit Hash wurge erzeugt

WetterService
danai 2024-06-15 17:08:03 +02:00
parent 829e875409
commit fc6228ca3e
2 changed files with 28 additions and 39 deletions

View File

@ -24,6 +24,16 @@
<version>5.8.1</version> <version>5.8.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
@ -84,6 +94,7 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId> <artifactId>maven-pmd-plugin</artifactId>
<version>3.22.0</version> <version>3.22.0</version>
<executions> <executions>
<execution> <execution>
<phase>verify</phase> <phase>verify</phase>

View File

@ -1,22 +1,16 @@
package domain; package domain;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Serializable; import org.apache.commons.codec.digest.DigestUtils;
import java.util.ArrayList;
import java.util.List;
public class User implements Serializable{ public class User {
private String username; private String username;
private String password; private String hashedPassword;
private Ort heimatstandort; private Ort heimatstandort;
private Auto auto; private Auto auto;
private double durchschnittsgeschwindigkeitPKW; private double durchschnittsgeschwindigkeitPKW;
@ -24,14 +18,19 @@ public class User implements Serializable{
public User(String username, String password, Ort heimatstandort, Auto auto, double durchschnittsgeschwindigkeitPKW, public User(String username, String password, Ort heimatstandort, Auto auto, double durchschnittsgeschwindigkeitPKW,
double durchschnittsgeschwindigkeitFahrrad) { double durchschnittsgeschwindigkeitFahrrad) {
this.username = username; this.username = username;
this.password = password; this.hashedPassword = hashPassword(password);
this.heimatstandort = heimatstandort; this.heimatstandort = heimatstandort;
this.auto = auto; this.auto = auto;
this.durchschnittsgeschwindigkeitPKW = durchschnittsgeschwindigkeitPKW; this.durchschnittsgeschwindigkeitPKW = durchschnittsgeschwindigkeitPKW;
this.durchschnittsgeschwindigkeitFahrrad = durchschnittsgeschwindigkeitFahrrad; this.durchschnittsgeschwindigkeitFahrrad = durchschnittsgeschwindigkeitFahrrad;
} }
private String hashPassword(String password) {
return DigestUtils.sha256Hex(password); // Hash-Funktion verwenden
}
public String getUsername() { public String getUsername() {
return username; return username;
} }
@ -40,12 +39,12 @@ public class User implements Serializable{
this.username = username; this.username = username;
} }
public String getPassword() { public String getHashedPassword() {
return password; return hashedPassword;
} }
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.hashedPassword = hashPassword(password); // Passwort hashen beim Setzen
} }
public Ort getHeimatstandort() { public Ort getHeimatstandort() {
@ -83,9 +82,9 @@ public class User implements Serializable{
public void registrieren() { public void registrieren() {
try (PrintWriter writer = new PrintWriter(new FileWriter("users.txt", true))) { try (PrintWriter writer = new PrintWriter(new FileWriter("users.txt", true))) {
writer.println(username + "," + password + "," + heimatstandort.getPLZ() + "," + heimatstandort.getOrtName() writer.println(username + "," + hashedPassword + "," + heimatstandort.getPLZ() + ","
+ "," + auto.getName() + "," + auto.getCO2AusstossProKm() + "," + durchschnittsgeschwindigkeitPKW + heimatstandort.getOrtName() + "," + auto.getName() + "," + auto.getCO2AusstossProKm() + ","
+ "," + durchschnittsgeschwindigkeitFahrrad); + durchschnittsgeschwindigkeitPKW + "," + durchschnittsgeschwindigkeitFahrrad);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -98,8 +97,7 @@ public class User implements Serializable{
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
String[] parts = line.split(","); String[] parts = line.split(",");
if (parts[0].equals(username) && parts[1].equals(password)) { if (parts[0].equals(username) && parts[1].equals(hashedPassword)) {
// Benutzerdaten laden
this.heimatstandort = new Ort(parts[2], parts[3]); this.heimatstandort = new Ort(parts[2], parts[3]);
this.auto = new Auto(parts[4], Double.parseDouble(parts[5])); this.auto = new Auto(parts[4], Double.parseDouble(parts[5]));
this.durchschnittsgeschwindigkeitPKW = Double.parseDouble(parts[6]); this.durchschnittsgeschwindigkeitPKW = Double.parseDouble(parts[6]);
@ -113,7 +111,6 @@ public class User implements Serializable{
return false; return false;
} }
public String zeigeWetterHeimatstandort() { public String zeigeWetterHeimatstandort() {
try { try {
return WetterService.getAktuellesWetter(heimatstandort.getOrtName()); return WetterService.getAktuellesWetter(heimatstandort.getOrtName());
@ -122,7 +119,6 @@ public class User implements Serializable{
} }
} }
public String zeigeWettervorhersageHeimatstandort() { public String zeigeWettervorhersageHeimatstandort() {
try { try {
return WetterService.getWettervorhersage(heimatstandort.getOrtName()); return WetterService.getWettervorhersage(heimatstandort.getOrtName());
@ -131,22 +127,4 @@ public class User implements Serializable{
} }
} }
private void saveUsers(List<User> users) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("users.ser"))) {
oos.writeObject(users);
} catch (IOException e) {
e.printStackTrace();
}
}
private List<User> loadUsers() {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("users.ser"))) {
return (List<User>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
return new ArrayList<>();
}
}
} }