37 lines
713 B
Kotlin
37 lines
713 B
Kotlin
package com.infendro.random
|
|
|
|
import com.infendro.bytearray.toInt
|
|
import com.infendro.hash.HashFunction
|
|
import kotlin.random.Random
|
|
|
|
abstract class PRNG : Random() {
|
|
abstract fun seed(
|
|
seed: ByteArray,
|
|
)
|
|
|
|
abstract fun reseed(
|
|
entropy: ByteArray,
|
|
)
|
|
|
|
abstract fun generate(
|
|
count: Int,
|
|
): ByteArray
|
|
|
|
override fun nextBits(
|
|
count: Int,
|
|
): Int {
|
|
if (count !in 0..32) throw Exception()
|
|
if (count == 0) return 0
|
|
|
|
return generate(4).toInt() ushr (32 - count)
|
|
}
|
|
|
|
companion object {
|
|
fun HMAC(
|
|
function: HashFunction,
|
|
): HMAC {
|
|
return com.infendro.random.HMAC(function)
|
|
}
|
|
}
|
|
}
|