new_select_screen #9
58
app/src/main/java/ru/myitschool/work/data/AESEncryption.kt
Normal file
58
app/src/main/java/ru/myitschool/work/data/AESEncryption.kt
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package ru.myitschool.work.data
|
||||||
|
|
||||||
|
import android.util.Base64
|
||||||
|
import javax.crypto.Cipher
|
||||||
|
import javax.crypto.SecretKeyFactory
|
||||||
|
import javax.crypto.spec.IvParameterSpec
|
||||||
|
import javax.crypto.spec.PBEKeySpec
|
||||||
|
import javax.crypto.spec.SecretKeySpec
|
||||||
|
|
||||||
|
|
||||||
|
object AESEncyption {
|
||||||
|
|
||||||
|
const val secretKey = "tK5Ugskdkipokuodvknfdk3434weofnf="
|
||||||
|
const val salt = "QLlGNHNhYTJTQWZ2bGhpV3U="
|
||||||
|
const val iv = "bVQqNFNhRkQ1Njc4UUFaPA=="
|
||||||
|
|
||||||
|
fun encrypt(strToEncrypt: String): String? {
|
||||||
|
try {
|
||||||
|
val ivParameterSpec = IvParameterSpec(Base64.decode(iv, Base64.DEFAULT))
|
||||||
|
|
||||||
|
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
|
||||||
|
val spec =
|
||||||
|
PBEKeySpec(secretKey.toCharArray(), Base64.decode(salt, Base64.DEFAULT), 10000, 256)
|
||||||
|
val tmp = factory.generateSecret(spec)
|
||||||
|
val secretKey = SecretKeySpec(tmp.encoded, "AES")
|
||||||
|
|
||||||
|
val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec)
|
||||||
|
return Base64.encodeToString(
|
||||||
|
cipher.doFinal(strToEncrypt.toByteArray(Charsets.UTF_8)),
|
||||||
|
Base64.DEFAULT
|
||||||
|
)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Error while encrypting: $e")
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun decrypt(strToDecrypt: String?): String? {
|
||||||
|
try {
|
||||||
|
|
||||||
|
val ivParameterSpec = IvParameterSpec(Base64.decode(iv, Base64.DEFAULT))
|
||||||
|
|
||||||
|
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
|
||||||
|
val spec =
|
||||||
|
PBEKeySpec(secretKey.toCharArray(), Base64.decode(salt, Base64.DEFAULT), 10000, 256)
|
||||||
|
val tmp = factory.generateSecret(spec)
|
||||||
|
val secretKey = SecretKeySpec(tmp.encoded, "AES")
|
||||||
|
|
||||||
|
val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec)
|
||||||
|
return String(cipher.doFinal(Base64.decode(strToDecrypt, Base64.DEFAULT)))
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Error while decrypting: $e")
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -8,6 +8,7 @@ import androidx.datastore.preferences.core.stringPreferencesKey
|
|||||||
import androidx.datastore.preferences.preferencesDataStore
|
import androidx.datastore.preferences.preferencesDataStore
|
||||||
import kotlinx.coroutines.flow.firstOrNull
|
import kotlinx.coroutines.flow.firstOrNull
|
||||||
import ru.myitschool.work.App
|
import ru.myitschool.work.App
|
||||||
|
import ru.myitschool.work.data.AESEncyption
|
||||||
import ru.myitschool.work.data.dto.AuthRequestDto
|
import ru.myitschool.work.data.dto.AuthRequestDto
|
||||||
import ru.myitschool.work.data.dto.AuthResponseDto
|
import ru.myitschool.work.data.dto.AuthResponseDto
|
||||||
import ru.myitschool.work.data.source.NetworkDataSource
|
import ru.myitschool.work.data.source.NetworkDataSource
|
||||||
@ -21,10 +22,12 @@ object AuthRepository {
|
|||||||
suspend fun checkAndSave(login: String, password: String): Result<AuthResponseDto> {
|
suspend fun checkAndSave(login: String, password: String): Result<AuthResponseDto> {
|
||||||
val data = AuthRequestDto(login=login, password=password)
|
val data = AuthRequestDto(login=login, password=password)
|
||||||
return NetworkDataSource.checkAuth(data).onSuccess { success ->
|
return NetworkDataSource.checkAuth(data).onSuccess { success ->
|
||||||
if (tokenCache != null) {
|
val encryptedTokenCache = AESEncyption.encrypt(success.token)
|
||||||
|
tokenCache = encryptedTokenCache
|
||||||
|
if (encryptedTokenCache != null) {
|
||||||
App.context.userDataStore.edit { preferences ->
|
App.context.userDataStore.edit { preferences ->
|
||||||
val prefKey = stringPreferencesKey(TOKEN_KEY)
|
val prefKey = stringPreferencesKey(TOKEN_KEY)
|
||||||
preferences[prefKey] = success.token
|
preferences[prefKey] = encryptedTokenCache
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -38,7 +41,10 @@ object AuthRepository {
|
|||||||
preferences[stringPreferencesKey(TOKEN_KEY)]
|
preferences[stringPreferencesKey(TOKEN_KEY)]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tokenCache
|
if (tokenCache != null) {
|
||||||
|
return AESEncyption.decrypt(tokenCache)
|
||||||
|
}
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun logout() {
|
suspend fun logout() {
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
package ru.myitschool.work.data.source
|
package ru.myitschool.work.data.source
|
||||||
|
|
||||||
import android.util.Log
|
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.call.body
|
import io.ktor.client.call.body
|
||||||
import io.ktor.client.engine.cio.CIO
|
import io.ktor.client.engine.cio.CIO
|
||||||
@ -47,7 +46,6 @@ object NetworkDataSource {
|
|||||||
contentType(ContentType.Application.Json)
|
contentType(ContentType.Application.Json)
|
||||||
setBody(data)
|
setBody(data)
|
||||||
}
|
}
|
||||||
Log.d("testlog", response.status.toString())
|
|
||||||
when (response.status) {
|
when (response.status) {
|
||||||
HttpStatusCode.OK -> response.body<AuthResponseDto>()
|
HttpStatusCode.OK -> response.body<AuthResponseDto>()
|
||||||
else -> error(response.bodyAsText())
|
else -> error(response.bodyAsText())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user