Sorted out native sourcesets, using intialization and sodium srng in js and native

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-05-28 22:41:05 +02:00
1 parent ccbb8ebe5b
commit 63c5db199e
12 files changed
+208 -175

No files matched your search

@@ -0,0 +1,39 @@
/*
* Copyright 2019 Ugljesa Jovanovic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.util.testBlocking
import kotlin.test.Test
import kotlin.test.assertTrue
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 21-Sep-2019
*/
class SRNGTest {
@Test
fun testSrng() = testBlocking {
Crypto.initialize()
//Just a sanity test, need to add better srng tests.
val randomBytes1 = SRNG.getRandomBytes(10)
val randomBytes2 = SRNG.getRandomBytes(10)
randomBytes1.forEach { println("RB1: $it")}
randomBytes2.forEach { println("RB2: $it")}
assertTrue { !randomBytes1.contentEquals(randomBytes2) }
}
}
@@ -1,10 +0,0 @@
package com.ionspin.kotlin.crypto
actual object Initializer {
actual suspend fun initialize() {
// sodi
}
actual fun initializeWithCallback(done: () -> Unit) {
}
}
@@ -0,0 +1,19 @@
package com.ionspin.kotlin.crypto
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import libsodium.sodium_init
actual object Initializer {
actual suspend fun initialize() {
GlobalScope.launch {
sodium_init()
}
}
actual fun initializeWithCallback(done: () -> Unit) {
sodium_init()
done()
}
}
@@ -17,6 +17,7 @@
package com.ionspin.kotlin.crypto
import kotlinx.cinterop.*
import libsodium.randombytes_buf
import platform.posix.*
//import libsod
@@ -30,7 +31,7 @@ actual object SRNG {
actual fun getRandomBytes(amount: Int): UByteArray {
memScoped {
val array = allocArray<UByteVar>(amount)
// randombytes_buf()
randombytes_buf(array, amount.toULong())
return UByteArray(amount) {
array[it]
}
@@ -1,6 +1,5 @@
package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.util.toHexString
import interop.*
import kotlinx.cinterop.*
import libsodium.*
/**
@@ -6,6 +6,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
* on 24-May-2020
*/
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bStateless
import com.ionspin.kotlin.crypto.util.testBlocking
import interop.*
import kotlinx.cinterop.*