add hashing passwords

This commit is contained in:
KP9lKk 2025-01-13 16:16:27 +03:00
parent c38a5eeb80
commit 3a05eebec3

View File

@ -1,12 +1,29 @@
import model.UserAuthorize import model.UserAuthorize
import java.security.KeyStore.SecretKeyEntry
import java.security.SecureRandom
import java.time.LocalDate import java.time.LocalDate
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec
import javax.crypto.spec.SecretKeySpec
val authorizeList = mutableListOf<UserAuthorize>() val authorizeList = mutableListOf<UserAuthorize>()
fun main(){ fun main(){
authorizeList.add(registration()) val secureRandom = SecureRandom()
authorizeList.forEach { val salt = byteArrayOf()
println(it) secureRandom.nextBytes(salt)
} }
fun hashedPassword(password: String, salt: ByteArray): String{
val pbeKeySpec = PBEKeySpec(
password.toCharArray(),
salt,
65536,
128)
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
return factory.generateSecret(pbeKeySpec).encoded.toString()
}
fun matches(password: String, passwordHashed: String, salt: ByteArray): Boolean{
return hashedPassword(password, salt).contentEquals(passwordHashed)
} }
fun registration(): UserAuthorize { fun registration(): UserAuthorize {
val userLogin = readlnOrNull() val userLogin = readlnOrNull()
@ -14,6 +31,11 @@ fun registration(): UserAuthorize {
require(userLogin.length >= 4){ require(userLogin.length >= 4){
"Никнейм должен быть больше 4 символов" "Никнейм должен быть больше 4 символов"
} }
userLogin.forEach {
require(it.isLetterOrDigit())
require("@" in userLogin)
require(userLogin.contains("@"))
}
return UserAuthorize( return UserAuthorize(
authorizeList.size + 1, authorizeList.size + 1,
password = "123", password = "123",