enable passing hash function to HMAC PRNG

This commit is contained in:
2025-08-03 00:12:34 +02:00
parent a606a62eb4
commit f6f9625cf2
2 changed files with 47 additions and 19 deletions

View File

@@ -1,5 +1,32 @@
package com.infendro.random
object PRNG {
val HMAC = HMAC()
import com.infendro.bytearray.toInt
import com.infendro.hash.HashFunction
import kotlin.random.Random
abstract class PRNG : Random() {
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)
}
}
}