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

com.ackbox.totp.ReseedingSecureRandom.kt Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.ackbox.totp

import java.security.SecureRandom
import java.util.concurrent.atomic.AtomicInteger

class ReseedingSecureRandom {

    private val count = AtomicInteger(0)
    private var secureRandom = createSecureRandom()

    fun nextBytes(bytes: ByteArray) {
        if (count.incrementAndGet() > MAX_OPERATIONS) {
            synchronized(this) {
                if (count.get() > MAX_OPERATIONS) {
                    secureRandom = createSecureRandom()
                    count.set(0)
                }
            }
        }
        secureRandom.nextBytes(bytes)
    }

    private fun createSecureRandom() = SecureRandom()

    companion object {

        private val MAX_OPERATIONS = 1000000
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy