sample for any class
This commit is contained in:
parent
3a05eebec3
commit
dc44b6acc0
@ -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 {
|
||||||
|
26
app/src/main/kotlin/datasource.kt
Normal file
26
app/src/main/kotlin/datasource.kt
Normal 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
|
||||||
|
}
|
@ -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)
|
||||||
}
|
}
|
||||||
|
val randomUserIndex = Random.nextInt(0, users.size)
|
||||||
fun hashedPassword(password: String, salt: ByteArray): String{
|
val findUser = users[randomUserIndex]
|
||||||
val pbeKeySpec = PBEKeySpec(
|
println(findUser::class.java)
|
||||||
password.toCharArray(),
|
when(findUser){
|
||||||
salt,
|
is AdminUser -> {
|
||||||
65536,
|
println("adm")
|
||||||
128)
|
}
|
||||||
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
|
is ManagerUser -> {
|
||||||
return factory.generateSecret(pbeKeySpec).encoded.toString()
|
println("manager")
|
||||||
}
|
}
|
||||||
fun matches(password: String, passwordHashed: String, salt: ByteArray): Boolean{
|
is DefaultUser ->{
|
||||||
return hashedPassword(password, salt).contentEquals(passwordHashed)
|
println("default")
|
||||||
}
|
|
||||||
fun registration(): UserAuthorize {
|
|
||||||
val userLogin = readlnOrNull()
|
|
||||||
checkNotNull(userLogin)
|
|
||||||
require(userLogin.length >= 4){
|
|
||||||
"Никнейм должен быть больше 4 символов"
|
|
||||||
}
|
}
|
||||||
userLogin.forEach {
|
|
||||||
require(it.isLetterOrDigit())
|
|
||||||
require("@" in userLogin)
|
|
||||||
require(userLogin.contains("@"))
|
|
||||||
}
|
}
|
||||||
return UserAuthorize(
|
|
||||||
authorizeList.size + 1,
|
|
||||||
password = "123",
|
|
||||||
login = userLogin,
|
|
||||||
lastAuthorizeDate = LocalDate.now()
|
|
||||||
)
|
|
||||||
}
|
}
|
9
app/src/main/kotlin/model/AdminUser.kt
Normal file
9
app/src/main/kotlin/model/AdminUser.kt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class AdminUser(
|
||||||
|
userUUID: UUID,
|
||||||
|
userLogin: String,
|
||||||
|
userPassword: String,
|
||||||
|
):User(userUUID, userLogin, userPassword)
|
10
app/src/main/kotlin/model/DefaultUser.kt
Normal file
10
app/src/main/kotlin/model/DefaultUser.kt
Normal 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)
|
9
app/src/main/kotlin/model/ManagerUser.kt
Normal file
9
app/src/main/kotlin/model/ManagerUser.kt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class ManagerUser(
|
||||||
|
userUUID: UUID,
|
||||||
|
userLogin: String,
|
||||||
|
userPassword: String,
|
||||||
|
):User(userUUID, userLogin, userPassword)
|
8
app/src/main/kotlin/model/User.kt
Normal file
8
app/src/main/kotlin/model/User.kt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
open class User(
|
||||||
|
val userUUID: UUID,
|
||||||
|
var login: String,
|
||||||
|
var password: String, )
|
@ -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
|
|
||||||
)
|
|
3
app/src/main/kotlin/service/AdminServiceImpl.kt
Normal file
3
app/src/main/kotlin/service/AdminServiceImpl.kt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
class AdminServiceImpl
|
Loading…
Reference in New Issue
Block a user