Improving performance of the Hex encoder.

This commit is contained in:
Vitor Pamplona
2024-12-03 14:07:13 -05:00
parent 869debaf9d
commit b9883093ac
4 changed files with 97 additions and 68 deletions
@@ -23,6 +23,8 @@ package com.vitorpamplona.quartz.encoders
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.quartz.crypto.CryptoUtils
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
@@ -69,4 +71,43 @@ class HexEncodingTest {
)
}
}
@Test
fun testIsHex() {
assertFalse("/0", HexValidator.isHex("/0"))
assertFalse("/.", HexValidator.isHex("/."))
assertFalse("!!", HexValidator.isHex("!!"))
assertFalse("::", HexValidator.isHex("::"))
assertFalse("@@", HexValidator.isHex("@@"))
assertFalse("GG", HexValidator.isHex("GG"))
assertFalse("FG", HexValidator.isHex("FG"))
assertFalse("`a", HexValidator.isHex("`a"))
assertFalse("gg", HexValidator.isHex("gg"))
assertFalse("fg", HexValidator.isHex("fg"))
}
@OptIn(ExperimentalStdlibApi::class)
@Test
fun testRandomsIsHex() {
for (i in 0..10000) {
val bytes = CryptoUtils.privkeyCreate()
val hex = bytes.toHexString(HexFormat.Default)
assertTrue(hex, HexValidator.isHex(hex))
val hexUpper = bytes.toHexString(HexFormat.UpperCase)
assertTrue(hexUpper, HexValidator.isHex(hexUpper))
}
}
@OptIn(ExperimentalStdlibApi::class)
@Test
fun testRandomsUppercase() {
for (i in 0..1000) {
val bytes = CryptoUtils.privkeyCreate()
val hex = bytes.toHexString(HexFormat.UpperCase)
assertEquals(
bytes.toList(),
Hex.decode(hex).toList(),
)
}
}
}