Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix js type exception #14

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions secure-random/src/jsMain/kotlin/org/kotlincrypto/SecureRandom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -46,9 +46,9 @@ public actual class SecureRandom public actual constructor() {
* */
public actual fun nextBytesCopyTo(bytes: ByteArray?) {
bytes.ifNotNullOrEmpty {
try {
val array = unsafeCast<Int8Array>()
val array = Uint8Array(size)

try {
if (isNode) {
crypto.randomFillSync(array)
} else {
Expand All @@ -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
}
}
}

Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}
}

Expand All @@ -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)
}
Loading