sample for any class

This commit is contained in:
KP9lKk 2025-01-14 15:42:47 +03:00
parent 3a05eebec3
commit dc44b6acc0
9 changed files with 87 additions and 50 deletions

View File

@ -10,6 +10,8 @@ plugins {
dependencies { dependencies {
// Project "app" depends on project "utils". (Project paths are separated with ":", so ":utils" refers to the top-level "utils" project.) // Project "app" depends on project "utils". (Project paths are separated with ":", so ":utils" refers to the top-level "utils" project.)
implementation(project(":utils")) implementation(project(":utils"))
// https://mvnrepository.com/artifact/io.github.serpro69/kotlin-faker
implementation("io.github.serpro69:kotlin-faker:2.0.0-rc.7")
} }
application { application {

View File

@ -0,0 +1,26 @@
import io.github.serpro69.kfaker.Faker
import model.*
import java.util.*
val faker = Faker()
fun generateUsers(count: Int):List<User> {
val userList = mutableListOf<User>()
repeat(count){
userList.add(AdminUser(
UUID.randomUUID(),
faker.name.neutralFirstName(),
faker.string.numerify("555")
))
userList.add(ManagerUser(
UUID.randomUUID(),
faker.phoneNumber.phoneNumber(),
faker.string.numerify("1010")
))
userList.add(DefaultUser(
UUID.randomUUID(),
faker.name.neutralFirstName(),
faker.string.numerify("555")
))
}
return userList
}

View File

@ -1,45 +1,25 @@
import model.UserAuthorize import model.AdminUser
import java.security.KeyStore.SecretKeyEntry import model.DefaultUser
import java.security.SecureRandom import model.ManagerUser
import java.time.LocalDate import kotlin.random.Random
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec
import javax.crypto.spec.SecretKeySpec
val authorizeList = mutableListOf<UserAuthorize>()
fun main(){ fun main(){
val secureRandom = SecureRandom() val users = generateUsers(10)
val salt = byteArrayOf() users.forEach {
secureRandom.nextBytes(salt) println(it.password)
}
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()
checkNotNull(userLogin)
require(userLogin.length >= 4){
"Никнейм должен быть больше 4 символов"
} }
userLogin.forEach { val randomUserIndex = Random.nextInt(0, users.size)
require(it.isLetterOrDigit()) val findUser = users[randomUserIndex]
require("@" in userLogin) println(findUser::class.java)
require(userLogin.contains("@")) when(findUser){
is AdminUser -> {
println("adm")
}
is ManagerUser -> {
println("manager")
}
is DefaultUser ->{
println("default")
}
} }
return UserAuthorize(
authorizeList.size + 1,
password = "123",
login = userLogin,
lastAuthorizeDate = LocalDate.now()
)
} }

View File

@ -0,0 +1,9 @@
package model
import java.util.UUID
class AdminUser(
userUUID: UUID,
userLogin: String,
userPassword: String,
):User(userUUID, userLogin, userPassword)

View File

@ -0,0 +1,10 @@
package model
import io.github.serpro69.kfaker.Faker
import java.util.UUID
class DefaultUser(
userUUID: UUID,
userLogin: String,
userPassword: String,
):User(userUUID, userLogin, userPassword)

View File

@ -0,0 +1,9 @@
package model
import java.util.UUID
class ManagerUser(
userUUID: UUID,
userLogin: String,
userPassword: String,
):User(userUUID, userLogin, userPassword)

View File

@ -0,0 +1,8 @@
package model
import java.util.UUID
open class User(
val userUUID: UUID,
var login: String,
var password: String, )

View File

@ -1,10 +0,0 @@
package model
import java.time.LocalDate
data class UserAuthorize(
val userId: Int,
val login: String,
var password: String,
var lastAuthorizeDate: LocalDate
)

View File

@ -0,0 +1,3 @@
package service
class AdminServiceImpl