From ae88bca92407d571b73ee0eadd4ac65338b5ba20 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 27 Mar 2021 10:03:03 +0000 Subject: [PATCH] Add ent command for analysing the randomness of keys Signed-off-by: Graham --- .../org/openrs2/archive/key/EntCommand.kt | 32 ++++++++++++++++ .../org/openrs2/archive/key/KeyCommand.kt | 1 + .../org/openrs2/archive/key/KeyExporter.kt | 38 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 archive/src/main/kotlin/org/openrs2/archive/key/EntCommand.kt create mode 100644 archive/src/main/kotlin/org/openrs2/archive/key/KeyExporter.kt diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/EntCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/key/EntCommand.kt new file mode 100644 index 00000000..5a375c0e --- /dev/null +++ b/archive/src/main/kotlin/org/openrs2/archive/key/EntCommand.kt @@ -0,0 +1,32 @@ +package org.openrs2.archive.key + +import com.github.ajalt.clikt.core.CliktCommand +import com.google.inject.Guice +import kotlinx.coroutines.runBlocking +import org.openrs2.archive.ArchiveModule +import org.openrs2.inject.CloseableInjector +import java.io.BufferedOutputStream +import java.io.DataOutputStream + +public class EntCommand : CliktCommand(name = "ent") { + override fun run(): Unit = runBlocking { + CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> + val exporter = injector.getInstance(KeyExporter::class.java) + val keys = exporter.exportValid() + + val process = ProcessBuilder("ent") + .redirectOutput(ProcessBuilder.Redirect.INHERIT) + .redirectError(ProcessBuilder.Redirect.INHERIT) + .start() + + DataOutputStream(BufferedOutputStream(process.outputStream)).use { out -> + for (key in keys) { + out.writeInt(key.k0) + out.writeInt(key.k1) + out.writeInt(key.k2) + out.writeInt(key.k3) + } + } + } + } +} diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/KeyCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/key/KeyCommand.kt index 0efce58f..d905a3b8 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/KeyCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/KeyCommand.kt @@ -8,6 +8,7 @@ public class KeyCommand : NoOpCliktCommand(name = "key") { subcommands( BruteForceCommand(), DownloadCommand(), + EntCommand(), ImportCommand() ) } diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/KeyExporter.kt b/archive/src/main/kotlin/org/openrs2/archive/key/KeyExporter.kt new file mode 100644 index 00000000..10188f06 --- /dev/null +++ b/archive/src/main/kotlin/org/openrs2/archive/key/KeyExporter.kt @@ -0,0 +1,38 @@ +package org.openrs2.archive.key + +import org.openrs2.crypto.XteaKey +import org.openrs2.db.Database +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +public class KeyExporter @Inject constructor( + private val database: Database +) { + public suspend fun exportValid(): List { + return database.execute { connection -> + connection.prepareStatement( + """ + SELECT (k.key).k0, (k.key).k1, (k.key).k2, (k.key).k3 + FROM keys k + JOIN containers c ON c.key_id = k.id + ORDER BY k.id ASC + """.trimIndent() + ).use { stmt -> + stmt.executeQuery().use { rows -> + val keys = mutableListOf() + + while (rows.next()) { + val k0 = rows.getInt(1) + val k1 = rows.getInt(2) + val k2 = rows.getInt(3) + val k3 = rows.getInt(4) + keys += XteaKey(k0, k1, k2, k3) + } + + keys + } + } + } + } +}