Skip to content

A Kotlin Multiplatform library for obtaining cryptographically secure random data from system sources

License

Notifications You must be signed in to change notification settings

KotlinCrypto/secure-random

Repository files navigation

secure-random

badge-license badge-latest-release

badge-kotlin

badge-platform-android badge-platform-jvm badge-platform-js badge-platform-js-node badge-platform-wasm badge-platform-linux badge-platform-macos badge-platform-ios badge-platform-tvos badge-platform-watchos badge-platform-windows badge-support-android-native badge-support-apple-silicon badge-support-js-ir badge-support-linux-arm

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.

Example Usages

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())
}

Samples

See the sample

Get Started

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"
}