From e4e3b5ba8be802010d38822a50c07d0f5e927e89 Mon Sep 17 00:00:00 2001 From: sergeych Date: Wed, 19 Jun 2024 05:40:13 +0700 Subject: [PATCH] Contrail reworked and finished --- .../kotlin/net/sergeych/crypto2/Contrail.kt | 30 +++++++++++++++++++ .../kotlin/net/sergeych/crypto2/contrail.kt | 9 ------ src/commonTest/kotlin/ToolsTest.kt | 16 +++++----- 3 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 src/commonMain/kotlin/net/sergeych/crypto2/Contrail.kt delete mode 100644 src/commonMain/kotlin/net/sergeych/crypto2/contrail.kt diff --git a/src/commonMain/kotlin/net/sergeych/crypto2/Contrail.kt b/src/commonMain/kotlin/net/sergeych/crypto2/Contrail.kt new file mode 100644 index 0000000..7239674 --- /dev/null +++ b/src/commonMain/kotlin/net/sergeych/crypto2/Contrail.kt @@ -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..