refactor, implement LCG

LCG ... Linear Congruential Generator
This commit is contained in:
2025-11-10 18:52:19 +01:00
parent 7db8972e27
commit feecfa49b9
7 changed files with 109 additions and 69 deletions

View File

@@ -0,0 +1,16 @@
package com.infendro.random.csprng
import com.infendro.hash.HashFunction
import com.infendro.random.prng.PRNG
abstract class CSPRNG : PRNG() {
abstract fun reseed(
entropy: UByteArray,
)
companion object {
fun HMAC(
function: HashFunction,
) = com.infendro.random.csprng.HMAC(function)
}
}

View File

@@ -0,0 +1,48 @@
package com.infendro.random.csprng
import com.infendro.hash.HashFunction
import kotlin.math.min
import kotlin.properties.Delegates.notNull
class HMAC internal constructor(
private val function: HashFunction,
) : CSPRNG() {
private val hmac = com.infendro.mac.HMAC(function)
private var k: UByteArray by notNull()
private var v: UByteArray by notNull()
override fun seed(
seed: UByteArray,
) {
k = UByteArray(function.bytes) { 0x00U }
v = UByteArray(function.bytes) { 0x01U }
reseed(seed)
}
override fun reseed(
entropy: UByteArray,
) {
k = hmac.hash(k, v + ubyteArrayOf(0x00U) + entropy)
v = hmac.hash(k, v)
k = hmac.hash(k, v + ubyteArrayOf(0x01U) + entropy)
v = hmac.hash(k, v)
}
override fun generate(
count: Int,
): UByteArray {
val bytes = buildList {
while (size < count) {
v = hmac.hash(k, v)
addAll(v.take(min(count - size, function.bytes)))
}
}.toUByteArray()
k = hmac.hash(k, v + ubyteArrayOf(0x00U))
v = hmac.hash(k, v)
return bytes
}
}