add hashing passwords
This commit is contained in:
parent
c38a5eeb80
commit
3a05eebec3
@ -1,12 +1,29 @@
|
||||
import model.UserAuthorize
|
||||
import java.security.KeyStore.SecretKeyEntry
|
||||
import java.security.SecureRandom
|
||||
import java.time.LocalDate
|
||||
import javax.crypto.SecretKeyFactory
|
||||
import javax.crypto.spec.PBEKeySpec
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
|
||||
val authorizeList = mutableListOf<UserAuthorize>()
|
||||
fun main(){
|
||||
authorizeList.add(registration())
|
||||
authorizeList.forEach {
|
||||
println(it)
|
||||
}
|
||||
val secureRandom = SecureRandom()
|
||||
val salt = byteArrayOf()
|
||||
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 {
|
||||
val userLogin = readlnOrNull()
|
||||
@ -14,6 +31,11 @@ fun registration(): UserAuthorize {
|
||||
require(userLogin.length >= 4){
|
||||
"Никнейм должен быть больше 4 символов"
|
||||
}
|
||||
userLogin.forEach {
|
||||
require(it.isLetterOrDigit())
|
||||
require("@" in userLogin)
|
||||
require(userLogin.contains("@"))
|
||||
}
|
||||
return UserAuthorize(
|
||||
authorizeList.size + 1,
|
||||
password = "123",
|
||||
|
Loading…
Reference in New Issue
Block a user