Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ object TokenEncryptor {
}

fun decrypt(bytes: ByteArray): ByteArray {
val cipher = Cipher.getInstance(TRANSFORMATION)
val iv = bytes.copyOfRange(0, cipher.blockSize)
val data = bytes.copyOfRange(cipher.blockSize, bytes.size)
cipher.init(Cipher.DECRYPT_MODE, getKey(), IvParameterSpec(iv))
return cipher.doFinal(data)
return try {
val cipher = Cipher.getInstance(TRANSFORMATION)
val iv = bytes.copyOfRange(0, cipher.blockSize)
val data = bytes.copyOfRange(cipher.blockSize, bytes.size)
cipher.init(Cipher.DECRYPT_MODE, getKey(), IvParameterSpec(iv))
cipher.doFinal(data)
} catch (e: Exception) {
ByteArray(0)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.konkuk.medicarecall.data.util

import android.util.Log
import androidx.datastore.core.Serializer
import com.konkuk.medicarecall.data.model.Token
import kotlinx.coroutines.Dispatchers
Expand All @@ -17,13 +18,24 @@ object TokenSerializer : Serializer<Token> {
get() = Token(null, null)

override suspend fun readFrom(input: InputStream): Token {
val encryptedBytes = withContext(Dispatchers.IO) {
input.use { it.readBytes() }
return try {
val encryptedBytes = withContext(Dispatchers.IO) {
input.use { it.readBytes() }
}

// 빈 파일인 경우 기본값 반환
if (encryptedBytes.isEmpty()) {
return defaultValue
}

val encryptedBytesDecoded = Base64.getDecoder().decode(encryptedBytes)
val decryptedBytes = TokenEncryptor.decrypt(encryptedBytesDecoded)
val decodedJsonString = decryptedBytes.decodeToString()
Json.decodeFromString(decodedJsonString)
} catch (e: Exception) {
Log.d("TokenSerializer", "Failed to read Token: ${e.message}")
defaultValue
}
val encryptedBytesDecoded = Base64.getDecoder().decode(encryptedBytes)
val decryptedBytes = TokenEncryptor.decrypt(encryptedBytesDecoded)
val decodedJsonString = decryptedBytes.decodeToString()
return Json.decodeFromString(decodedJsonString)
}

override suspend fun writeTo(t: Token, output: OutputStream) {
Expand Down