Kotlin Multiplatform library for obtaining cryptographically secure random data from the system.
NOTE: For Jvm, SecureRandom
extends java.security.SecureRandom
for interoperability.
The Linux/AndroidNative implementation was heavily inspired by
rust-random/getrandom.
fun main() {
val sRandom = SecureRandom()
val bytes: ByteArray = try {
sRandom.nextBytesOf(count = 20)
} catch (e: SecRandomCopyException) {
e.printStackTrace()
return
}
println(bytes.toList())
}
fun main() {
val sRandom = SecureRandom()
val bytes = ByteArray(20)
try {
sRandom.nextBytesCopyTo(bytes)
} catch (e: SecRandomCopyException) {
e.printStackTrace()
return
}
println(bytes.toList())
}
See the sample
The best way to keep KotlinCrypto
dependencies up to date is by using the
version-catalog. Alternatively, see below.
// build.gradle.kts
dependencies {
implementation("org.kotlincrypto:secure-random:0.3.2")
}
// build.gradle
dependencies {
implementation "org.kotlincrypto:secure-random:0.3.2"
}