Skip to content

Commit

Permalink
fix: revocation policy encoded list decoding - condider bit order lef…
Browse files Browse the repository at this point in the history
…t-to-right
  • Loading branch information
mikeplotean committed Feb 5, 2025
1 parent c2c32fa commit 434f410
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package id.walt.policies.policies

import id.walt.policies.policies.StreamUtils.getBitValue
import io.github.oshai.kotlinlogging.KotlinLogging
import io.ktor.client.*
import io.ktor.client.plugins.contentnegotiation.*
Expand Down Expand Up @@ -100,20 +101,30 @@ object StreamUtils {
}

private fun extractBitValue(bytes: ByteArray, index: ULong, bitSize: UInt): List<Char> {
val bitSet = BitSet.valueOf(bytes)
val bits = bytes.toBitSequence()
val bitStart = index * bitSize % BITS_PER_BYTE
val result = mutableListOf<Char>()
for (i in bitStart..<bitStart + bitSize) {
val b = bitSet[i.toInt()].takeIf { it }?.let { 1 } ?: 0
result.add(b.digitToChar())
val bitSet = bits.drop(bitStart.toInt())
var b = 0u
val result = mutableListOf<Boolean>()
while (b++ < bitSize) {
result.add(bitSet.iterator().next())
}
return result
return result.map { if (it) '1' else '0' }
}
}

fun get(bitstring: String, idx: ULong? = null, bitSize: Int = 1) =
idx?.let { StreamUtils.getBitValue(GZIPInputStream(Base64Utils.decode(bitstring).inputStream()), it, bitSize) }
idx?.let { getBitValue(GZIPInputStream(Base64Utils.decode(bitstring).inputStream()), it, bitSize) }

fun isBinaryValue(value: List<Char>) = setOf('0', '1').let { valid ->
value.all { it in valid }
}

fun ByteArray.toBitSequence(): Sequence<Boolean> = sequence {
for (byte in this@toBitSequence) {
for (i in 7 downTo 0) { // Iterate through bits from left to right (MSB to LSB)
val bit = (byte.toInt() shr i) and 1 == 1 // Extract the i-th bit
yield(bit)
}
}
}

0 comments on commit 434f410

Please sign in to comment.