Working sodium cinterop

This commit is contained in:
Ugljesa Jovanovic 2020-05-24 20:41:32 +02:00 committed by Ugljesa Jovanovic
parent 3f7df6c651
commit cc2f392bb7
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
6 changed files with 22 additions and 31 deletions

View File

@ -22,6 +22,7 @@ import org.gradle.api.tasks.testing.logging.TestLogging
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
import org.jetbrains.kotlin.konan.library.konanCommonLibraryPath
plugins {
kotlin(PluginsDeps.multiplatform)
@ -92,11 +93,16 @@ kotlin {
compilations.getByName("main") {
val libsodiumCinterop by cinterops.creating {
defFile(project.file("src/nativeInterop/cinterop/libsodium.def"))
// packageName("sodium")
// includeDirs.apply {
// allHeaders("/usr/include/sodium")
// header("/usr/include/sodium.h")
// }
// linkerOpts("-lsodium")
}
}
binaries {
staticLib {
optimized = true
}
}
}
@ -177,7 +183,7 @@ kotlin {
implementation(kotlin(Deps.Common.test))
implementation(Deps.Common.coroutines)
implementation(Deps.Common.kotlinBigNum)
implementation(project(Deps.Common.apiProject))
api(project(Deps.Common.apiProject))
}
}
val commonTest by getting {

View File

@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.hash.blake2b
import interop.*
import kotlinx.cinterop.*
import libsodium.*
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
@ -9,8 +10,7 @@ import kotlinx.cinterop.*
@ExperimentalUnsignedTypes
actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: Int) : Blake2b {
override val MAX_HASH_BYTES: Int
get() = TODO("not implemented yet")
override val MAX_HASH_BYTES: Int = 1024
override fun update(data: UByteArray) {
TODO("not implemented yet")
@ -21,7 +21,9 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
}
override fun digest(): UByteArray {
TODO("not implemented yet")
val result = sodium_init()
println("Sodium init $result")
return ubyteArrayOf(0U)
}
override fun digestString(): String {

View File

@ -17,7 +17,7 @@ class Blake2bLinuxTest {
@Test
fun testCinterop() {
val sodiumInitResult = sodium_init()
// println("Sodium init $sodiumInitResult")
println("Sodium init $sodiumInitResult")
println("1")
}
}

View File

@ -1,6 +1,6 @@
headers = sodium.h
headerFilter = sodium.h sodium/**
staticLibraries = libsodium.a
libraryPaths = /usr/lib
compilerOpts = -Iinclude -I/usr/include
linkerOpts = -L/usr/lib -lsodium
libraryPaths = /home/ionspin/Projects/Future/sodiumWrapper/lib
compilerOpts = -I/home/ionspin/Projects/Future/sodiumWrapper/include
linkerOpts =

View File

@ -107,7 +107,6 @@ kotlin {
executable {
println("Optimized: $optimized")
optimized = true
}
}
}
@ -188,7 +187,7 @@ kotlin {
implementation(kotlin(Deps.Common.test))
implementation(Deps.Common.coroutines)
implementation(Deps.Common.kotlinBigNum)
implementation(project(":multiplatform-crypto"))
implementation(project(":multiplatform-crypto-delegated"))
}
}
val commonTest by getting {

View File

@ -1,5 +1,4 @@
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Pure
import com.ionspin.kotlin.crypto.keyderivation.argon2.ArgonType
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegated
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime
@ -7,22 +6,7 @@ import kotlin.time.measureTime
@ExperimentalStdlibApi
fun main() {
println("Test")
val argon2Instance = Argon2Pure(
password = "Password",
salt = "RandomSalt",
parallelism = 1,
tagLength = 64U,
requestedMemorySize = 4096U,
numberOfIterations = 100,
key = "",
associatedData = "",
argonType = ArgonType.Argon2id
)
val time = measureTime {
val tag = argon2Instance.derive()
val tagString = tag.map { it.toString(16).padStart(2, '0') }.joinToString(separator = "")
val expectedTagString = "27c61b6538ef9f4a1250f8712cac09fc4329969295f9440249437d38c1617a005c2702d76a8a59e4cda2dfba48e1132261dacdfd31296945906992ea32f1d06e"
println("Tag: ${tagString}")
}
println("Time $time")
// Blake
val blake = Blake2bDelegated()
blake.digest()
}