TD-3: Create Player Service with Password Methods
All checks were successful
Quality Check / Validate OAS (push) Successful in 31s
Quality Check / Testing (push) Successful in 48s
Quality Check / Static Analysis (push) Successful in 59s
Quality Check / Linting (push) Successful in 1m10s
Quality Check / Validate OAS (pull_request) Successful in 29s
Quality Check / Testing (pull_request) Successful in 46s
Quality Check / Linting (pull_request) Successful in 52s
Quality Check / Static Analysis (pull_request) Successful in 55s
All checks were successful
Quality Check / Validate OAS (push) Successful in 31s
Quality Check / Testing (push) Successful in 48s
Quality Check / Static Analysis (push) Successful in 59s
Quality Check / Linting (push) Successful in 1m10s
Quality Check / Validate OAS (pull_request) Successful in 29s
Quality Check / Testing (pull_request) Successful in 46s
Quality Check / Linting (pull_request) Successful in 52s
Quality Check / Static Analysis (pull_request) Successful in 55s
This commit is contained in:
parent
671f278f54
commit
0b69697c56
1 changed files with 52 additions and 0 deletions
|
@ -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[16];
|
||||
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