Compare commits
10 Commits
89e7569fa7
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
b8130ef527
|
|||
|
996751c1c6
|
|||
|
d9454fcdb2
|
|||
|
06d86a6679
|
|||
|
a28cb7d2f9
|
|||
|
8fab26a476
|
|||
|
3465a60cb1
|
|||
|
2aed836e67
|
|||
|
2ae6fda70e
|
|||
|
2c6fe0a7d9
|
10
README.md
10
README.md
@@ -1,9 +1,11 @@
|
|||||||
# prng-kt
|
# prng.kt
|
||||||
|
|
||||||
`prng-kt` is a Kotlin Multiplatform library that provides common [Pseudorandom Number Generators](https://en.wikipedia.org/wiki/Pseudorandom_number_generator).
|
`prng.kt` is a Kotlin Multiplatform library that provides common [Pseudorandom Number Generators](https://en.wikipedia.org/wiki/Pseudorandom_number_generator).
|
||||||
|
|
||||||
Supported [PRNG](https://en.wikipedia.org/wiki/Pseudorandom_number_generator) algorithms:
|
Supported [PRNG](https://en.wikipedia.org/wiki/Pseudorandom_number_generator) algorithms:
|
||||||
- LCG
|
- LCG
|
||||||
|
- xoshiro256++
|
||||||
|
- xoshiro256**
|
||||||
|
|
||||||
Supported [CSPRNG](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) algorithms:
|
Supported [CSPRNG](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) algorithms:
|
||||||
- HMAC-DRBG
|
- HMAC-DRBG
|
||||||
@@ -18,7 +20,7 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("com.infendro:prng:1.3.0")
|
implementation("com.infendro:prng:1.3.1")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -27,7 +29,7 @@ dependencies {
|
|||||||
```kotlin
|
```kotlin
|
||||||
fun main() {
|
fun main() {
|
||||||
// create PRNG instance
|
// create PRNG instance
|
||||||
val rng = CSPRNG.HMAC(`SHA-256`)
|
val rng = CSPRNG.HMAC(`SHA-256`())
|
||||||
|
|
||||||
// seed PRNG to receive deterministic numbers
|
// seed PRNG to receive deterministic numbers
|
||||||
rng.seed(/* seed */)
|
rng.seed(/* seed */)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import com.infendro.plugin.token
|
|||||||
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||||
|
|
||||||
group = "com.infendro"
|
group = "com.infendro"
|
||||||
version = "1.3.0"
|
version = "1.3.1"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
alias(libs.plugins.infendro)
|
alias(libs.plugins.infendro)
|
||||||
@@ -31,9 +31,9 @@ kotlin {
|
|||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
commonMain.dependencies {
|
commonMain.dependencies {
|
||||||
|
implementation(libs.bytes)
|
||||||
implementation(libs.hash)
|
implementation(libs.hash)
|
||||||
implementation(libs.mac)
|
implementation(libs.mac)
|
||||||
implementation(libs.bytes)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
[versions]
|
[versions]
|
||||||
infendro = "1.1.0"
|
infendro = "1.1.0"
|
||||||
kotlin = "2.3.0"
|
kotlin = "2.3.0"
|
||||||
mac = "1.3.0"
|
bytes = "1.4.2"
|
||||||
hash = "1.5.1"
|
hash = "1.7.0"
|
||||||
bytes = "1.3.3"
|
mac = "1.5.0"
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
||||||
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
mac = { module = "com.infendro:mac", version.ref = "mac" }
|
|
||||||
hash = { module = "com.infendro:hash", version.ref = "hash" }
|
|
||||||
bytes = { module = "com.infendro:bytes", version.ref = "bytes" }
|
bytes = { module = "com.infendro:bytes", version.ref = "bytes" }
|
||||||
|
hash = { module = "com.infendro:hash", version.ref = "hash" }
|
||||||
|
mac = { module = "com.infendro:mac", version.ref = "mac" }
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import com.infendro.mac.HMAC
|
|||||||
import com.infendro.random.CSPRNG
|
import com.infendro.random.CSPRNG
|
||||||
import kotlin.properties.Delegates.notNull
|
import kotlin.properties.Delegates.notNull
|
||||||
|
|
||||||
class `HMAC-DRBG` internal constructor(
|
class `HMAC-DRBG`(
|
||||||
private val function: HashFunction,
|
private val function: HashFunction,
|
||||||
) : CSPRNG() {
|
) : CSPRNG() {
|
||||||
private val hmac = HMAC(function)
|
private val hmac = HMAC(function)
|
||||||
@@ -14,6 +14,8 @@ class `HMAC-DRBG` internal constructor(
|
|||||||
private var k: ByteArray by notNull()
|
private var k: ByteArray by notNull()
|
||||||
private var v: ByteArray by notNull()
|
private var v: ByteArray by notNull()
|
||||||
|
|
||||||
|
private val buffer = ByteArray(function.bytes + 1)
|
||||||
|
|
||||||
override fun seed(seed: ByteArray) {
|
override fun seed(seed: ByteArray) {
|
||||||
k = ByteArray(function.bytes) { 0x00 }
|
k = ByteArray(function.bytes) { 0x00 }
|
||||||
v = ByteArray(function.bytes) { 0x01 }
|
v = ByteArray(function.bytes) { 0x01 }
|
||||||
@@ -21,17 +23,28 @@ class `HMAC-DRBG` internal constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun reseed(entropy: ByteArray) {
|
override fun reseed(entropy: ByteArray) {
|
||||||
k = hmac.hash(k, v + byteArrayOf(0x00) + entropy)
|
val value = ByteArray(v.size + 1 + entropy.size)
|
||||||
v = hmac.hash(k, v)
|
|
||||||
k = hmac.hash(k, v + byteArrayOf(0x01) + entropy)
|
v.copyInto(value)
|
||||||
v = hmac.hash(k, v)
|
value[v.size] = 0x00
|
||||||
|
entropy.copyInto(value, v.size + 1)
|
||||||
|
hmac.hashInto(k, value, k)
|
||||||
|
hmac.hashInto(k, v, v)
|
||||||
|
|
||||||
|
value[v.size] = 0x01
|
||||||
|
hmac.hashInto(k, value, k)
|
||||||
|
hmac.hashInto(k, v, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun generate(): Int {
|
override fun generate(): Int {
|
||||||
v = hmac.hash(k, v)
|
hmac.hashInto(k, v, v)
|
||||||
val result = v.toInt()
|
val result = v.toInt()
|
||||||
k = hmac.hash(k, v + byteArrayOf(0x00))
|
|
||||||
v = hmac.hash(k, v)
|
v.copyInto(buffer)
|
||||||
|
buffer[v.size] = 0x00
|
||||||
|
hmac.hashInto(k, buffer, k)
|
||||||
|
hmac.hashInto(k, v, v)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ import com.infendro.hash.sha2.`SHA-256`
|
|||||||
import com.infendro.random.PRNG
|
import com.infendro.random.PRNG
|
||||||
import kotlin.properties.Delegates.notNull
|
import kotlin.properties.Delegates.notNull
|
||||||
|
|
||||||
class LCG internal constructor() : PRNG() {
|
class LCG : PRNG() {
|
||||||
|
private val function = `SHA-256`()
|
||||||
|
|
||||||
private val a: Long = 25214903917L
|
private val a: Long = 25214903917L
|
||||||
private val c: Long = 11L
|
private val c: Long = 11L
|
||||||
private val mask: Long = (1L shl 48) - 1
|
private val mask: Long = (1L shl 48) - 1
|
||||||
@@ -13,7 +15,7 @@ class LCG internal constructor() : PRNG() {
|
|||||||
private var x: Long by notNull()
|
private var x: Long by notNull()
|
||||||
|
|
||||||
override fun seed(seed: ByteArray) {
|
override fun seed(seed: ByteArray) {
|
||||||
x = `SHA-256`.hash(seed).toLong() and mask
|
x = function.hash(seed).toLong() and mask
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun generate(): Int {
|
override fun generate(): Int {
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.infendro.random.prng
|
||||||
|
|
||||||
|
import com.infendro.bytes.bytearray.toLongArray
|
||||||
|
import com.infendro.hash.sha2.`SHA-256`
|
||||||
|
import com.infendro.random.PRNG
|
||||||
|
import kotlin.properties.Delegates.notNull
|
||||||
|
|
||||||
|
class Xoshiro256pp : PRNG() {
|
||||||
|
private val function = `SHA-256`()
|
||||||
|
|
||||||
|
private var s: LongArray by notNull()
|
||||||
|
|
||||||
|
override fun seed(seed: ByteArray) {
|
||||||
|
s = function.hash(seed).toLongArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun generate(): Int {
|
||||||
|
val result = (s[0] + s[3]).rotateLeft(23) + s[0]
|
||||||
|
|
||||||
|
val t = s[1] shl 17
|
||||||
|
s[2] = s[2] xor s[0]
|
||||||
|
s[3] = s[3] xor s[1]
|
||||||
|
s[1] = s[1] xor s[2]
|
||||||
|
s[0] = s[0] xor s[3]
|
||||||
|
s[2] = s[2] xor t
|
||||||
|
s[3] = s[3].rotateLeft(45)
|
||||||
|
|
||||||
|
return (result ushr 32).toInt()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.infendro.random.prng
|
||||||
|
|
||||||
|
import com.infendro.bytes.bytearray.toLongArray
|
||||||
|
import com.infendro.hash.sha2.`SHA-256`
|
||||||
|
import com.infendro.random.PRNG
|
||||||
|
import kotlin.properties.Delegates.notNull
|
||||||
|
|
||||||
|
class Xoshiro256ss : PRNG() {
|
||||||
|
private val function = `SHA-256`()
|
||||||
|
|
||||||
|
private var s: LongArray by notNull()
|
||||||
|
|
||||||
|
override fun seed(seed: ByteArray) {
|
||||||
|
s = function.hash(seed).toLongArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun generate(): Int {
|
||||||
|
val result = (s[1] * 5).rotateLeft(7) * 9
|
||||||
|
|
||||||
|
val t = s[1] shl 17
|
||||||
|
s[2] = s[2] xor s[0]
|
||||||
|
s[3] = s[3] xor s[1]
|
||||||
|
s[1] = s[1] xor s[2]
|
||||||
|
s[0] = s[0] xor s[3]
|
||||||
|
s[2] = s[2] xor t
|
||||||
|
s[3] = s[3].rotateLeft(45)
|
||||||
|
|
||||||
|
return (result ushr 32).toInt()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user