TD-3: Create Player Service with Password Methods
All checks were successful
Quality Check / Validate OAS (push) Successful in 36s
Quality Check / Linting (push) Successful in 1m17s
Quality Check / Validate OAS (pull_request) Successful in 38s
Quality Check / Testing (push) Successful in 1m3s
Quality Check / Linting (pull_request) Successful in 1m19s
Quality Check / Static Analysis (push) Successful in 2m0s
Quality Check / Testing (pull_request) Successful in 52s
Quality Check / Static Analysis (pull_request) Successful in 1m7s
All checks were successful
Quality Check / Validate OAS (push) Successful in 36s
Quality Check / Linting (push) Successful in 1m17s
Quality Check / Validate OAS (pull_request) Successful in 38s
Quality Check / Testing (push) Successful in 1m3s
Quality Check / Linting (pull_request) Successful in 1m19s
Quality Check / Static Analysis (push) Successful in 2m0s
Quality Check / Testing (pull_request) Successful in 52s
Quality Check / Static Analysis (pull_request) Successful in 1m7s
This commit is contained in:
parent
671f278f54
commit
3877b8f466
2 changed files with 56 additions and 2 deletions
|
@ -15,6 +15,8 @@ import jakarta.persistence.*;
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "player")
|
@Table(name = "player")
|
||||||
public class Player {
|
public class Player {
|
||||||
|
public static final int PASSWORD_SALT_BYTE_LENGTH = 16;
|
||||||
|
public static final int PASSWORD_HASH_BYTE_LENGTH = 64;
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
private Long id;
|
private Long id;
|
||||||
|
@ -24,10 +26,10 @@ public class Player {
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Size(min = 64, max = 64)
|
@Size(min = PASSWORD_HASH_BYTE_LENGTH, max = PASSWORD_HASH_BYTE_LENGTH)
|
||||||
private byte[] passwordHash;
|
private byte[] passwordHash;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Size(min = 16, max = 16)
|
@Size(min = PASSWORD_SALT_BYTE_LENGTH, max = PASSWORD_SALT_BYTE_LENGTH)
|
||||||
private byte[] passwordSalt;
|
private byte[] passwordSalt;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
package de.towerdefence.server.player;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class PlayerService {
|
||||||
|
@Autowired
|
||||||
|
private PlayerRepository playerRepository;
|
||||||
|
|
||||||
|
private final SecureRandom random;
|
||||||
|
|
||||||
|
public PlayerService(PlayerRepository playerRepository) {
|
||||||
|
this.playerRepository = playerRepository;
|
||||||
|
this.random = new SecureRandom();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkPassword(Player player, String password) throws NoSuchAlgorithmException {
|
||||||
|
return Arrays.equals(
|
||||||
|
hashPassword(
|
||||||
|
player.getPasswordSalt(),
|
||||||
|
password.getBytes(StandardCharsets.UTF_8)
|
||||||
|
),
|
||||||
|
player.getPasswordHash()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(Player player, String password) throws NoSuchAlgorithmException {
|
||||||
|
byte[] salt = new byte[Player.PASSWORD_SALT_BYTE_LENGTH];
|
||||||
|
this.random.nextBytes(salt);
|
||||||
|
|
||||||
|
byte[] passwordHash = hashPassword(
|
||||||
|
salt,
|
||||||
|
password.getBytes(StandardCharsets.UTF_8)
|
||||||
|
);
|
||||||
|
|
||||||
|
player.setPasswordSalt(salt);
|
||||||
|
player.setPasswordHash(passwordHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] hashPassword(byte[] salt, byte[] password) throws NoSuchAlgorithmException {
|
||||||
|
MessageDigest md = MessageDigest.getInstance("SHA-512");
|
||||||
|
md.update(salt);
|
||||||
|
return md.digest(password);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue