mirror of
https://github.com/2martens/uni.git
synced 2026-05-06 19:36:26 +02:00
[Projekt] Klasse fertiggestellt
Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.math.BigInteger;
|
||||||
import java.nio.file.*;
|
import java.nio.file.*;
|
||||||
import java.nio.charset.*;
|
import java.nio.charset.*;
|
||||||
|
import java.security.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
class Useradmin {
|
class Useradmin {
|
||||||
@ -29,9 +31,9 @@ class Useradmin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addUser(String username, char[] password) {
|
public void addUser(String username, char[] password) {
|
||||||
char[] hashedPW = password;
|
String salt = genSalt();
|
||||||
String hashedPWString = String.valueOf(hashedPW);
|
String hashedPW = generateHash(String.valueOf(password), salt);
|
||||||
String format = username + ":" + hashedPWString;
|
String format = username + ":" + hashedPW;
|
||||||
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("passwords.txt", true)))) {
|
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("passwords.txt", true)))) {
|
||||||
out.println(format);
|
out.println(format);
|
||||||
System.out.println("User has been created");
|
System.out.println("User has been created");
|
||||||
@ -41,8 +43,6 @@ class Useradmin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean checkUser(String username, char[] password) {
|
public boolean checkUser(String username, char[] password) {
|
||||||
char[] hashedPW = password;
|
|
||||||
String hashedPWString = String.valueOf(hashedPW);
|
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
try {
|
try {
|
||||||
List<String> passwords = Files.readAllLines(Paths.get("passwords.txt"), StandardCharsets.UTF_8);
|
List<String> passwords = Files.readAllLines(Paths.get("passwords.txt"), StandardCharsets.UTF_8);
|
||||||
@ -50,8 +50,12 @@ class Useradmin {
|
|||||||
for (String uidPW : passwords) {
|
for (String uidPW : passwords) {
|
||||||
int indexOfColon = uidPW.indexOf(':');
|
int indexOfColon = uidPW.indexOf(':');
|
||||||
String user = uidPW.substring(0, indexOfColon);
|
String user = uidPW.substring(0, indexOfColon);
|
||||||
String pw = uidPW.substring(indexOfColon + 1);
|
String pwSalt = uidPW.substring(indexOfColon + 1);
|
||||||
if (user.equals(username) && pw.equals(hashedPWString)) {
|
indexOfColon = pwSalt.indexOf(':');
|
||||||
|
String salt = pwSalt.substring(0, indexOfColon);
|
||||||
|
String hashedPWString = generateHash(String.valueOf(password), salt);
|
||||||
|
|
||||||
|
if (user.equals(username) && pwSalt.equals(hashedPWString)) {
|
||||||
result = true;
|
result = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,4 +65,29 @@ class Useradmin {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private String generateHash(String str, String salt) {
|
||||||
|
String hashValue = salt + str;
|
||||||
|
for (int i = 0; i < 4200; i++) {
|
||||||
|
hashValue = hash(hashValue);
|
||||||
|
}
|
||||||
|
return salt + ":" + hashValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String hash(String str) {
|
||||||
|
try {
|
||||||
|
byte[] plain = str.getBytes(StandardCharsets.UTF_8);
|
||||||
|
MessageDigest md = MessageDigest.getInstance("SHA-512");
|
||||||
|
md.update(plain);
|
||||||
|
return new BigInteger(1, md.digest()).toString(16);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new UnsupportedOperationException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private String genSalt() {
|
||||||
|
SecureRandom random = new SecureRandom();
|
||||||
|
return new BigInteger(32, random).toString(32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user