Skip to content

Commit a07ad19

Browse files
committed
hash fix, unescaping chars
1 parent b03b170 commit a07ad19

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

src/main/kotlin/components/Extensions.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ fun String.runCommand(runner: (command: String, result: String) -> Unit = { _, _
2323
fun String.escape(): String = Regex.escape(this)
2424
fun String.unescape(): String = StringEscapeUtils.unescapeJava(this)
2525
fun String.removeNewLines(): String = this.replace("\n", "")
26+
fun String.androidTreatment(): String {
27+
val va = this.split(" ")
28+
val values = mutableListOf<String>()
29+
va.forEach { value ->
30+
if (value.trim().isNotBlank()) {
31+
values.add(value.trim())
32+
}
33+
}
34+
return values.joinToString(separator = " ")
35+
}
2636

2737
fun File.validForConfiguration(configuration: Configuration): Boolean {
2838
var valid = this.absolutePath.contains("/${configuration.name}/")

src/main/kotlin/components/XParser.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fun parseXML(file: File): List<StringEntity> {
4040
var name = ""
4141
val attributes = mutableListOf<SAttribute>()
4242
var obfuscate = false
43+
var androidTreatment = true
4344
for (a in 0 until node.attributes.length) {
4445
val attribute = node.attributes.item(a)
4546
for (n in 0 until attribute.childNodes.length) {
@@ -49,11 +50,14 @@ fun parseXML(file: File): List<StringEntity> {
4950
if (attribute.nodeName == "hidden" && attr.nodeValue != "false") {
5051
obfuscate = true
5152
}
53+
if (attribute.nodeName == "androidTreatment" && attr.nodeValue == "false") {
54+
androidTreatment = false
55+
}
5256
attributes.add(SAttribute(attribute.nodeName, attr.nodeValue))
5357
}
5458
}
5559
if (obfuscate) {
56-
entities.add(StringEntity(name, attributes, node.firstChild.nodeValue, "string", i))
60+
entities.add(StringEntity(name, attributes, node.textContent, "string", i, androidTreatment))
5761
}
5862
}
5963
return entities
@@ -73,7 +77,7 @@ fun modifyXML(file: File, mainModule: String, key: String, debug: Boolean) {
7377
it.tag == "string" && it.index == i
7478
}
7579
entity?.let {
76-
node.firstChild.nodeValue = obfuscate(mainModule, key, it).value
80+
node.textContent = obfuscate(mainModule, key, it).value
7781
}
7882
}
7983

@@ -85,13 +89,16 @@ fun modifyXML(file: File, mainModule: String, key: String, debug: Boolean) {
8589
}
8690

8791
fun obfuscate(mainModule: String, key: String, entity: StringEntity): StringEntity {
88-
val obfuscation = Stark.obfuscate(mainModule, key, entity.value.unescape().toByteArray()).toReadableString()
89-
return StringEntity(entity.name, entity.attributes, obfuscation, entity.tag, entity.index)
92+
val obfuscation = Stark.obfuscate(mainModule, key, when(entity.androidTreatment) {
93+
true -> entity.value.androidTreatment()
94+
false -> entity.value.unescape()
95+
}.toByteArray()).toReadableString()
96+
return StringEntity(entity.name, entity.attributes, obfuscation, entity.tag, entity.index, entity.androidTreatment)
9097
}
9198

9299
fun reveal(mainModule: String, key: String, entity: StringEntity): StringEntity {
93100
val arr: ByteArray = entity.value.split(", ").map { it.toInt().toByte() }.toByteArray()
94101
val original = String(Stark.reveal(mainModule, key, arr))
95-
return StringEntity(entity.name, entity.attributes, original, entity.tag, entity.index)
102+
return StringEntity(entity.name, entity.attributes, original, entity.tag, entity.index, entity.androidTreatment)
96103
}
97104

4.23 KB
Binary file not shown.

src/main/kotlin/models/StringEntity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class StringEntity(
55
var attributes: List<SAttribute>,
66
var value: String,
77
val tag: String,
8-
val index: Int
8+
val index: Int,
9+
val androidTreatment: Boolean = false
910
)
1011

1112
data class SAttribute(val name: String, val value: String)

src/test/kotlin/SCTest.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ class SCTest {
8080
fun `06 - (PLUGIN) restore string files`() {
8181
prepareTask.runCommand { _, _ ->
8282
assert(restoreFiles(projectName, mainModule).isEmpty())
83-
assert(backupFiles(projectName, defaultConfig()).isNotEmpty())
83+
assert(backupFiles(projectName, defaultConfig().apply {
84+
stringFiles.add("strings_extra.xml")
85+
srcFolders.add("src/other_source")
86+
}).isNotEmpty())
8487
assert(restoreFiles(projectName, mainModule).isNotEmpty())
8588
}
8689
}
@@ -116,7 +119,10 @@ class SCTest {
116119
@Test
117120
fun `09 - (PLUGIN) obfuscate and reveal string values`() {
118121
signingReportTask.runCommand { _, report ->
119-
val files = locateFiles(projectName, defaultConfig())
122+
val files = locateFiles(projectName, defaultConfig().apply {
123+
stringFiles.add("strings_extra.xml")
124+
srcFolders.add("src/other_source")
125+
})
120126
files.forEach { file ->
121127
val entities = parseXML(file.file)
122128
entities.forEach { entity ->
@@ -133,7 +139,10 @@ class SCTest {
133139
obfuscated
134140
)
135141

136-
assert(original.value == entity.value)
142+
assert(original.value == when(entity.androidTreatment) {
143+
true -> entity.value.androidTreatment()
144+
else -> entity.value
145+
})
137146

138147
}
139148
}
@@ -157,6 +166,7 @@ class SCTest {
157166
}
158167
}
159168

169+
160170
@Test
161171
fun `11 - (ANDROID COMPILATION) obfuscate xml and build (not real workflow)`() {
162172
signingReportTask.runCommand { _, report ->
@@ -189,6 +199,7 @@ class SCTest {
189199
}
190200
}
191201

202+
192203
@Test
193204
fun `13 - (ANDROID COMPILATION) plugin running on Android`() {
194205
pluginBuildTask().runCommand { _, report ->

0 commit comments

Comments
 (0)