Contrail reworked and finished

This commit is contained in:
sergeych committed 2024-06-19 05:40:13 +07:00
1 parent f64412fdc7
commit e4e3b5ba8b
3 files changed
+39 -16

No files matched your search

@@ -0,0 +1,30 @@
package net.sergeych.crypto2
import net.sergeych.bintools.CRC
/**
* The crc8-protected binary data manipulation. It uses crc8-bluetooth polynomial (0xA7)
* flavor. Note that crc8 provides good protection only for relatively small data sets.
*/
object Contrail {
/**
* Create a contrail by adding a crc byte to the end of [data]. The Result is always one byte longer.
*/
fun create(data: UByteArray): UByteArray = data + CRC.crc8(data.toByteArray())
/**
* Check the contrail is valid.
*/
fun isValid(data: UByteArray): Boolean = CRC.crc8(
data.copyOfRange(0, data.size - 1).toByteArray()
) == data.last()
/**
* Check that the contrail is valid and extracts its data
* @return the data without crc or null if it is not valid
*/
fun unpack(contrailData: UByteArray): UByteArray? =
if (isValid(contrailData)) contrailData.sliceArray(0..<contrailData.size - 1) else null
}
@@ -1,9 +0,0 @@
package net.sergeych.crypto2
import net.sergeych.bintools.CRC
fun isValidContrail(data: UByteArray): Boolean = CRC.crc8(
data.copyOfRange(1, data.size).toByteArray()
) == data[0]
fun createContrail(data: UByteArray): UByteArray = ubyteArrayOf(CRC.crc8(data.toByteArray())) + data