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>
<scope>test</scope>
</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>
<build>
<plugins>
@ -84,6 +94,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.22.0</version>
<executions>
<execution>
<phase>verify</phase>

View File

@ -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());
@ -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<>();
}
}
}