use bytearray library

This commit is contained in:
2025-07-24 20:35:51 +02:00
parent 2a11ec3653
commit 9579d3c97b
9 changed files with 30 additions and 138 deletions

View File

@@ -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 {

View File

@@ -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" }

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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<Byte>.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<Byte>.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<Byte>.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<Byte>.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<Byte>.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray {
return toByteArray().toULongArray(order)
}