Skip to content

Commit c8c9205

Browse files
committed
[BOOK-76] refactor: 토끼 리뷰 반영
Cipher는 thread-safe 하지 않고, 한 번 doFinal() 호출 후 내부 상태가 변경되기 때문에 Singleton 클래스 내에서 필드로 보관하면 위험함(다중 호출 시 IllegalStateException 이나 경쟁 상태가 발생할 수 있음)
1 parent 1cddd5d commit c8c9205

File tree

1 file changed

+4
-1
lines changed
  • core/datastore/impl/src/main/kotlin/com/ninecraft/booket/core/datastore/impl/security

1 file changed

+4
-1
lines changed

core/datastore/impl/src/main/kotlin/com/ninecraft/booket/core/datastore/impl/security/CryptoManager.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ import javax.inject.Singleton
1313

1414
@Singleton
1515
class CryptoManager @Inject constructor() {
16-
private val cipher = Cipher.getInstance(TRANSFORMATION)
1716
private val keyStore = KeyStore
1817
.getInstance("AndroidKeyStore")
1918
.apply {
2019
load(null)
2120
}
2221

22+
private fun newCipher() = Cipher.getInstance(TRANSFORMATION)
23+
2324
private fun getKey(): SecretKey {
2425
val existingKey = keyStore
2526
.getEntry(KEY_ALIAS, null) as? KeyStore.SecretKeyEntry
@@ -47,6 +48,7 @@ class CryptoManager @Inject constructor() {
4748
}
4849

4950
fun encrypt(plainText: String): String {
51+
val cipher = newCipher()
5052
cipher.init(Cipher.ENCRYPT_MODE, getKey())
5153
val iv = cipher.iv
5254
val encryptedBytes = cipher.doFinal(plainText.toByteArray())
@@ -58,6 +60,7 @@ class CryptoManager @Inject constructor() {
5860
val combined = Base64.decode(encodedText, Base64.NO_WRAP)
5961
val iv = combined.copyOfRange(0, IV_SIZE)
6062
val encrypted = combined.copyOfRange(IV_SIZE, combined.size)
63+
val cipher = newCipher()
6164
cipher.init(Cipher.DECRYPT_MODE, getKey(), IvParameterSpec(iv))
6265
val decryptedString = String(cipher.doFinal(encrypted))
6366
return decryptedString

0 commit comments

Comments
 (0)