From 3a05eebec34627e2cbd48f692c435670ef7f229a Mon Sep 17 00:00:00 2001 From: KP9lKk Date: Mon, 13 Jan 2025 16:16:27 +0300 Subject: [PATCH] add hashing passwords --- app/src/main/kotlin/main.kt | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/main.kt b/app/src/main/kotlin/main.kt index 1bec2e2..e0bba3a 100644 --- a/app/src/main/kotlin/main.kt +++ b/app/src/main/kotlin/main.kt @@ -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() 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",