This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-06-06 00:41:00 +02:00
1 parent 3ad86e284a
commit 907128fcfa
5 files changed
+76 -28

No files matched your search

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