Matt Nelson
01/31/2023, 8:18 PMwasm32
support for a library of mine; secure-random? It's the only platform not supported currently and I've no clue where to start 😢
Issue TicketBig Chungus
01/31/2023, 10:01 PMephemient
01/31/2023, 11:15 PMSvyatoslav Kuzmich [JB]
02/01/2023, 9:27 AMwasm()
target is highly experimental and lacks documentation. But in case you want to try it, you can use @JsFun
to call JS functions. Sharing ByteArray
with JavaScript is not supported at the moment, so your best bet would be returning random Int
multiple times and unpack them into bytes:
@JsFun("""() => {
var array = new Uint32Array(1);
globalThis.crypto.getRandomValues(array);
return array[0];
}""")
external fun secureNextRandomInt(): Int
In upcoming 1.8.20 you’ll be able to use WebAssembly linear memory to reduce number of calls to JavaScript:
@JsFun("""(memoryAddress, sizeInBytes) => {
var array = new Uint8Array(wasmExports.memory.buffer, memoryAddress, sizeInBytes)
globalThis.crypto.getRandomValues(array);
}""")
external fun fillLinearMemoryWithSecureRandomData(memoryAddress: Int, sizeInBytes: Int)
@OptIn(UnsafeWasmMemoryApi::class)
fun fillByteArrayWithSecureRandomData(array: ByteArray) {
withScopedMemoryAllocator { allocator ->
val ptr = allocator.allocate(array.size)
fillLinearMemoryWithSecureRandomData(ptr.address.toInt(), array.size)
for (i in array.indices) {
array[i] = (ptr + i).loadByte()
}
}
}
Running Kotlin in a WASI environment is even more experimental, but in case you are interested, you can find experiments with secure random generation in kowasm projectMatt Nelson
02/01/2023, 1:46 PMSharing👀 https://github.com/05nelsonm/secure-random/blob/504e2ae058ca77c68b85142ef054ab837a[…]rc/jsMain/kotlin/io/matthewnelson/secure/random/SecureRandom.ktwith JavaScript is not supported at the moment ...ByteArray
Svyatoslav Kuzmich [JB]
02/01/2023, 1:49 PMMatt Nelson
02/01/2023, 1:49 PM1.8.20
until I add support for it. Will look into things this week though!