implement seed
This commit is contained in:
@@ -4,20 +4,24 @@ import com.infendro.bytearray.addAll
|
|||||||
import com.infendro.hash.HashFunction
|
import com.infendro.hash.HashFunction
|
||||||
|
|
||||||
class HMAC internal constructor(
|
class HMAC internal constructor(
|
||||||
function: HashFunction,
|
private val function: HashFunction,
|
||||||
) : PRNG() {
|
) : PRNG() {
|
||||||
private val random = Default
|
private val random = Default
|
||||||
private val hmac = com.infendro.mac.HMAC(function)
|
private val hmac = com.infendro.mac.HMAC(function)
|
||||||
private var K: ByteArray
|
private lateinit var K: ByteArray
|
||||||
private var V: ByteArray
|
private lateinit var V: ByteArray
|
||||||
|
|
||||||
init {
|
init {
|
||||||
K = ByteArray(function.bytes).also {
|
val bytes = random.nextBytes(function.bytes)
|
||||||
random.nextBytes(it)
|
seed(bytes)
|
||||||
}
|
|
||||||
V = ByteArray(function.bytes).also {
|
|
||||||
random.nextBytes(it)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun seed(
|
||||||
|
seed: ByteArray,
|
||||||
|
) {
|
||||||
|
K = ByteArray(function.bytes) { 0x00 }
|
||||||
|
V = ByteArray(function.bytes) { 0x01 }
|
||||||
|
reseed(seed)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun reseed(
|
override fun reseed(
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ import com.infendro.hash.HashFunction
|
|||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
|
|
||||||
abstract class PRNG : Random() {
|
abstract class PRNG : Random() {
|
||||||
|
abstract fun seed(
|
||||||
|
seed: ByteArray,
|
||||||
|
)
|
||||||
|
|
||||||
abstract fun reseed(
|
abstract fun reseed(
|
||||||
entropy: ByteArray,
|
entropy: ByteArray,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user