diff --git a/multiplatform-crypto/build.gradle.kts b/multiplatform-crypto/build.gradle.kts index ea519f3..e7749e9 100644 --- a/multiplatform-crypto/build.gradle.kts +++ b/multiplatform-crypto/build.gradle.kts @@ -19,6 +19,8 @@ import com.moowork.gradle.node.task.NodeTask 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 plugins { @@ -63,13 +65,21 @@ kotlin { println("Destination dir ${it.compileKotlinTask.destinationDir}") } } - nodejs() { + browser { + testTask { + useKarma { + useChrome() + } + } + } + nodejs { testTask { useMocha() { timeout = "10s" } } } + } linuxX64("linux") { binaries { @@ -304,13 +314,34 @@ tasks { } } + val linuxTest by getting(KotlinNativeTest::class) { + + testLogging { + events("PASSED", "FAILED", "SKIPPED") + // showStandardStreams = true + } + } + + val jsNodeTest by getting(KotlinJsTest::class) { + + testLogging { + events("PASSED", "FAILED", "SKIPPED") + showStandardStreams = true + } + } + + val jsBrowserTest by getting(KotlinJsTest::class) { + + testLogging { + events("PASSED", "FAILED", "SKIPPED") + showStandardStreams = true + } + } + } - - - signing { isRequired = false sign(publishing.publications) diff --git a/multiplatform-crypto/src/jsMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder b/multiplatform-crypto/src/jsMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/multiplatform-crypto/src/jsMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt b/multiplatform-crypto/src/jsMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt index 0a7a85c..e7d7e7d 100644 --- a/multiplatform-crypto/src/jsMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt +++ b/multiplatform-crypto/src/jsMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt @@ -25,12 +25,27 @@ actual object SRNG { var counter = 0 @ExperimentalUnsignedTypes actual fun getRandomBytes(amount: Int): Array { -// val runningOnNode = js("(typeof window === 'undefined')").unsafeCast() -// if (runningOnNode) { -// js("var crypto = require('crypto')").asDynamic().randomBytes(amount) -// } else { -// throw RuntimeException("Secure random not supported yet for non-nodejs environment") -// } - return Array(amount) { (counter++).toUByte() } // TODO Wow. Such random. Very entropy. + val runningOnNode = js( + "if (typeof window === 'undefined') {\n" + + " true;\n" + + " } else {\n" + + " false;\n" + + " }" + ) + val randomBytes = if (runningOnNode) { + js("require('crypto')").randomBytes(amount).toJSON().data + } else { + js( + """ + var randomArray = new Uint8Array(amount); + var crypto = (self.crypto || self.msCrypto); + crypto.getRandomValues(randomArray); + """ + ) + var randomArrayResult = js("Array.prototype.slice.call(randomArray);") + randomArrayResult + } + + return randomBytes as Array } } \ No newline at end of file diff --git a/multiplatform-crypto/src/jsTest/kotlin/com/ionspin/kotlin/crypto/SRNGJsTest.kt b/multiplatform-crypto/src/jsTest/kotlin/com/ionspin/kotlin/crypto/SRNGJsTest.kt new file mode 100644 index 0000000..bfc7588 --- /dev/null +++ b/multiplatform-crypto/src/jsTest/kotlin/com/ionspin/kotlin/crypto/SRNGJsTest.kt @@ -0,0 +1,40 @@ +/* + * 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 kotlin.test.Test +import kotlin.test.assertTrue + +/** + * Created by Ugljesa Jovanovic + * ugljesa.jovanovic@ionspin.com + * on 05-Jan-2020 + */ +class SRNGJsTest { + + @Test + fun testJsSrng() { + val bytes1 = SRNG.getRandomBytes(10) + val bytes2 = SRNG.getRandomBytes(10) + assertTrue { + !bytes1.contentEquals(bytes2) && + bytes1.size == 10 && + bytes2.size == 10 + } + + } +} \ No newline at end of file