Skip to content

Instantly share code, notes, and snippets.

@eugenehp
Created April 5, 2021 20:44
Show Gist options
  • Save eugenehp/b654e9112719451db3dbfe0b20a060e9 to your computer and use it in GitHub Desktop.
Save eugenehp/b654e9112719451db3dbfe0b20a060e9 to your computer and use it in GitHub Desktop.
react-native-randomness kotlin module
package com.reactnativerandomness
import android.util.Base64
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import java.security.SecureRandom
class RandomnessModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
override fun getName(): String {
return "Randomness"
}
// Example method
// See https://reactnative.dev/docs/native-modules-android
@ReactMethod
fun random(length: Int, promise: Promise) {
val data = ByteArray(length)
val random = SecureRandom()
random.nextBytes(data)
val base64 = Base64.encodeToString(data, Base64.NO_WRAP)
promise.resolve(base64)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment