Working SRNG implementation for nodeJs and browser js
This commit is contained in:
parent
7e905bd948
commit
cb7ca575ab
@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
import com.moowork.gradle.node.task.NodeTask
|
import com.moowork.gradle.node.task.NodeTask
|
||||||
import org.gradle.api.tasks.testing.logging.TestLogging
|
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.gradle.tasks.Kotlin2JsCompile
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
@ -63,13 +65,21 @@ kotlin {
|
|||||||
println("Destination dir ${it.compileKotlinTask.destinationDir}")
|
println("Destination dir ${it.compileKotlinTask.destinationDir}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nodejs() {
|
browser {
|
||||||
|
testTask {
|
||||||
|
useKarma {
|
||||||
|
useChrome()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nodejs {
|
||||||
testTask {
|
testTask {
|
||||||
useMocha() {
|
useMocha() {
|
||||||
timeout = "10s"
|
timeout = "10s"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
linuxX64("linux") {
|
linuxX64("linux") {
|
||||||
binaries {
|
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 {
|
signing {
|
||||||
isRequired = false
|
isRequired = false
|
||||||
sign(publishing.publications)
|
sign(publishing.publications)
|
||||||
|
@ -25,12 +25,27 @@ actual object SRNG {
|
|||||||
var counter = 0
|
var counter = 0
|
||||||
@ExperimentalUnsignedTypes
|
@ExperimentalUnsignedTypes
|
||||||
actual fun getRandomBytes(amount: Int): Array<UByte> {
|
actual fun getRandomBytes(amount: Int): Array<UByte> {
|
||||||
// val runningOnNode = js("(typeof window === 'undefined')").unsafeCast<Boolean>()
|
val runningOnNode = js(
|
||||||
// if (runningOnNode) {
|
"if (typeof window === 'undefined') {\n" +
|
||||||
// js("var crypto = require('crypto')").asDynamic().randomBytes(amount)
|
" true;\n" +
|
||||||
// } else {
|
" } else {\n" +
|
||||||
// throw RuntimeException("Secure random not supported yet for non-nodejs environment")
|
" false;\n" +
|
||||||
// }
|
" }"
|
||||||
return Array<UByte>(amount) { (counter++).toUByte() } // TODO Wow. Such random. Very entropy.
|
)
|
||||||
|
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<UByte>
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user