forked from sergeych/crypto2
		
	Contrail reworked and finished
This commit is contained in:
		
							parent
							
								
									f64412fdc7
								
							
						
					
					
						commit
						e4e3b5ba8b
					
				
							
								
								
									
										30
									
								
								src/commonMain/kotlin/net/sergeych/crypto2/Contrail.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/commonMain/kotlin/net/sergeych/crypto2/Contrail.kt
									
									
									
									
									
										Normal file
									
								
							| @ -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 | ||||
| @ -1,19 +1,21 @@ | ||||
| import kotlinx.coroutines.test.runTest | ||||
| import net.sergeych.crypto2.Contrail | ||||
| import net.sergeych.crypto2.NumericNonce | ||||
| import net.sergeych.crypto2.createContrail | ||||
| import net.sergeych.crypto2.initCrypto | ||||
| import net.sergeych.crypto2.isValidContrail | ||||
| import kotlin.test.* | ||||
| 
 | ||||
| class ToolsTest { | ||||
|     @Test | ||||
|     fun testContrails() = runTest { | ||||
|         initCrypto() | ||||
|         val c = createContrail(ubyteArrayOf(1u, 2u, 3u, 4u, 5u)) | ||||
|         assertEquals(134u, c[0]) | ||||
|         assertTrue { isValidContrail(c) } | ||||
|         c[2] = 11u | ||||
|         assertFalse { isValidContrail(c) } | ||||
|         val data = ubyteArrayOf(1u, 2u, 3u, 4u, 5u) | ||||
|         val c = Contrail.create(data) | ||||
|         assertTrue { Contrail.isValid(c) } | ||||
|         c[2] = c[2] xor 11u | ||||
|         assertFalse { Contrail.isValid(c) } | ||||
|         c[2] = c[2] xor 11u | ||||
|         assertTrue { Contrail.isValid(c) } | ||||
|         assertContentEquals(data,Contrail.unpack(c)) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user