diff --git a/build.gradle.kts b/build.gradle.kts index f2f567b..94b3697 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,7 +20,7 @@ plugins { } group = "net.sergeych" -version = "0.8.2-SNAPSHOT" +version = "0.8.3-SNAPSHOT" repositories { mavenCentral() @@ -44,11 +44,11 @@ kotlin { linuxX64() linuxArm64() - macosX64() - macosArm64() - iosX64() - iosArm64() - iosSimulatorArm64() +// macosX64() +// macosArm64() +// iosX64() +// iosArm64() +// iosSimulatorArm64() // mingwX64() @OptIn(ExperimentalWasmDsl::class) wasmJs { @@ -72,7 +72,7 @@ kotlin { implementation(project.dependencies.platform("org.kotlincrypto.hash:bom:0.5.1")) implementation("org.kotlincrypto.hash:sha3") api("com.ionspin.kotlin:bignum:0.3.9") - api("net.sergeych:mp_bintools:0.1.7") + api("net.sergeych:mp_bintools:0.1.11") api("net.sergeych:mp_stools:1.5.1") } } diff --git a/src/commonMain/kotlin/net/sergeych/crypto2/ByteChunk.kt b/src/commonMain/kotlin/net/sergeych/crypto2/ByteChunk.kt deleted file mode 100644 index b619b7a..0000000 --- a/src/commonMain/kotlin/net/sergeych/crypto2/ByteChunk.kt +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright (c) 2025. Sergey S. Chernov - All Rights Reserved - * - * You may use, distribute and modify this code under the - * terms of the private license, which you must obtain from the author - * - * To obtain the license, contact the author: https://t.me/real_sergeych or email to - * real dot sergeych at gmail. - */ - -package net.sergeych.crypto2 - -import kotlinx.serialization.Serializable -import net.sergeych.bintools.decodeHex -import net.sergeych.bintools.encodeToHex -import kotlin.math.min - -/** - * Bytes sequence with comparison, concatenation, and string representation, - * could be used as hash keys for pure binary values, etc. - */ -@Suppress("unused") -@Serializable -class ByteChunk(val data: UByteArray): Comparable { - - val size: Int get() = data.size - - /** - * Per-byte comparison also of different length. From two chunks - * of different size but equal beginning, the shorter is considered - * the smaller. - */ - override fun compareTo(other: ByteChunk): Int { - val limit = min(size, other.size) - for( i in 0 ..< limit) { - val own = data[i] - val their = other.data[i] - if( own < their) return -1 - else if( own > their) return 1 - } - if( size < other.size ) return -1 - if( size > other.size ) return 1 - return 0 - } - - /** - * Equal chunks means content equality. - */ - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is ByteChunk) return false - - return data contentEquals other.data - } - - /** - * Content-based hash code - */ - override fun hashCode(): Int { - return data.contentHashCode() - } - - /** - * hex representation of data - */ - override fun toString(): String = base64Url - - /** - * Hex encoded data - */ - val hex by lazy { data.encodeToHex() } - - val base64Url by lazy { data.encodeToBase64Url() } - - /** - * human-readable dump - */ - val dump by lazy { data.toDump() } - - /** - * Concatenate two chunks and return new one - */ - operator fun plus(other: ByteChunk): ByteChunk = ByteChunk(data + other.data) - - fun toByteArray(): ByteArray = data.asByteArray() - fun toUByteArray(): UByteArray = data - - companion object { - fun fromHex(hex: String): ByteChunk = ByteChunk(hex.decodeHex().asUByteArray()) - fun random(sizeInBytes: Int=16) = randomUBytes(sizeInBytes).toChunk() - } -} - -private fun UByteArray.toChunk(): ByteChunk = ByteChunk(this) -@Suppress("unused") -private fun ByteArray.toChunk(): ByteChunk = ByteChunk(this.asUByteArray()) - -@Suppress("unused") -fun ByteArray.asChunk() = ByteChunk(toUByteArray()) -fun UByteArray.asChunk(): ByteChunk = ByteChunk(this) \ No newline at end of file diff --git a/src/commonMain/kotlin/net/sergeych/crypto2/Hash.kt b/src/commonMain/kotlin/net/sergeych/crypto2/Hash.kt index 18bdee9..4fedabf 100644 --- a/src/commonMain/kotlin/net/sergeych/crypto2/Hash.kt +++ b/src/commonMain/kotlin/net/sergeych/crypto2/Hash.kt @@ -14,6 +14,8 @@ import com.ionspin.kotlin.crypto.generichash.GenericHash import com.ionspin.kotlin.crypto.util.encodeToUByteArray import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.flow.Flow +import net.sergeych.bintools.ByteChunk +import net.sergeych.bintools.asChunk import org.kotlincrypto.hash.sha3.SHA3_256 import org.kotlincrypto.hash.sha3.SHA3_384