Home > Back-end >  Kotlin - Reading the least significant bit (Steganography)
Kotlin - Reading the least significant bit (Steganography)

Time:12-03

I am creating a program that can read the least significant bit of a image file using Kotlin. I have a function that reads the bytes in a file, but I am unsure how to actully print the bytes in the function consumeArray.

My goal is to print the least significant bits of a image.

override fun run() {
    val buff = ByteArray(1230)
    File("src\\main\\kotlin\\day01_least_significant_bit_steganography\\eksempel_bakgrunnsbilde.png").inputStream().buffered().use { input ->
        while(true) {
            val sz = input.read(buff)
            if (sz <= 0) break

            ///at that point we have a sz bytes in the buff to process
            consumeArray(buff, 0, sz)
        }
    }
} // run

private fun consumeArray(buff: ByteArray, i: Int, sz: Int) {
    println("??")
} // consumeArray

CodePudding user response:

In Kotlin 1.4 you can get the least significant bit of any byte with .takeLowestOneBit() method.

It may happen that it's equal to zero, so you need to iterate byteArray until any non-zero least significant bit is met (I believe this is what was meant under "least significant bit of byteArray"):

var lowestBit: Byte = 0
for (index in sz - 1 downTo 0) {
    val currentLowestBit = buff[index].takeLowestOneBit()
    if (currentLowestBit != 0.toByte()) {
        lowestBit = currentLowestBit
        break
    }
}

Note that it will print the least significant bit of your buffer, not the whole image (if it's bigger than the buffer)

  • Related