optimize nextBytes()
This commit is contained in:
@@ -35,7 +35,6 @@ class HMAC internal constructor(
|
||||
val bytes = buildList {
|
||||
while (size < count) {
|
||||
v = hmac.hash(k, v)
|
||||
|
||||
addAll(v.take(min(count - size, function.bytes)))
|
||||
}
|
||||
}.toUByteArray()
|
||||
|
||||
@@ -26,7 +26,6 @@ class LCG internal constructor() : PRNG() {
|
||||
return buildList {
|
||||
while (size < count) {
|
||||
x = (a * x + c) % m
|
||||
|
||||
addAll(x.toUByteArray().takeLast(min(count - size, 6)))
|
||||
}
|
||||
}.toUByteArray()
|
||||
|
||||
@@ -15,12 +15,33 @@ abstract class PRNG : Random() {
|
||||
override fun nextBits(
|
||||
bitCount: Int,
|
||||
): Int {
|
||||
if (bitCount !in 0..32) throw Exception()
|
||||
if (bitCount == 0) return 0
|
||||
if (bitCount !in 0..32)
|
||||
throw IllegalArgumentException()
|
||||
if (bitCount == 0)
|
||||
return 0
|
||||
|
||||
return generate(4).toInt() ushr (32 - bitCount)
|
||||
}
|
||||
|
||||
override fun nextBytes(
|
||||
array: ByteArray,
|
||||
fromIndex: Int,
|
||||
toIndex: Int,
|
||||
): ByteArray {
|
||||
if (fromIndex !in 0..array.size || toIndex !in 0..array.size)
|
||||
throw IllegalArgumentException()
|
||||
if (fromIndex > toIndex)
|
||||
throw IllegalArgumentException()
|
||||
|
||||
val size = toIndex - fromIndex
|
||||
val bytes = generate(size).asByteArray()
|
||||
for (i in 0..<size) {
|
||||
array[i + fromIndex] = bytes[i]
|
||||
}
|
||||
|
||||
return array
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun LCG() = com.infendro.random.prng.LCG()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user