From 64f0f733cf16e86cdb93c2a99311f3738697b27f Mon Sep 17 00:00:00 2001 From: Matthew Nelson Date: Tue, 19 Mar 2024 08:18:29 -0400 Subject: [PATCH] Use Uint8Array when obtaining bytes --- .../kotlin/org/kotlincrypto/SecureRandom.kt | 16 ++++++++++----- .../kotlin/org/kotlincrypto/SecureRandom.kt | 20 +++++++++---------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/secure-random/src/jsMain/kotlin/org/kotlincrypto/SecureRandom.kt b/secure-random/src/jsMain/kotlin/org/kotlincrypto/SecureRandom.kt index 3436022..107c46b 100644 --- a/secure-random/src/jsMain/kotlin/org/kotlincrypto/SecureRandom.kt +++ b/secure-random/src/jsMain/kotlin/org/kotlincrypto/SecureRandom.kt @@ -17,7 +17,7 @@ package org.kotlincrypto -import org.khronos.webgl.Int8Array +import org.khronos.webgl.Uint8Array import org.kotlincrypto.internal.commonNextBytesOf import org.kotlincrypto.internal.ifNotNullOrEmpty @@ -46,9 +46,9 @@ public actual class SecureRandom public actual constructor() { * */ public actual fun nextBytesCopyTo(bytes: ByteArray?) { bytes.ifNotNullOrEmpty { - try { - val array = unsafeCast() + val array = Uint8Array(size) + try { if (isNode) { crypto.randomFillSync(array) } else { @@ -62,6 +62,12 @@ public actual class SecureRandom public actual constructor() { } catch (t: Throwable) { throw SecRandomCopyException("Failed to obtain bytes", t) } + + val ad = array.asDynamic() + for (i in indices) { + this[i] = (ad[i] as Number).toByte() + ad[i] = 0 + } } } @@ -90,7 +96,7 @@ private fun isNodeJs(): Boolean = js( private external class Crypto { // Browser - fun getRandomValues(array: Int8Array) + fun getRandomValues(array: Uint8Array) // Node.js - fun randomFillSync(array: Int8Array) + fun randomFillSync(buf: Uint8Array) } diff --git a/secure-random/src/wasmJsMain/kotlin/org/kotlincrypto/SecureRandom.kt b/secure-random/src/wasmJsMain/kotlin/org/kotlincrypto/SecureRandom.kt index 771f497..0f07eb9 100644 --- a/secure-random/src/wasmJsMain/kotlin/org/kotlincrypto/SecureRandom.kt +++ b/secure-random/src/wasmJsMain/kotlin/org/kotlincrypto/SecureRandom.kt @@ -17,7 +17,7 @@ package org.kotlincrypto -import org.khronos.webgl.Int8Array +import org.khronos.webgl.Uint8Array import org.khronos.webgl.get import org.khronos.webgl.set import org.kotlincrypto.internal.commonNextBytesOf @@ -48,9 +48,9 @@ public actual class SecureRandom public actual constructor() { * */ public actual fun nextBytesCopyTo(bytes: ByteArray?) { bytes.ifNotNullOrEmpty { - try { - val array = Int8Array(size) + val array = Uint8Array(size) + try { if (isNode) { crypto.randomFillSync(array) } else { @@ -61,14 +61,14 @@ public actual class SecureRandom public actual constructor() { offset += len } } - - for (i in indices) { - this[i] = array[i] - array[i] = 0 - } } catch (t: Throwable) { throw SecRandomCopyException("Failed to obtain bytes", t) } + + for (i in indices) { + this[i] = array[i] + array[i] = 0 + } } } @@ -95,7 +95,7 @@ private fun isNodeJs(): Boolean = js( private external class Crypto: JsAny { // Browser - fun getRandomValues(array: Int8Array) + fun getRandomValues(array: Uint8Array) // Node.js - fun randomFillSync(array: Int8Array) + fun randomFillSync(buf: Uint8Array) }