Compare commits
3 Commits
56f1316e43
...
065621bf36
| Author | SHA1 | Date | |
|---|---|---|---|
|
065621bf36
|
|||
|
9579d3c97b
|
|||
|
2a11ec3653
|
@@ -1,7 +1,8 @@
|
|||||||
group = "com.infendro"
|
group = "com.infendro"
|
||||||
version = "1.2.0"
|
version = "1.2.1"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,6 +17,12 @@ kotlin {
|
|||||||
nodejs()
|
nodejs()
|
||||||
}
|
}
|
||||||
linuxX64()
|
linuxX64()
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
commonMain.dependencies {
|
||||||
|
implementation(libs.bytearray)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
publishing {
|
publishing {
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
[versions]
|
[versions]
|
||||||
kotlin = "2.1.20"
|
kotlin = "2.1.20"
|
||||||
|
bytearray = "1.1.0"
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
||||||
|
|
||||||
|
[libraries]
|
||||||
|
bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" }
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package com.infendro.hash
|
package com.infendro.hash
|
||||||
|
|
||||||
import com.infendro.hash.util.ByteOrder.LITTLE_ENDIAN
|
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
|
||||||
import com.infendro.hash.util.addAll
|
import com.infendro.bytearray.addAll
|
||||||
import com.infendro.hash.util.toByteArray
|
import com.infendro.bytearray.toByteArray
|
||||||
import com.infendro.hash.util.toUIntArray
|
import com.infendro.bytearray.toUIntArray
|
||||||
|
|
||||||
object MD5 : HashFunction() {
|
object MD5 : HashFunction() {
|
||||||
override val bytes: Int
|
override val bytes: Int
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package com.infendro.hash
|
package com.infendro.hash
|
||||||
|
|
||||||
import com.infendro.hash.util.addAll
|
import com.infendro.bytearray.addAll
|
||||||
import com.infendro.hash.util.toByteArray
|
import com.infendro.bytearray.toByteArray
|
||||||
import com.infendro.hash.util.toUIntArray
|
import com.infendro.bytearray.toUIntArray
|
||||||
|
|
||||||
object SHA1 : HashFunction() {
|
object SHA1 : HashFunction() {
|
||||||
override val bytes: Int
|
override val bytes: Int
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package com.infendro.hash
|
package com.infendro.hash
|
||||||
|
|
||||||
import com.infendro.hash.util.addAll
|
import com.infendro.bytearray.addAll
|
||||||
import com.infendro.hash.util.toByteArray
|
import com.infendro.bytearray.toByteArray
|
||||||
import com.infendro.hash.util.toUIntArray
|
import com.infendro.bytearray.toUIntArray
|
||||||
|
|
||||||
object SHA224 : HashFunction() {
|
object SHA224 : HashFunction() {
|
||||||
override val bytes: Int
|
override val bytes: Int
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package com.infendro.hash
|
package com.infendro.hash
|
||||||
|
|
||||||
import com.infendro.hash.util.addAll
|
import com.infendro.bytearray.addAll
|
||||||
import com.infendro.hash.util.toByteArray
|
import com.infendro.bytearray.toByteArray
|
||||||
import com.infendro.hash.util.toUIntArray
|
import com.infendro.bytearray.toUIntArray
|
||||||
|
|
||||||
object SHA256 : HashFunction() {
|
object SHA256 : HashFunction() {
|
||||||
override val bytes: Int
|
override val bytes: Int
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package com.infendro.hash
|
package com.infendro.hash
|
||||||
|
|
||||||
import com.infendro.hash.util.addAll
|
import com.infendro.bytearray.addAll
|
||||||
import com.infendro.hash.util.toByteArray
|
import com.infendro.bytearray.toByteArray
|
||||||
import com.infendro.hash.util.toULongArray
|
import com.infendro.bytearray.toULongArray
|
||||||
|
|
||||||
object SHA384 : HashFunction() {
|
object SHA384 : HashFunction() {
|
||||||
override val bytes: Int
|
override val bytes: Int
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package com.infendro.hash
|
package com.infendro.hash
|
||||||
|
|
||||||
import com.infendro.hash.util.addAll
|
import com.infendro.bytearray.addAll
|
||||||
import com.infendro.hash.util.toByteArray
|
import com.infendro.bytearray.toByteArray
|
||||||
import com.infendro.hash.util.toULongArray
|
import com.infendro.bytearray.toULongArray
|
||||||
|
|
||||||
object SHA512 : HashFunction() {
|
object SHA512 : HashFunction() {
|
||||||
override val bytes: Int
|
override val bytes: Int
|
||||||
|
|||||||
@@ -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,
|
|
||||||
) {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user