Stops accepting space as a Valid hex char and requires an even number of chars (padding)

This commit is contained in:
Vitor Pamplona 2023-09-09 14:33:19 -04:00
parent be2402702f
commit cda821e5af

View File

@ -12,18 +12,19 @@ fun HexKey.hexToByteArray(): ByteArray {
}
object HexValidator {
private fun isHex2(c: Char): Boolean {
private fun isHexChar(c: Char): Boolean {
return when (c) {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F', ' ' -> true
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F' -> true
else -> false
}
}
fun isHex(hex: String?): Boolean {
if (hex == null) return false
if (hex.length % 2 != 0) return false // must be even
var isHex = true
for (c in hex.toCharArray()) {
if (!isHex2(c)) {
if (!isHexChar(c)) {
isHex = false
break
}