Added sha256/512 to native, using posix malloc instead of sodium malloc
This commit is contained in:
parent
197aacac33
commit
6170dc0464
@ -25,6 +25,9 @@ object Versions {
|
||||
|
||||
val kotlinBigNumVersion = "0.1.6-1.4-M2-SNAPSHOT"
|
||||
|
||||
val lazySodium = "4.2.6"
|
||||
val jna = "5.5.0"
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -76,6 +79,11 @@ object Deps {
|
||||
val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Versions.kotlinSerialization}"
|
||||
// val coroutinesTest = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.kotlinCoroutines}"
|
||||
val coroutinesTest = "com.ionspin.kotlin.coroutines:kotlinx-coroutines-test:${Versions.kotlinCoroutines}"
|
||||
|
||||
object Delegated {
|
||||
val lazysodium = "com.goterl.lazycode:lazysodium-java:${Versions.lazySodium}"
|
||||
val jna = "net.java.dev.jna:jna:${Versions.jna}"
|
||||
}
|
||||
}
|
||||
|
||||
object iOs {
|
||||
|
@ -421,6 +421,10 @@ kotlin {
|
||||
implementation(kotlin(Deps.Jvm.test))
|
||||
implementation(kotlin(Deps.Jvm.testJUnit))
|
||||
implementation(Deps.Jvm.coroutinesCore)
|
||||
|
||||
//lazysodium
|
||||
implementation(Deps.Jvm.Delegated.lazysodium)
|
||||
implementation(Deps.Jvm.Delegated.jna)
|
||||
}
|
||||
}
|
||||
val jvmTest by getting {
|
||||
|
@ -24,6 +24,6 @@ package com.ionspin.kotlin.crypto.hash.sha
|
||||
*/
|
||||
|
||||
|
||||
expect class Sha256Delegated(key: UByteArray? = null, hashLength: Int = Sha256Properties.MAX_HASH_BYTES) : Sha256
|
||||
expect class Sha256Delegated() : Sha256
|
||||
|
||||
expect object Sha256StatelessDelegated : StatelessSha256
|
@ -15,7 +15,7 @@ import org.khronos.webgl.get
|
||||
*/
|
||||
|
||||
|
||||
actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha256 {
|
||||
actual class Sha256Delegated actual constructor() : Sha256 {
|
||||
|
||||
val state : dynamic
|
||||
|
||||
|
@ -11,6 +11,7 @@ import java.security.MessageDigest
|
||||
|
||||
|
||||
actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha256 {
|
||||
|
||||
override fun update(data: UByteArray) {
|
||||
TODO("not implemented yet")
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
|
||||
import com.ionspin.kotlin.crypto.util.toHexString
|
||||
import kotlinx.cinterop.*
|
||||
import libsodium.*
|
||||
import platform.posix.free
|
||||
import platform.posix.malloc
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
@ -16,16 +17,10 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
|
||||
val requestedHashLength : Int
|
||||
val state : crypto_generichash_state
|
||||
init {
|
||||
println("Initializing libsodium hash")
|
||||
requestedHashLength = hashLength
|
||||
println("Size ${crypto_generichash_state.size}")
|
||||
println("Align ${crypto_generichash_state.align}")
|
||||
println("Using sodium malloc for state")
|
||||
val allocated = sodium_malloc(crypto_generichash_state.size.convert())!!
|
||||
val allocated = malloc(crypto_generichash_state.size.convert())!!
|
||||
state = allocated.reinterpret<crypto_generichash_state>().pointed
|
||||
println("allocated state")
|
||||
crypto_generichash_init(state.ptr, key?.run { this.toUByteArray().toCValues() }, key?.size?.convert() ?: 0UL.convert(), hashLength.convert())
|
||||
println("Initialized libsodium hash")
|
||||
}
|
||||
|
||||
override fun update(data: UByteArray) {
|
||||
@ -36,7 +31,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
|
||||
val hashResult = UByteArray(requestedHashLength)
|
||||
val hashResultPinned = hashResult.pin()
|
||||
crypto_generichash_final(state.ptr, hashResultPinned.addressOf(0), requestedHashLength.convert())
|
||||
sodium_free(state.ptr)
|
||||
free(state.ptr)
|
||||
return hashResult
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,10 @@
|
||||
package com.ionspin.kotlin.crypto.hash.sha
|
||||
|
||||
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless
|
||||
import kotlinx.cinterop.addressOf
|
||||
import kotlinx.cinterop.convert
|
||||
import kotlinx.cinterop.pin
|
||||
import kotlinx.cinterop.toCValues
|
||||
import libsodium.crypto_hash_sha256
|
||||
import kotlinx.cinterop.*
|
||||
import libsodium.*
|
||||
import platform.posix.free
|
||||
import platform.posix.malloc
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
@ -14,16 +13,28 @@ import libsodium.crypto_hash_sha256
|
||||
*/
|
||||
|
||||
|
||||
actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha256 {
|
||||
actual class Sha256Delegated actual constructor() : Sha256 {
|
||||
|
||||
val state : crypto_hash_sha256_state
|
||||
|
||||
init {
|
||||
val allocated = malloc(crypto_hash_sha256_state.size.convert())!!
|
||||
state = allocated.reinterpret<crypto_hash_sha256_state>().pointed
|
||||
crypto_hash_sha256_init(state.ptr)
|
||||
}
|
||||
|
||||
override fun update(data: UByteArray) {
|
||||
TODO("not implemented yet")
|
||||
crypto_hash_sha256_update(state.ptr, data.toCValues(), data.size.convert())
|
||||
}
|
||||
|
||||
|
||||
|
||||
override fun digest(): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
val hashResult = UByteArray(Sha256Properties.MAX_HASH_BYTES)
|
||||
val hashResultPinned = hashResult.pin()
|
||||
crypto_hash_sha256_final(state.ptr, hashResultPinned.addressOf(0))
|
||||
free(state.ptr)
|
||||
return hashResult
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,5 +1,10 @@
|
||||
package com.ionspin.kotlin.crypto.hash.sha
|
||||
|
||||
import kotlinx.cinterop.*
|
||||
import libsodium.*
|
||||
import platform.posix.free
|
||||
import platform.posix.malloc
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
@ -8,14 +13,26 @@ package com.ionspin.kotlin.crypto.hash.sha
|
||||
|
||||
|
||||
actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha512 {
|
||||
val state : crypto_hash_sha512_state
|
||||
|
||||
init {
|
||||
val allocated = malloc(crypto_hash_sha512_state.size.convert())!!
|
||||
state = allocated.reinterpret<crypto_hash_sha512_state>().pointed
|
||||
crypto_hash_sha512_init(state.ptr)
|
||||
}
|
||||
|
||||
override fun update(data: UByteArray) {
|
||||
TODO("not implemented yet")
|
||||
crypto_hash_sha512_update(state.ptr, data.toCValues(), data.size.convert())
|
||||
}
|
||||
|
||||
|
||||
|
||||
override fun digest(): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
val hashResult = UByteArray(Sha512Properties.MAX_HASH_BYTES)
|
||||
val hashResultPinned = hashResult.pin()
|
||||
crypto_hash_sha512_final(state.ptr, hashResultPinned.addressOf(0))
|
||||
free(state.ptr)
|
||||
return hashResult
|
||||
}
|
||||
|
||||
}
|
||||
@ -23,6 +40,10 @@ actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: In
|
||||
actual object Sha512StatelessDelegated : StatelessSha512 {
|
||||
|
||||
override fun digest(inputMessage: UByteArray): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
val hashResult = UByteArray(Sha512StatelessDelegated.MAX_HASH_BYTES)
|
||||
val hashResultPinned = hashResult.pin()
|
||||
crypto_hash_sha512(hashResultPinned.addressOf(0), inputMessage.toCValues(), inputMessage.size.convert())
|
||||
hashResultPinned.unpin()
|
||||
return hashResult
|
||||
}
|
||||
}
|
@ -35,6 +35,7 @@ class Sha512DelegatedLinuxTest {
|
||||
|
||||
@Test
|
||||
fun testBlake2BStateless() = testBlocking {
|
||||
Blake2bDelegatedStateless.digest("test".encodeToUByteArray())
|
||||
val result = Blake2bDelegatedStateless.digest("test".encodeToUByteArray())
|
||||
println(result.toHexString())
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user