diff --git a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt index 1813fd1..d8e76bf 100644 --- a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt +++ b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt @@ -21,7 +21,11 @@ object Crypto : CryptoProvider { override suspend fun initialize() { Initializer.initialize() } - + + fun initializeWithCallback(done: () -> Unit) { + Initializer.initializeWithCallback(done) + } + object Blake2b { fun updateable(): com.ionspin.kotlin.crypto.hash.blake2b.Blake2b { @@ -29,7 +33,6 @@ object Crypto : CryptoProvider { } fun stateless(message: String): UByteArray { - println("?") return Blake2bStateless.digest(message) } } diff --git a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt index 7394336..ad36182 100644 --- a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt +++ b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt @@ -1,6 +1,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b import com.ionspin.kotlin.crypto.util.toHexString import kotlinx.cinterop.* +import kotlinx.cinterop.nativeHeap.alloc import libsodium.* /** * Created by Ugljesa Jovanovic @@ -12,22 +13,39 @@ import libsodium.* actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: Int) : Blake2b { override val MAX_HASH_BYTES: Int = 64 + val requestedHashLength : Int + val state : crypto_generichash_state + init { + println("Initializing libsodium hash") + requestedHashLength = hashLength + state = nativeHeap.alloc() + println("allocated state") + crypto_generichash_init(state.ptr, key?.run { this.toUByteArray().toCValues() }, key?.size?.toULong() ?: 0UL, hashLength.toULong()) + println("Initialized libsodium hash") + } + override fun update(data: UByteArray) { - TODO("not implemented yet") + crypto_generichash_update(state.ptr, data.toCValues(), data.size.toULong()) } override fun update(data: String) { - TODO("not implemented yet") + val ubyteArray = data.encodeToByteArray().toUByteArray() + crypto_generichash_update(state.ptr, ubyteArray.toCValues(), ubyteArray.size.toULong()) } override fun digest(): UByteArray { - val inputString = "test" - val hashLength = 64 - val key : String? = null - val result2 = allocEverything(inputString, key, hashLength) - val result2String = result2.toHexString() - println(result2String) - return ubyteArrayOf(0U) + val hashResult = UByteArray(requestedHashLength) + val hashResultPinned = hashResult.pin() + val result = crypto_generichash_final(state.ptr, hashResultPinned.addressOf(0), requestedHashLength.toULong()) + println("HashPointer: ${hashResult.toHexString()}") + return hashResult +// val inputString = "test" +// val hashLength = 64 +// val key : String? = null +// val result2 = allocEverything(inputString, key, hashLength) +// val result2String = result2.toHexString() +// println(result2String) +// return ubyteArrayOf(0U) } fun allocEverything(inputString: String, key: String?, hashLength: Int) : UByteArray { @@ -51,7 +69,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I } override fun digestString(): String { - TODO("not implemented yet") + return digest().toHexString() } } diff --git a/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt b/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt index 9a71168..519939c 100644 --- a/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt +++ b/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt @@ -6,25 +6,41 @@ package com.ionspin.kotlin.crypto.hash.blake2b * on 24-May-2020 */ +import com.ionspin.kotlin.crypto.Crypto import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bStateless import com.ionspin.kotlin.crypto.util.testBlocking import interop.* import kotlinx.cinterop.* +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.runBlocking import libsodium.* import kotlin.test.Test +import kotlin.test.assertTrue class Blake2bLinuxTest { @Test fun testCinterop() { - val sodiumInitResult = sodium_init() - println("Sodium init $sodiumInitResult") - println("1") + runBlocking { + Crypto.initialize() + } +// val sodiumInitResult = sodium_init() +// println("Sodium init $sodiumInitResult") +// println("1") } @Test - fun testBlake2BSodiumInterop() = testBlocking { + fun testBlake2bUpdateable() = testBlocking { + val blake2b = Crypto.Blake2b.updateable() + blake2b.update("test") + val result = blake2b.digestString() + println(result) + assertTrue { result.length > 2 } + } + + @Test + fun testBlake2BStateless() = testBlocking { Blake2bStateless.digest("test") } } \ No newline at end of file diff --git a/sample/src/commonMain/kotlin/com/ionspin/kotlin/crypto/sample/Sample.kt b/sample/src/commonMain/kotlin/com/ionspin/kotlin/crypto/sample/Sample.kt index 7ecfd59..c22c2c3 100644 --- a/sample/src/commonMain/kotlin/com/ionspin/kotlin/crypto/sample/Sample.kt +++ b/sample/src/commonMain/kotlin/com/ionspin/kotlin/crypto/sample/Sample.kt @@ -8,10 +8,21 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch object Sample { - suspend fun runSample() { - Crypto.initialize() + fun runSample() { + println("Initializing crypto library") + Crypto.initializeWithCallback { + blake2b() + } + + + } + + fun blake2b() { + println("Blake2b updateable") val blake2bUpdateable = Crypto.Blake2b.updateable() blake2bUpdateable.update("test") println(blake2bUpdateable.digest().toHexString()) + println("Blake2b stateless") + println("Blake2b stateless: ${Crypto.Blake2b.stateless("test")}") } } \ No newline at end of file diff --git a/sample/src/linuxArm64Main/kotlin/com/ionspin/kotlin/crypto/sample/Runner.kt b/sample/src/linuxArm64Main/kotlin/com/ionspin/kotlin/crypto/sample/Runner.kt index b060b83..faab8b3 100644 --- a/sample/src/linuxArm64Main/kotlin/com/ionspin/kotlin/crypto/sample/Runner.kt +++ b/sample/src/linuxArm64Main/kotlin/com/ionspin/kotlin/crypto/sample/Runner.kt @@ -1,21 +1,21 @@ import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegated import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bStateless import com.ionspin.kotlin.crypto.sample.Sample +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking +import platform.posix.pthread_self +import platform.posix.sleep +import kotlin.native.concurrent.TransferMode +import kotlin.native.concurrent.Worker import kotlin.time.ExperimentalTime import kotlin.time.measureTime @ExperimentalTime -fun main() = runBlocking { +fun main() { + Sample.runSample() -// println("Test") -//// Blake -// val blake = Blake2bDelegated() -// val res = blake.digest() -// println("Result of res") -//// println(res) -// val staticRes = Blake2bStateless.digest("test") -// println("Result:") -// println(staticRes) + }