Skip to content

Commit 64f0f73

Browse files
committed
Use Uint8Array when obtaining bytes
1 parent 72f5234 commit 64f0f73

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

secure-random/src/jsMain/kotlin/org/kotlincrypto/SecureRandom.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.kotlincrypto
1919

20-
import org.khronos.webgl.Int8Array
20+
import org.khronos.webgl.Uint8Array
2121
import org.kotlincrypto.internal.commonNextBytesOf
2222
import org.kotlincrypto.internal.ifNotNullOrEmpty
2323

@@ -46,9 +46,9 @@ public actual class SecureRandom public actual constructor() {
4646
* */
4747
public actual fun nextBytesCopyTo(bytes: ByteArray?) {
4848
bytes.ifNotNullOrEmpty {
49-
try {
50-
val array = unsafeCast<Int8Array>()
49+
val array = Uint8Array(size)
5150

51+
try {
5252
if (isNode) {
5353
crypto.randomFillSync(array)
5454
} else {
@@ -62,6 +62,12 @@ public actual class SecureRandom public actual constructor() {
6262
} catch (t: Throwable) {
6363
throw SecRandomCopyException("Failed to obtain bytes", t)
6464
}
65+
66+
val ad = array.asDynamic()
67+
for (i in indices) {
68+
this[i] = (ad[i] as Number).toByte()
69+
ad[i] = 0
70+
}
6571
}
6672
}
6773

@@ -90,7 +96,7 @@ private fun isNodeJs(): Boolean = js(
9096

9197
private external class Crypto {
9298
// Browser
93-
fun getRandomValues(array: Int8Array)
99+
fun getRandomValues(array: Uint8Array)
94100
// Node.js
95-
fun randomFillSync(array: Int8Array)
101+
fun randomFillSync(buf: Uint8Array)
96102
}

secure-random/src/wasmJsMain/kotlin/org/kotlincrypto/SecureRandom.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.kotlincrypto
1919

20-
import org.khronos.webgl.Int8Array
20+
import org.khronos.webgl.Uint8Array
2121
import org.khronos.webgl.get
2222
import org.khronos.webgl.set
2323
import org.kotlincrypto.internal.commonNextBytesOf
@@ -48,9 +48,9 @@ public actual class SecureRandom public actual constructor() {
4848
* */
4949
public actual fun nextBytesCopyTo(bytes: ByteArray?) {
5050
bytes.ifNotNullOrEmpty {
51-
try {
52-
val array = Int8Array(size)
51+
val array = Uint8Array(size)
5352

53+
try {
5454
if (isNode) {
5555
crypto.randomFillSync(array)
5656
} else {
@@ -61,14 +61,14 @@ public actual class SecureRandom public actual constructor() {
6161
offset += len
6262
}
6363
}
64-
65-
for (i in indices) {
66-
this[i] = array[i]
67-
array[i] = 0
68-
}
6964
} catch (t: Throwable) {
7065
throw SecRandomCopyException("Failed to obtain bytes", t)
7166
}
67+
68+
for (i in indices) {
69+
this[i] = array[i]
70+
array[i] = 0
71+
}
7272
}
7373
}
7474

@@ -95,7 +95,7 @@ private fun isNodeJs(): Boolean = js(
9595

9696
private external class Crypto: JsAny {
9797
// Browser
98-
fun getRandomValues(array: Int8Array)
98+
fun getRandomValues(array: Uint8Array)
9999
// Node.js
100-
fun randomFillSync(array: Int8Array)
100+
fun randomFillSync(buf: Uint8Array)
101101
}

0 commit comments

Comments
 (0)