-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types): add obfuscation for number types
- Loading branch information
Showing
11 changed files
with
139 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/vfmunhoz/protectedtypes/extensions/NumberExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.vfmunhoz.protectedtypes.extensions | ||
|
||
import kotlin.math.floor | ||
|
||
fun Number.obfuscate(): String = toString().let { | ||
val from = floor(it.length / 2.0).toInt() | ||
val to = it.length - 1 | ||
|
||
when(this) { | ||
is Float, is Double -> it.replaceFrom(from, to, "#", setOf('-', '.')) | ||
else -> it.replaceFrom(from, to, "#", setOf('-')) | ||
} | ||
} | ||
|
||
fun main() { | ||
print(100123.1456f) | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/com/vfmunhoz/protectedtypes/extensions/StringExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.vfmunhoz.protectedtypes.extensions | ||
|
||
import java.lang.RuntimeException | ||
|
||
fun String.replaceFrom(start: Int, end: Int, replacement: CharSequence, ignores: Set<Char> = emptySet()): String { | ||
if(start > end) throw throw IndexOutOfBoundsException("End index ($end) is less than start index ($start).") | ||
|
||
val builder = StringBuilder() | ||
|
||
builder.appendRange(this, 0, start) | ||
for(pos in start.. end) { | ||
if(ignores.contains(this[pos])) { | ||
builder.append(this[pos]) | ||
continue | ||
} | ||
|
||
builder.append(replacement) | ||
} | ||
builder.appendRange(this, end + 1, this.length) | ||
|
||
return builder.toString() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/test/kotlin/com/vfmunhoz/protectedtypes/extensions/NumberExtensionsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.vfmunhoz.protectedtypes.extensions | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
internal class NumberExtensionsTest { | ||
|
||
@Test | ||
fun `tests obfuscation function for integer types`() { | ||
val myByte: Byte = 10 | ||
val myShort: Short = 100 | ||
val myInteger = 1000 | ||
val myLong: Long = 10000 | ||
|
||
assertEquals("1#", myByte.obfuscate()) | ||
assertEquals("1##", myShort.obfuscate()) | ||
assertEquals("10##", myInteger.obfuscate()) | ||
assertEquals("10###", myLong.obfuscate()) | ||
} | ||
|
||
@Test | ||
fun `tests obfuscation function for floating point types`() { | ||
val smallFloat = 100.01f | ||
val smallDouble = 100.01 | ||
|
||
val smallFloatObfuscated = smallFloat.obfuscate() | ||
|
||
assertTrue(smallFloatObfuscated.contains('.')) | ||
assertEquals(smallFloat.toString().length, smallFloatObfuscated.length) | ||
assertEquals(smallFloat.toString().indexOf('.'), smallFloatObfuscated.indexOf('.')) | ||
assertEquals("100.##", smallFloatObfuscated) | ||
|
||
val smallDoubleObfuscated = smallFloat.obfuscate() | ||
|
||
assertTrue(smallDoubleObfuscated.contains('.')) | ||
assertEquals(smallDouble.toString().length, smallDoubleObfuscated.length) | ||
assertEquals(smallDouble.toString().indexOf('.'), smallDoubleObfuscated.indexOf('.')) | ||
assertEquals("100.##", smallDoubleObfuscated) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/kotlin/com/vfmunhoz/protectedtypes/extensions/StringExtensionsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.vfmunhoz.protectedtypes.extensions | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
|
||
internal class StringExtensionsTest { | ||
|
||
private val text = "Hello ProtectedTypes!" | ||
|
||
@Test | ||
fun `should throw an exception if start position is greater than end position`() { | ||
assertFailsWith<IndexOutOfBoundsException> { text.replaceFrom(8, 1, "@") } | ||
} | ||
|
||
@Test | ||
fun `replaces part of a string without skipping any char`() { | ||
assertEquals("Hello @@@@@@@@@@@@@@@", text.replaceFrom(6, text.length -1, "@")) | ||
assertEquals("@@@@@ ProtectedTypes!", text.replaceFrom(0, 4, "@")) | ||
assertEquals("Hello @@@@@@@@@Types!", text.replaceFrom(6, 14, "@")) | ||
} | ||
|
||
@Test | ||
fun `replaces part of a string skipping multiple chars`() { | ||
assertEquals("Hello @@@@@@@@@@@@@@!", text.replaceFrom(6, text.length -1, "@", setOf('!'))) | ||
assertEquals("@@ll@ ProtectedTypes!", text.replaceFrom(0, 4, "@", setOf('l'))) | ||
assertEquals("Hello P@@t@@t@@Types!", text.replaceFrom(6, 14, "@", setOf('P', 't'))) | ||
} | ||
} |