This commit is contained in:
adm 2025-01-14 14:27:05 +03:00
parent df6cabbca0
commit 5adc4b47be
4 changed files with 82 additions and 27 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -1,39 +1,80 @@
import model.UserAuthorize import model.UserAuthorize
import model.Roles import model.Roles
import java.awt.Checkbox import java.security.SecureRandom
import java.time.LocalDate import java.time.LocalDate
import kotlin.system.exitProcess import java.time.LocalDateTime
import javax.crypto.SecretKey
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec
import javax.swing.text.PasswordView
val authorizeList = mutableListOf<UserAuthorize>() val authorizeList = mutableListOf<UserAuthorize>()
fun main() { fun main() {
val userLogin = readlnOrNull()
registration()
authorizeList.add(registration()) authorizeList.add(registration())
authorizeList.forEach{ authorization()
}
fun authorization(){
println() println()
println()
println()
println("Введите почту или номер телефона для авторизации")
val readUserEmailOrNumber = readln()
println("Введите пароль")
val readUserPassword = readln()
authorizeList.forEach{
if ((readUserEmailOrNumber==it.email || readUserEmailOrNumber==it.phone) && hashing(readUserPassword)==it.password){
it.lastAuthorizeDate= LocalDateTime.now()
return println(it.role.roleName +"\r\n"+it.lastAuthorizeDate)
} }
} }
}
fun hashing(password : String): SecretKey? {
val salt = byteArrayOf(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f)
val iterations = 10000
val keySize = 256
val keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
val keySpec = PBEKeySpec(password.toCharArray(), salt, iterations, keySize)
val secretKey = keyFactory.generateSecret(keySpec)
return secretKey
}
fun registration() : UserAuthorize { fun registration() : UserAuthorize {
var userLogin = ""
while (userLogin?.length!! <4){
print("Введите никнейм: ") print("Введите никнейм: ")
val userLogin = readlnOrNull() userLogin = readln()
checkNotNull(userLogin) if (userLogin.length<4){
require(userLogin.length >= 4) { println("Никнейм должен быть больше 4 символов. Попробуйте снова")
"Никнейм больше 4 символов" }
} }
var userPassword = ""
var dig = 0
var let = 0
while (userPassword.length<8 && dig<1 && let<1){
println("Пароль должен быть не менее 8 символов и должен содеражть буквы и цифры")
print("Введите пароль: ") print("Введите пароль: ")
val userPassword = readlnOrNull() dig=0
checkNotNull(userPassword) let=0
userPassword = readln()
userPassword.forEach {
if(it.isDigit()){
dig++
}
else if (it.isLetter()){
let++
}
}
}
userPassword.forEach { userPassword.forEach {
require(it.isLetterOrDigit()) require(it.isLetterOrDigit())
require(userPassword.length >= 8) require(userPassword.length >= 8)
} }
println()
print("Повторите пароль: ") print("Повторите пароль: ")
var secondUserPassword = "" var secondUserPassword = ""
while (secondUserPassword != userPassword) { while (secondUserPassword != userPassword) {
@ -43,27 +84,31 @@ fun registration() : UserAuthorize {
println("Пароли не совпадают") println("Пароли не совпадают")
} }
} }
var hashUserPassword=hashing(userPassword)
println()
print("Введите почту: ") print("Введите почту: ")
val userEmail = readlnOrNull() val userEmail = readlnOrNull()
checkNotNull(userEmail) checkNotNull(userEmail)
require("@" in userEmail) require("@" in userEmail)
println()
print("Введите номер телефона: ") print("Введите номер телефона: ")
var userPhone = readlnOrNull() var userPhone = readlnOrNull()
checkNotNull(userPhone) checkNotNull(userPhone)
require(userPhone.startsWith("89") && userPhone.length == 11) require((userPhone.startsWith("89") && userPhone.length == 11) || (userPhone.startsWith("+7") && userPhone.length == 12))
print("Вы админ? (Y, N): ")
var role = Roles(1, "администратор")
var adm = readln()
if (adm!="Y"){
role = Roles(2, "пользователь")
}
return UserAuthorize( return UserAuthorize(
authorizeList.size + 1, authorizeList.size + 1,
password = userPassword, password = hashUserPassword,
login = userLogin, login = userLogin,
lastAuthorizeDate = LocalDate.now(), lastAuthorizeDate = LocalDateTime.now(),
email = userEmail, email = userEmail,
phone = userPhone phone = userPhone,
role = role
) )
} }

View File

@ -1,12 +1,15 @@
package model package model
import java.time.LocalDate import java.time.LocalDate
import java.time.LocalDateTime
import javax.crypto.SecretKey
data class UserAuthorize( data class UserAuthorize(
val userId : Int, val userId : Int,
val login : String, val login : String,
var password : String, var password : SecretKey?,
var email : String, var email : String,
var phone : String, var phone : String,
var lastAuthorizeDate : LocalDate var role : Roles,
var lastAuthorizeDate : LocalDateTime
) )