From 9579d3c97bd7803b7b2ba3effeb705f044142d8d Mon Sep 17 00:00:00 2001 From: Infendro Date: Thu, 24 Jul 2025 20:35:51 +0200 Subject: [PATCH] use bytearray library --- build.gradle.kts | 7 ++ gradle/libs.versions.toml | 4 + .../kotlin/com/infendro/hash/MD5.kt | 8 +- .../kotlin/com/infendro/hash/SHA1.kt | 6 +- .../kotlin/com/infendro/hash/SHA224.kt | 6 +- .../kotlin/com/infendro/hash/SHA256.kt | 6 +- .../kotlin/com/infendro/hash/SHA384.kt | 6 +- .../kotlin/com/infendro/hash/SHA512.kt | 6 +- .../com/infendro/hash/util/ByteArray.kt | 119 ------------------ 9 files changed, 30 insertions(+), 138 deletions(-) delete mode 100644 src/commonMain/kotlin/com/infendro/hash/util/ByteArray.kt diff --git a/build.gradle.kts b/build.gradle.kts index 6b3972c..d84a237 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,6 +2,7 @@ group = "com.infendro" version = "1.2.0" repositories { + maven("https://git.infendro.com/api/packages/Infendro/maven") mavenCentral() } @@ -16,6 +17,12 @@ kotlin { nodejs() } linuxX64() + + sourceSets { + commonMain.dependencies { + implementation(libs.bytearray) + } + } } publishing { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index bb704f3..ade18cb 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,9 @@ [versions] kotlin = "2.1.20" +bytearray = "1.1.0" [plugins] multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } + +[libraries] +bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" } diff --git a/src/commonMain/kotlin/com/infendro/hash/MD5.kt b/src/commonMain/kotlin/com/infendro/hash/MD5.kt index 0a51ced..1bb0508 100644 --- a/src/commonMain/kotlin/com/infendro/hash/MD5.kt +++ b/src/commonMain/kotlin/com/infendro/hash/MD5.kt @@ -1,9 +1,9 @@ package com.infendro.hash -import com.infendro.hash.util.ByteOrder.LITTLE_ENDIAN -import com.infendro.hash.util.addAll -import com.infendro.hash.util.toByteArray -import com.infendro.hash.util.toUIntArray +import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN +import com.infendro.bytearray.addAll +import com.infendro.bytearray.toByteArray +import com.infendro.bytearray.toUIntArray object MD5 : HashFunction() { override val bytes: Int diff --git a/src/commonMain/kotlin/com/infendro/hash/SHA1.kt b/src/commonMain/kotlin/com/infendro/hash/SHA1.kt index 744cabd..9ab3020 100644 --- a/src/commonMain/kotlin/com/infendro/hash/SHA1.kt +++ b/src/commonMain/kotlin/com/infendro/hash/SHA1.kt @@ -1,8 +1,8 @@ package com.infendro.hash -import com.infendro.hash.util.addAll -import com.infendro.hash.util.toByteArray -import com.infendro.hash.util.toUIntArray +import com.infendro.bytearray.addAll +import com.infendro.bytearray.toByteArray +import com.infendro.bytearray.toUIntArray object SHA1 : HashFunction() { override val bytes: Int diff --git a/src/commonMain/kotlin/com/infendro/hash/SHA224.kt b/src/commonMain/kotlin/com/infendro/hash/SHA224.kt index 9cb74bb..7946009 100644 --- a/src/commonMain/kotlin/com/infendro/hash/SHA224.kt +++ b/src/commonMain/kotlin/com/infendro/hash/SHA224.kt @@ -1,8 +1,8 @@ package com.infendro.hash -import com.infendro.hash.util.addAll -import com.infendro.hash.util.toByteArray -import com.infendro.hash.util.toUIntArray +import com.infendro.bytearray.addAll +import com.infendro.bytearray.toByteArray +import com.infendro.bytearray.toUIntArray object SHA224 : HashFunction() { override val bytes: Int diff --git a/src/commonMain/kotlin/com/infendro/hash/SHA256.kt b/src/commonMain/kotlin/com/infendro/hash/SHA256.kt index bbed26d..02a401c 100644 --- a/src/commonMain/kotlin/com/infendro/hash/SHA256.kt +++ b/src/commonMain/kotlin/com/infendro/hash/SHA256.kt @@ -1,8 +1,8 @@ package com.infendro.hash -import com.infendro.hash.util.addAll -import com.infendro.hash.util.toByteArray -import com.infendro.hash.util.toUIntArray +import com.infendro.bytearray.addAll +import com.infendro.bytearray.toByteArray +import com.infendro.bytearray.toUIntArray object SHA256 : HashFunction() { override val bytes: Int diff --git a/src/commonMain/kotlin/com/infendro/hash/SHA384.kt b/src/commonMain/kotlin/com/infendro/hash/SHA384.kt index 93a2bea..da5e74f 100644 --- a/src/commonMain/kotlin/com/infendro/hash/SHA384.kt +++ b/src/commonMain/kotlin/com/infendro/hash/SHA384.kt @@ -1,8 +1,8 @@ package com.infendro.hash -import com.infendro.hash.util.addAll -import com.infendro.hash.util.toByteArray -import com.infendro.hash.util.toULongArray +import com.infendro.bytearray.addAll +import com.infendro.bytearray.toByteArray +import com.infendro.bytearray.toULongArray object SHA384 : HashFunction() { override val bytes: Int diff --git a/src/commonMain/kotlin/com/infendro/hash/SHA512.kt b/src/commonMain/kotlin/com/infendro/hash/SHA512.kt index 92b5404..e8f9dbd 100644 --- a/src/commonMain/kotlin/com/infendro/hash/SHA512.kt +++ b/src/commonMain/kotlin/com/infendro/hash/SHA512.kt @@ -1,8 +1,8 @@ package com.infendro.hash -import com.infendro.hash.util.addAll -import com.infendro.hash.util.toByteArray -import com.infendro.hash.util.toULongArray +import com.infendro.bytearray.addAll +import com.infendro.bytearray.toByteArray +import com.infendro.bytearray.toULongArray object SHA512 : HashFunction() { override val bytes: Int diff --git a/src/commonMain/kotlin/com/infendro/hash/util/ByteArray.kt b/src/commonMain/kotlin/com/infendro/hash/util/ByteArray.kt deleted file mode 100644 index 20fa7ff..0000000 --- a/src/commonMain/kotlin/com/infendro/hash/util/ByteArray.kt +++ /dev/null @@ -1,119 +0,0 @@ -package com.infendro.hash.util - -import com.infendro.hash.util.ByteOrder.BIG_ENDIAN -import com.infendro.hash.util.ByteOrder.LITTLE_ENDIAN - -internal fun MutableCollection.addAll( - bytes: ByteArray, -): Boolean { - return addAll(bytes.toList()) -} - -enum class ByteOrder { - BIG_ENDIAN, - LITTLE_ENDIAN, -} - -internal fun UInt.toByteArray( - order: ByteOrder = BIG_ENDIAN, -): ByteArray { - val bytes = ByteArray(4) { i -> - val offset = (3 - i) * 8 - (this shr offset).toByte() - } - - return when (order) { - BIG_ENDIAN -> bytes - LITTLE_ENDIAN -> bytes.reversedArray() - } -} - -internal fun ULong.toByteArray( - order: ByteOrder = BIG_ENDIAN, -): ByteArray { - val bytes = ByteArray(8) { i -> - val offset = (7 - i) * 8 - (this shr offset).toByte() - } - - return when (order) { - BIG_ENDIAN -> bytes - LITTLE_ENDIAN -> bytes.reversedArray() - } -} - -internal fun ByteArray.toUInt( - order: ByteOrder = BIG_ENDIAN, -): UInt { - if (size != 4) throw Exception() - - val bytes = when (order) { - BIG_ENDIAN -> this - LITTLE_ENDIAN -> this.reversedArray() - } - - return bytes.fold(0U) { acc, byte -> - (acc shl 8) or (byte.toUInt() and 0xFFU) - } -} - -internal fun Collection.toUInt( - order: ByteOrder = BIG_ENDIAN, -): UInt { - return toByteArray().toUInt(order) -} - -internal fun ByteArray.toULong( - order: ByteOrder = BIG_ENDIAN, -): ULong { - if (size != 8) throw Exception() - - val bytes = when (order) { - BIG_ENDIAN -> this - LITTLE_ENDIAN -> this.reversedArray() - } - - return bytes.fold(0UL) { acc, byte -> - (acc shl 8) or (byte.toULong() and 0xFFUL) - } -} - -internal fun Collection.toULong( - order: ByteOrder = BIG_ENDIAN, -): ULong { - return toByteArray().toULong(order) -} - -internal fun ByteArray.toUIntArray( - order: ByteOrder = BIG_ENDIAN, -): UIntArray { - if (size % 4 != 0) throw Exception() - - return toList() - .chunked(4) - .map { it.toUInt(order) } - .toUIntArray() -} - -internal fun Collection.toUIntArray( - order: ByteOrder = BIG_ENDIAN, -): UIntArray { - return toByteArray().toUIntArray(order) -} - -internal fun ByteArray.toULongArray( - order: ByteOrder = BIG_ENDIAN, -): ULongArray { - if (size % 8 != 0) throw Exception() - - return toList() - .chunked(8) - .map { it.toULong(order) } - .toULongArray() -} - -internal fun Collection.toULongArray( - order: ByteOrder = BIG_ENDIAN, -): ULongArray { - return toByteArray().toULongArray(order) -}