Add encrypt password

This commit is contained in:
Данил
2025-06-15 14:50:12 +07:00
parent 6567addfae
commit f525aaec3e
2 changed files with 56 additions and 19 deletions
@@ -47,24 +47,27 @@ public class DataBase {
if(!connection.isValid(100))
OpenConnection();
PreparedStatement saveUser_stmt = connection.prepareStatement("INSERT INTO users(login, password) VALUE (?, ?)");
PreparedStatement saveUser_stmt = connection.prepareStatement("INSERT INTO users_v2(login, password) VALUE (?, ?)");
saveUser_stmt.setString(1, login);
saveUser_stmt.setString(2, password);
saveUser_stmt.executeUpdate();
PreparedStatement findID = connection.prepareStatement("SELECT id FROM users WHERE login = ?");
PreparedStatement findID = connection.prepareStatement("SELECT id FROM users_v2 WHERE login = ?");
findID.setString(1, login);
ResultSet newID = findID.executeQuery();
newID.next();
return new User(newID.getInt(1), login, password);
User user = new User(newID.getInt(1), login, password);
newID.close();
return user;
}
public void AddTask(Task task, User user) throws SQLException {
if(!connection.isValid(100))
OpenConnection();
PreparedStatement addTask = connection.prepareStatement("INSERT INTO tasks(ID," +
PreparedStatement addTask = connection.prepareStatement("INSERT INTO tasks_v2(ID," +
" TaskName," +
" TaskDescription," +
" TaskDate," +
@@ -85,7 +88,7 @@ public class DataBase {
if(!connection.isValid(100))
OpenConnection();
PreparedStatement addTask = connection.prepareStatement("UPDATE tasks SET TaskName = ?," +
PreparedStatement addTask = connection.prepareStatement("UPDATE tasks_v2 SET TaskName = ?," +
" TaskDescription = ?," +
" TaskDate = ?," +
" TaskTime = ?" +
@@ -104,7 +107,7 @@ public class DataBase {
if(!connection.isValid(100))
OpenConnection();
PreparedStatement remove = connection.prepareStatement("DELETE FROM tasks WHERE ID = ? AND UserOwner = ?");
PreparedStatement remove = connection.prepareStatement("DELETE FROM tasks_v2 WHERE ID = ? AND UserOwner = ?");
remove.setLong(1, taskID);
remove.setLong(2, user.getID());
remove.executeUpdate();
@@ -114,7 +117,7 @@ public class DataBase {
if(!connection.isValid(100))
OpenConnection();
PreparedStatement update = connection.prepareStatement("UPDATE users SET password = ? WHERE id = ?");
PreparedStatement update = connection.prepareStatement("UPDATE users_v2 SET password = ? WHERE id = ?");
update.setString(1, password);
update.setInt(2, userID);
update.executeUpdate();
@@ -124,7 +127,7 @@ public class DataBase {
if(!connection.isValid(100))
OpenConnection();
PreparedStatement findTasks = connection.prepareStatement("SELECT * FROM tasks WHERE UserOwner = ?");
PreparedStatement findTasks = connection.prepareStatement("SELECT * FROM tasks_v2 WHERE UserOwner = ?");
findTasks.setInt(1, user.getID());
ResultSet result = findTasks.executeQuery();
@@ -138,6 +141,7 @@ public class DataBase {
result.getString(6)); // TaskTime
taskList.add(task);
}
result.close();
return taskList;
}
@@ -145,15 +149,18 @@ public class DataBase {
if(!connection.isValid(100))
OpenConnection();
PreparedStatement getUser_stmt = connection.prepareStatement("SELECT * FROM users WHERE login = ?");
PreparedStatement getUser_stmt = connection.prepareStatement("SELECT * FROM users_v2 WHERE login = ?");
getUser_stmt.setString(1, login);
ResultSet resultSet = getUser_stmt.executeQuery();
if(!resultSet.next())
if(!resultSet.next()) {
resultSet.close();
return null;
}
int id = resultSet.getInt(1);
String pass = resultSet.getNString(3);
resultSet.close();
return new User(id, login, pass);
}
}
@@ -9,6 +9,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@@ -37,6 +39,25 @@ public class TaskTrakerServerApplication {
System.exit(code);
}
private static String convertByteArrayToHexString(byte[] arrayBytes) {
StringBuilder stringBuffer = new StringBuilder();
for (byte arrayByte : arrayBytes) {
stringBuffer.append(Integer.toString((arrayByte & 0xff) + 0x100, 16)
.substring(1));
}
return stringBuffer.toString();
}
private static String getMD5Hash(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
return convertByteArrayToHexString(messageDigest);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
@GetMapping("/admin/shutdown")
void shutdown(@RequestParam(name = "password", defaultValue = "null") String password) {
if(!password.equals(API_KEYS.ADMIN_PASSWORD))
@@ -53,8 +74,9 @@ public class TaskTrakerServerApplication {
return "{\n\t\"status\":\"error\"\n}";
else {
try {
String passHash = getMD5Hash(pass);
User user = UserRepository.getRepository().getUserByLogin(login);
if (user == null || !user.getPassword().equals(pass))
if (user == null || !user.getPassword().equals(passHash))
return "{\n\t\"status\":\"error\"\n}";
else
return "{\n\t\"status\":\"ok\"\n}";
@@ -75,7 +97,8 @@ public class TaskTrakerServerApplication {
else if (login.equals("null") || pass.equals("null"))
return "{\n\t\"status\":\"error\",\n\t\"token\":\"null\"\n}";
try {
User user = UserRepository.getRepository().AddUser(login, pass);
String passHash = getMD5Hash(pass);
User user = UserRepository.getRepository().AddUser(login, passHash);
if(user == null)
return "{\n\t\"status\":\"error\"\n}";
@@ -94,11 +117,13 @@ public class TaskTrakerServerApplication {
return "{\n\t\"status\":\"error\"\n}";
try {
String passHash = getMD5Hash(oldPassword);
User user = UserRepository.getRepository().getUserByLogin(login);
if(user == null || !user.getPassword().equals(oldPassword))
if(user == null || !user.getPassword().equals(passHash))
return "{\n\t\"status\":\"error\"\n}";
user.setPassword(newPassword);
passHash = getMD5Hash(newPassword);
user.setPassword(passHash);
return "{\n\t\"status\":\"ok\"\n}";
} catch (SQLException e) {
System.out.println(e.getMessage());
@@ -112,9 +137,10 @@ public class TaskTrakerServerApplication {
@RequestParam(name = "id") long taskID) {
if (!login.equals("null") || !pass.equals("null")) {
try {
String passHash = getMD5Hash(pass);
User user = UserRepository.getRepository().getUserByLogin(login);
if (user == null || !user.getPassword().equals(pass))
if (user == null || !user.getPassword().equals(passHash))
return new Task(0, 0, null, null, null, null);
List<Task> taskList = DataBase.getInstance().getTaskList(user);
@@ -138,8 +164,9 @@ public class TaskTrakerServerApplication {
return new ArrayList<>();
else {
try {
String passHash = getMD5Hash(pass);
User user = UserRepository.getRepository().getUserByLogin(login);
if (user == null || !user.getPassword().equals(pass))
if (user == null || !user.getPassword().equals(passHash))
return new ArrayList<>();
else
return DataBase.getInstance().getTaskList(user);
@@ -156,8 +183,9 @@ public class TaskTrakerServerApplication {
@RequestBody Task task) {
if (!login.equals("null") && !pass.equals("null") && task != null) {
try {
String passHash = getMD5Hash(pass);
User user = UserRepository.getRepository().getUserByLogin(login);
if (user == null || !user.getPassword().equals(pass))
if (user == null || !user.getPassword().equals(passHash))
return;
DataBase.getInstance().AddTask(task, user);
} catch (SQLException e) {
@@ -172,8 +200,9 @@ public class TaskTrakerServerApplication {
@RequestBody Task task) {
if (!login.equals("null") && !pass.equals("null") && task != null) {
try {
String passHash = getMD5Hash(pass);
User user = UserRepository.getRepository().getUserByLogin(login);
if (user == null || !user.getPassword().equals(pass))
if (user == null || !user.getPassword().equals(passHash))
return;
DataBase.getInstance().UpdateTask(task, user);
} catch (SQLException e) {
@@ -188,8 +217,9 @@ public class TaskTrakerServerApplication {
@RequestParam(name = "id", defaultValue = "-1") long id) {
if (!login.equals("null") && !pass.equals("null") && id != -1) {
try {
String passHash = getMD5Hash(pass);
User user = UserRepository.getRepository().getUserByLogin(login);
if (user == null || !user.getPassword().equals(pass))
if (user == null || !user.getPassword().equals(passHash))
return;
DataBase.getInstance().RemoveTask(id, user);
} catch (SQLException e) {