All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.fr.acinq.lightning.utils.SecureRandomJvm.kt Maven / Gradle / Ivy

There is a newer version: 1.8.4
Show newest version
package fr.acinq.lightning.utils

import java.security.SecureRandom
import kotlin.random.Random

class SecureRandomJvm : Random() {

    private val storage = object : ThreadLocal() {
        override fun initialValue(): SecureRandom {
            return SecureRandom()
        }
    }

    private val random: SecureRandom get() = storage.get()

    override fun nextBits(bitCount: Int): Int {
        val int = random.nextInt()
        return (int ushr (32 - bitCount)) and (-bitCount shr 31)
    }

    override fun nextInt(): Int = random.nextInt()
    override fun nextInt(until: Int): Int = random.nextInt(until)
    override fun nextLong(): Long = random.nextLong()
    override fun nextBoolean(): Boolean = random.nextBoolean()
    override fun nextDouble(): Double = random.nextDouble()
    override fun nextFloat(): Float = random.nextFloat()
    override fun nextBytes(array: ByteArray): ByteArray = array.also { random.nextBytes(it) }
}

actual fun Random.Default.secure(): Random = SecureRandomJvm()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy