KotlinCrypto / secure-random

A Kotlin Multiplatform library for obtaining cryptographically secure random data from system sources
Apache License 2.0
25 stars 3 forks source link

Extend ```kotlin.random.Random``` #3

Closed DatL4g closed 1 year ago

DatL4g commented 1 year ago

The SecureRandom class should extend kotlin.random.Random so we can use it when something wants a Kotlin Random object

05nelsonm commented 1 year ago

I thought about that too when first implementing it, but there were a few reasons it does not.

  1. Uncaught exceptions
    • Unfortunately the native implementations all have a chance of throwing exception when trying to procure securely random bytes from the system (i.e. SecRandomCopyException). Functions for kotlin.random.Random do not annotate this exception, so if someone were to use org.kotlincrypto.SecureRandom as an argument to something that accepts kotlin.random.Random, that's an uncaught exception for whatever that something is going to do with it.
  2. JVM Interoperability
    • The JVM implementation extends java.security.SecureRandom. The JVM implementation of org.kotlincrypto.SecureRandom can only extend one or the other. I chose the latter for interoperability purposes with JVM.

IMO, any function or class that is denoting kotlin.random.Random as an argument does not need a cryptographically secure source of random data. The default implementation kotlin.random.Random.Default should suffice. If there is a crypto lib that is accepting kotlin.random.Random as an argument, one should immediately stop using that library.