From 84b384156fa522eee47656f6f6bacddda3c088fc Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 14 Jan 2020 23:06:22 +0000 Subject: [PATCH] Add RsaKeyProvider --- common/pom.xml | 4 ++++ .../java/dev/openrs2/common/CommonModule.kt | 14 ++++++++++++ .../openrs2/common/crypto/RsaKeyProvider.kt | 22 +++++++++++++++++++ conf/.gitignore | 1 + pom.xml | 5 +++++ 5 files changed, 46 insertions(+) create mode 100644 common/src/main/java/dev/openrs2/common/CommonModule.kt create mode 100644 common/src/main/java/dev/openrs2/common/crypto/RsaKeyProvider.kt create mode 100644 conf/.gitignore diff --git a/common/pom.xml b/common/pom.xml index a472baea..6624d84e 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -19,6 +19,10 @@ com.google.guava guava + + com.google.inject + guice + com.google.jimfs jimfs diff --git a/common/src/main/java/dev/openrs2/common/CommonModule.kt b/common/src/main/java/dev/openrs2/common/CommonModule.kt new file mode 100644 index 00000000..bda823be --- /dev/null +++ b/common/src/main/java/dev/openrs2/common/CommonModule.kt @@ -0,0 +1,14 @@ +package dev.openrs2.common + +import com.google.inject.AbstractModule +import com.google.inject.Scopes +import dev.openrs2.common.crypto.RsaKeyProvider +import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters + +class CommonModule : AbstractModule() { + override fun configure() { + bind(RSAPrivateCrtKeyParameters::class.java) + .toProvider(RsaKeyProvider::class.java) + .`in`(Scopes.SINGLETON) + } +} diff --git a/common/src/main/java/dev/openrs2/common/crypto/RsaKeyProvider.kt b/common/src/main/java/dev/openrs2/common/crypto/RsaKeyProvider.kt new file mode 100644 index 00000000..97dd7d1c --- /dev/null +++ b/common/src/main/java/dev/openrs2/common/crypto/RsaKeyProvider.kt @@ -0,0 +1,22 @@ +package dev.openrs2.common.crypto + +import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters +import java.nio.file.Files +import java.nio.file.Paths +import javax.inject.Provider + +class RsaKeyProvider : Provider { + override fun get(): RSAPrivateCrtKeyParameters { + return if (Files.exists(PATH)) { + Rsa.readPrivateKey(PATH) + } else { + val (_, private) = Rsa.generateKeyPair() + Rsa.writePrivateKey(PATH, private) + private + } + } + + companion object { + private val PATH = Paths.get("conf/private.key") + } +} diff --git a/conf/.gitignore b/conf/.gitignore new file mode 100644 index 00000000..b8bd887f --- /dev/null +++ b/conf/.gitignore @@ -0,0 +1 @@ +/private.key diff --git a/pom.xml b/pom.xml index 013a329d..f8b3f979 100644 --- a/pom.xml +++ b/pom.xml @@ -58,6 +58,11 @@ guava 28.2-jre + + com.google.inject + guice + 4.2.2 + com.google.jimfs jimfs