This commit is contained in:
Ugljesa Jovanovic 2020-06-06 00:41:00 +02:00 committed by Ugljesa Jovanovic
parent 3ad86e284a
commit 907128fcfa
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
5 changed files with 76 additions and 28 deletions

View File

@ -22,6 +22,10 @@ object Crypto : CryptoProvider {
Initializer.initialize() Initializer.initialize()
} }
fun initializeWithCallback(done: () -> Unit) {
Initializer.initializeWithCallback(done)
}
object Blake2b { object Blake2b {
fun updateable(): com.ionspin.kotlin.crypto.hash.blake2b.Blake2b { fun updateable(): com.ionspin.kotlin.crypto.hash.blake2b.Blake2b {
@ -29,7 +33,6 @@ object Crypto : CryptoProvider {
} }
fun stateless(message: String): UByteArray { fun stateless(message: String): UByteArray {
println("?")
return Blake2bStateless.digest(message) return Blake2bStateless.digest(message)
} }
} }

View File

@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.hash.blake2b package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.util.toHexString import com.ionspin.kotlin.crypto.util.toHexString
import kotlinx.cinterop.* import kotlinx.cinterop.*
import kotlinx.cinterop.nativeHeap.alloc
import libsodium.* import libsodium.*
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
@ -12,22 +13,39 @@ import libsodium.*
actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: Int) : Blake2b { actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: Int) : Blake2b {
override val MAX_HASH_BYTES: Int = 64 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) { override fun update(data: UByteArray) {
TODO("not implemented yet") crypto_generichash_update(state.ptr, data.toCValues(), data.size.toULong())
} }
override fun update(data: String) { 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 { override fun digest(): UByteArray {
val inputString = "test" val hashResult = UByteArray(requestedHashLength)
val hashLength = 64 val hashResultPinned = hashResult.pin()
val key : String? = null val result = crypto_generichash_final(state.ptr, hashResultPinned.addressOf(0), requestedHashLength.toULong())
val result2 = allocEverything(inputString, key, hashLength) println("HashPointer: ${hashResult.toHexString()}")
val result2String = result2.toHexString() return hashResult
println(result2String) // val inputString = "test"
return ubyteArrayOf(0U) // 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 { 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 { override fun digestString(): String {
TODO("not implemented yet") return digest().toHexString()
} }
} }

View File

@ -6,25 +6,41 @@ package com.ionspin.kotlin.crypto.hash.blake2b
* on 24-May-2020 * on 24-May-2020
*/ */
import com.ionspin.kotlin.crypto.Crypto
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bStateless import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bStateless
import com.ionspin.kotlin.crypto.util.testBlocking import com.ionspin.kotlin.crypto.util.testBlocking
import interop.* import interop.*
import kotlinx.cinterop.* import kotlinx.cinterop.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.runBlocking
import libsodium.* import libsodium.*
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertTrue
class Blake2bLinuxTest { class Blake2bLinuxTest {
@Test @Test
fun testCinterop() { fun testCinterop() {
val sodiumInitResult = sodium_init() runBlocking {
println("Sodium init $sodiumInitResult") Crypto.initialize()
println("1") }
// val sodiumInitResult = sodium_init()
// println("Sodium init $sodiumInitResult")
// println("1")
} }
@Test @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") Blake2bStateless.digest("test")
} }
} }

View File

@ -8,10 +8,21 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
object Sample { object Sample {
suspend fun runSample() { fun runSample() {
Crypto.initialize() println("Initializing crypto library")
Crypto.initializeWithCallback {
blake2b()
}
}
fun blake2b() {
println("Blake2b updateable")
val blake2bUpdateable = Crypto.Blake2b.updateable() val blake2bUpdateable = Crypto.Blake2b.updateable()
blake2bUpdateable.update("test") blake2bUpdateable.update("test")
println(blake2bUpdateable.digest().toHexString()) println(blake2bUpdateable.digest().toHexString())
println("Blake2b stateless")
println("Blake2b stateless: ${Crypto.Blake2b.stateless("test")}")
} }
} }

View File

@ -1,21 +1,21 @@
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegated import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegated
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bStateless import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bStateless
import com.ionspin.kotlin.crypto.sample.Sample import com.ionspin.kotlin.crypto.sample.Sample
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking 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.ExperimentalTime
import kotlin.time.measureTime import kotlin.time.measureTime
@ExperimentalTime @ExperimentalTime
fun main() = runBlocking { fun main() {
Sample.runSample() 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)
} }