Skip to content

Commit

Permalink
feat(types): add obfuscation for number types
Browse files Browse the repository at this point in the history
  • Loading branch information
vfmunhoz committed Jun 28, 2021
1 parent c717b81 commit 8196e5e
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 7 deletions.
13 changes: 12 additions & 1 deletion .sv4git.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ commit-message:
- style
- test
scope:
values: ["configs", "tests", "types", "protected-byte", "protected-short", "protected-int", "protected-long", "protected-float", "protected-double"]
values: [
"configs",
"extensions"
"types",
"protected-byte",
"protected-short",
"protected-int",
"protected-long",
"protected-float",
"protected-double",
"tests",
]
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)
}
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()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vfmunhoz.protectedtypes.types

import com.vfmunhoz.protectedtypes.extensions.obfuscate

@JvmInline
value class ProtectedByte(val value: Byte) : Comparable<ProtectedByte> {

Expand Down Expand Up @@ -87,7 +89,7 @@ value class ProtectedByte(val value: Byte) : Comparable<ProtectedByte> {
operator fun compareTo(other: Byte): Int = value.compareTo(other)
override operator fun compareTo(other: ProtectedByte): Int = compareTo(other.value)

override fun toString(): String = "###$value###"
override fun toString(): String = value.obfuscate()
}

// Primitive + protected
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vfmunhoz.protectedtypes.types

import com.vfmunhoz.protectedtypes.extensions.obfuscate

@JvmInline
value class ProtectedDouble(val value: Double) : Comparable<ProtectedDouble> {

Expand Down Expand Up @@ -87,7 +89,7 @@ value class ProtectedDouble(val value: Double) : Comparable<ProtectedDouble> {
operator fun compareTo(other: Double): Int = value.compareTo(other)
override operator fun compareTo(other: ProtectedDouble): Int = compareTo(other.value)

override fun toString(): String = "###$value###"
override fun toString(): String = value.obfuscate()
}

// Primitive + protected
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vfmunhoz.protectedtypes.types

import com.vfmunhoz.protectedtypes.extensions.obfuscate

@JvmInline
value class ProtectedFloat(val value: Float) : Comparable<ProtectedFloat> {

Expand Down Expand Up @@ -87,7 +89,7 @@ value class ProtectedFloat(val value: Float) : Comparable<ProtectedFloat> {
operator fun compareTo(other: Float): Int = value.compareTo(other)
override operator fun compareTo(other: ProtectedFloat): Int = compareTo(other.value)

override fun toString(): String = "###$value###"
override fun toString(): String = value.obfuscate()
}

// Primitive + protected
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vfmunhoz.protectedtypes.types

import com.vfmunhoz.protectedtypes.extensions.obfuscate

@JvmInline
value class ProtectedInt(val value: Int) : Comparable<ProtectedInt> {

Expand Down Expand Up @@ -87,7 +89,7 @@ value class ProtectedInt(val value: Int) : Comparable<ProtectedInt> {
operator fun compareTo(other: Int): Int = value.compareTo(other)
override operator fun compareTo(other: ProtectedInt): Int = compareTo(other.value)

override fun toString(): String = "###$value###"
override fun toString(): String = value.obfuscate()
}

// Primitive + protected
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vfmunhoz.protectedtypes.types

import com.vfmunhoz.protectedtypes.extensions.obfuscate

@JvmInline
value class ProtectedLong(val value: Long) : Comparable<ProtectedLong> {

Expand Down Expand Up @@ -87,7 +89,7 @@ value class ProtectedLong(val value: Long) : Comparable<ProtectedLong> {
operator fun compareTo(other: Long): Int = value.compareTo(other)
override operator fun compareTo(other: ProtectedLong): Int = compareTo(other.value)

override fun toString(): String = "###$value###"
override fun toString(): String = value.obfuscate()
}

// Primitive + protected
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vfmunhoz.protectedtypes.types

import com.vfmunhoz.protectedtypes.extensions.obfuscate

@JvmInline
value class ProtectedShort(val value: Short) : Comparable<ProtectedShort> {

Expand Down Expand Up @@ -87,7 +89,7 @@ value class ProtectedShort(val value: Short) : Comparable<ProtectedShort> {
operator fun compareTo(other: Short): Int = value.compareTo(other)
override operator fun compareTo(other: ProtectedShort): Int = compareTo(other.value)

override fun toString(): String = "###$value###"
override fun toString(): String = value.obfuscate()
}

// Primitive + protected
Expand Down
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)
}
}
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')))
}
}

0 comments on commit 8196e5e

Please sign in to comment.