Skip to content

Commit db6731f

Browse files
committed
Refactor: Crypto 클래스 리팩토링 및 테스트 코드 수정
- Crypto 클래스를 Crypto 인터페이스와 SecureCrypto 구현체로 분리 - 패키지 구조 변경 (security -> security.crypto) - CryptoTest 클래스명을 SecureCryptoTest로 변경하고, 리팩토링된 클래스에 맞게 수정
1 parent 33004c6 commit db6731f

3 files changed

Lines changed: 32 additions & 23 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.threegap.bitnagil.security.crypto
2+
3+
interface Crypto {
4+
fun encrypt(bytes: ByteArray): ByteArray
5+
6+
fun decrypt(bytes: ByteArray): ByteArray
7+
}

core/security/src/main/java/com/threegap/bitnagil/security/Crypto.kt renamed to core/security/src/main/java/com/threegap/bitnagil/security/crypto/SecureCrypto.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
package com.threegap.bitnagil.security
1+
package com.threegap.bitnagil.security.crypto
22

3+
import com.threegap.bitnagil.security.keystore.KeyProvider
34
import javax.crypto.Cipher
45
import javax.crypto.spec.IvParameterSpec
56

6-
class Crypto(
7+
internal class SecureCrypto(
78
private val keyProvider: KeyProvider,
89
private val transformation: String = "AES/CBC/PKCS7Padding",
9-
) {
10-
fun encrypt(bytes: ByteArray): ByteArray {
10+
) : Crypto {
11+
override fun encrypt(bytes: ByteArray): ByteArray {
1112
val cipher = Cipher.getInstance(transformation)
1213
cipher.init(Cipher.ENCRYPT_MODE, keyProvider.getKey())
1314
val iv = cipher.iv
1415
val encrypted = cipher.doFinal(bytes)
1516
return iv + encrypted
1617
}
1718

18-
fun decrypt(bytes: ByteArray): ByteArray {
19+
override fun decrypt(bytes: ByteArray): ByteArray {
1920
val cipher = Cipher.getInstance(transformation)
2021
require(bytes.size >= cipher.blockSize) {
2122
INVALID_INPUT_TOO_SHORT_MSG

core/security/src/test/java/com/threegap/bitnagil/security/CryptoTest.kt renamed to core/security/src/test/java/com/threegap/bitnagil/security/crypto/SecureCryptoTest.kt

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package com.threegap.bitnagil.security
1+
package com.threegap.bitnagil.security.crypto
22

3+
import com.threegap.bitnagil.security.keystore.KeyProvider
34
import org.junit.Assert.assertEquals
45
import org.junit.Assert.assertNotEquals
56
import org.junit.Assert.assertThrows
@@ -8,7 +9,7 @@ import javax.crypto.BadPaddingException
89
import javax.crypto.KeyGenerator
910
import javax.crypto.SecretKey
1011

11-
class CryptoTest {
12+
class SecureCryptoTest {
1213
private class FakeKeyProvider : KeyProvider {
1314
private val key: SecretKey =
1415
KeyGenerator
@@ -19,8 +20,8 @@ class CryptoTest {
1920
override fun getKey(): SecretKey = key
2021
}
2122

22-
private val crypto =
23-
Crypto(
23+
private val secureCrypto =
24+
SecureCrypto(
2425
keyProvider = FakeKeyProvider(),
2526
transformation = "AES/CBC/PKCS5Padding",
2627
)
@@ -31,8 +32,8 @@ class CryptoTest {
3132
val original = "테스트 데이터".toByteArray()
3233

3334
// when
34-
val encrypted = crypto.encrypt(original)
35-
val decrypted = crypto.decrypt(encrypted)
35+
val encrypted = secureCrypto.encrypt(original)
36+
val decrypted = secureCrypto.decrypt(encrypted)
3637

3738
// then
3839
assertEquals(String(original), String(decrypted))
@@ -44,8 +45,8 @@ class CryptoTest {
4445
val input = "같은 입력".toByteArray()
4546

4647
// when
47-
val encrypted1 = crypto.encrypt(input)
48-
val encrypted2 = crypto.encrypt(input)
48+
val encrypted1 = secureCrypto.encrypt(input)
49+
val encrypted2 = secureCrypto.encrypt(input)
4950

5051
// then
5152
assertNotEquals(encrypted1.toList(), encrypted2.toList())
@@ -58,27 +59,27 @@ class CryptoTest {
5859

5960
// when & then
6061
assertThrows(IllegalArgumentException::class.java) {
61-
crypto.decrypt(invalid)
62+
secureCrypto.decrypt(invalid)
6263
}
6364
}
6465

6566
@Test
6667
fun `빈 바이트 배열 암호화 시 예외가 발생하지 않아야 한다`() {
6768
val input = ByteArray(0)
68-
val encrypted = crypto.encrypt(input)
69-
val decrypted = crypto.decrypt(encrypted)
69+
val encrypted = secureCrypto.encrypt(input)
70+
val decrypted = secureCrypto.decrypt(encrypted)
7071
assertEquals(String(input), String(decrypted))
7172
}
7273

7374
@Test
7475
fun `IV 일부가 조작된 경우 복호화하면 원래 데이터와 달라야 한다`() {
7576
// given
7677
val original = "iv 테스트".toByteArray()
77-
val encrypted = crypto.encrypt(original)
78+
val encrypted = secureCrypto.encrypt(original)
7879
encrypted[0] = encrypted[0].inc()
7980

8081
// when
81-
val decrypted = crypto.decrypt(encrypted)
82+
val decrypted = secureCrypto.decrypt(encrypted)
8283

8384
// then
8485
assertNotEquals(String(original), String(decrypted))
@@ -88,20 +89,20 @@ class CryptoTest {
8889
fun `암호화된 데이터가 조작된 경우 복호화 실패해야 한다`() {
8990
// given
9091
val original = "데이터 조작".toByteArray()
91-
val encrypted = crypto.encrypt(original)
92+
val encrypted = secureCrypto.encrypt(original)
9293
encrypted[encrypted.lastIndex] = encrypted.last().inc()
9394

9495
// when & then
9596
assertThrows(BadPaddingException::class.java) {
96-
crypto.decrypt(encrypted)
97+
secureCrypto.decrypt(encrypted)
9798
}
9899
}
99100

100101
@Test
101102
fun `다른 키로 복호화하면 실패해야 한다`() {
102103
// given
103104
val original = "다른 키 테스트".toByteArray()
104-
val encrypted = crypto.encrypt(original)
105+
val encrypted = secureCrypto.encrypt(original)
105106

106107
val otherKeyProvider =
107108
object : KeyProvider {
@@ -112,11 +113,11 @@ class CryptoTest {
112113
}
113114
}
114115

115-
val otherCrypto = Crypto(otherKeyProvider, "AES/CBC/PKCS5Padding")
116+
val otherSecureCrypto = SecureCrypto(otherKeyProvider, "AES/CBC/PKCS5Padding")
116117

117118
// when & then
118119
assertThrows(BadPaddingException::class.java) {
119-
otherCrypto.decrypt(encrypted)
120+
otherSecureCrypto.decrypt(encrypted)
120121
}
121122
}
122123
}

0 commit comments

Comments
 (0)