Compare commits
2 Commits
3effa09602
...
20d031fba6
| Author | SHA1 | Date | |
|---|---|---|---|
|
20d031fba6
|
|||
|
d4be1689e2
|
35
README.md
35
README.md
@@ -1,18 +1,21 @@
|
|||||||
# Kotlin OTP
|
# prng-kt
|
||||||
|
|
||||||
This library provides various [Pseudorandom Number Generator](https://en.wikipedia.org/wiki/Pseudorandom_number_generator)
|
`prng-kt` is a Kotlin Multiplatform library that provides common pseudorandom number generators.
|
||||||
implementations in Kotlin Multiplatform.
|
|
||||||
|
|
||||||
## Features
|
Supported hash functions:
|
||||||
|
- PRNG
|
||||||
|
- LCG
|
||||||
|
- CSPRNG
|
||||||
|
- HMAC DRBG
|
||||||
|
|
||||||
* Generate random numbers using Kotlin's Random interface.
|
## Targets
|
||||||
* Seed the PRNGs to receive predictable results.
|
|
||||||
* Reseed the PRNGs to add entropy to the following results.
|
| Target | Status |
|
||||||
* [HMAC DRBG](https://en.wikipedia.org/wiki/NIST_SP_800-90A#Hash_DRBG_and_HMAC_DRBG)
|
|------------------|---------------|
|
||||||
* Multiplatform support
|
| JVM | Supported |
|
||||||
* JVM
|
| JavaScript | Supported |
|
||||||
* JavaScript
|
| Native (Linux) | Supported |
|
||||||
* Native (Linux)
|
| Native (Windows) | Not Supported |
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -24,19 +27,19 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("com.infendro:random:1.1.0")
|
implementation("com.infendro:prng:1.2.0")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
import com.infendro.hash.SHA256
|
import com.infendro.hash.sha2.`SHA-256`
|
||||||
import com.infendro.random.PRNG
|
import com.infendro.random.csprng.CSPRNG
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
// create a PRNG instance
|
// create a PRNG instance
|
||||||
val rng = PRNG.HMAC(SHA256)
|
val rng = CSPRNG.HMAC(`SHA-256`)
|
||||||
|
|
||||||
// seed the PRNG to receive predictable results
|
// seed the PRNG to receive predictable results
|
||||||
rng.seed(/* some seed */)
|
rng.seed(/* some seed */)
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ class HMAC internal constructor(
|
|||||||
val bytes = buildList {
|
val bytes = buildList {
|
||||||
while (size < count) {
|
while (size < count) {
|
||||||
v = hmac.hash(k, v)
|
v = hmac.hash(k, v)
|
||||||
|
|
||||||
addAll(v.take(min(count - size, function.bytes)))
|
addAll(v.take(min(count - size, function.bytes)))
|
||||||
}
|
}
|
||||||
}.toUByteArray()
|
}.toUByteArray()
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ class LCG internal constructor() : PRNG() {
|
|||||||
return buildList {
|
return buildList {
|
||||||
while (size < count) {
|
while (size < count) {
|
||||||
x = (a * x + c) % m
|
x = (a * x + c) % m
|
||||||
|
|
||||||
addAll(x.toUByteArray().takeLast(min(count - size, 6)))
|
addAll(x.toUByteArray().takeLast(min(count - size, 6)))
|
||||||
}
|
}
|
||||||
}.toUByteArray()
|
}.toUByteArray()
|
||||||
|
|||||||
@@ -15,12 +15,33 @@ abstract class PRNG : Random() {
|
|||||||
override fun nextBits(
|
override fun nextBits(
|
||||||
bitCount: Int,
|
bitCount: Int,
|
||||||
): Int {
|
): Int {
|
||||||
if (bitCount !in 0..32) throw Exception()
|
if (bitCount !in 0..32)
|
||||||
if (bitCount == 0) return 0
|
throw IllegalArgumentException()
|
||||||
|
if (bitCount == 0)
|
||||||
|
return 0
|
||||||
|
|
||||||
return generate(4).toInt() ushr (32 - bitCount)
|
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 {
|
companion object {
|
||||||
fun LCG() = com.infendro.random.prng.LCG()
|
fun LCG() = com.infendro.random.prng.LCG()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user