File tree Expand file tree Collapse file tree
jsMain/kotlin/org/kotlincrypto
wasmJsMain/kotlin/org/kotlincrypto Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1717
1818package org.kotlincrypto
1919
20- import org.khronos.webgl.Int8Array
20+ import org.khronos.webgl.Uint8Array
2121import org.kotlincrypto.internal.commonNextBytesOf
2222import 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
9197private 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}
Original file line number Diff line number Diff line change 1717
1818package org.kotlincrypto
1919
20- import org.khronos.webgl.Int8Array
20+ import org.khronos.webgl.Uint8Array
2121import org.khronos.webgl.get
2222import org.khronos.webgl.set
2323import 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
9696private 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}
You can’t perform that action at this time.
0 commit comments