-
Notifications
You must be signed in to change notification settings - Fork 1
/
Encryption.kt
62 lines (47 loc) · 1.72 KB
/
Encryption.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.neva.gradle.fork.encryption
import com.neva.gradle.fork.ForkException
import org.apache.commons.codec.binary.Base64
import java.nio.charset.Charset
internal class Encryption {
private fun encode(bytes: ByteArray): String = BASE64.encodeToString(bytes)
private fun decode(string: String): ByteArray = BASE64.decode(string)
@Suppress("TooGenericExceptionCaught")
fun encrypt(text: String?, context: String? = null): String? {
if (text.isNullOrBlank()) {
return text
}
try {
return "${TOKEN_START}${encode(text.toByteArray(CHARSET))}$TOKEN_END"
} catch (e: Exception) {
throw ForkException("Fork property encryption failed! Context: '${context.orEmpty()}'", e)
}
}
@Suppress("TooGenericExceptionCaught")
fun decrypt(text: String?, context: String? = null): String? {
if (text.isNullOrBlank() || !isEncrypted(text)) {
return text
}
val raw = text.removeSurrounding(TOKEN_START, TOKEN_END)
try {
return decode(raw).toString(CHARSET)
} catch (e: Exception) {
throw ForkException(listOf(
"Fork property decryption failed! Context: '${context.orEmpty()}'",
"Most probably encrypted value got corrupted.",
"Consider regenerating encrypted values to fix the problem."
).joinToString("\n"), e)
}
}
fun isEncrypted(text: String?): Boolean {
if (text.isNullOrBlank()) {
return false
}
return text.startsWith(TOKEN_START) && text.endsWith(TOKEN_END)
}
companion object {
private val BASE64 = Base64(0, byteArrayOf('\r'.toByte(), '\n'.toByte()), false)
private val CHARSET = Charset.forName("UTF8")
private const val TOKEN_START = "{fp/}"
private const val TOKEN_END = "{/fp}"
}
}