Add RsaKeyProvider

bzip2
Graham 5 years ago
parent 0bb0d1a5b6
commit 84b384156f
  1. 4
      common/pom.xml
  2. 14
      common/src/main/java/dev/openrs2/common/CommonModule.kt
  3. 22
      common/src/main/java/dev/openrs2/common/crypto/RsaKeyProvider.kt
  4. 1
      conf/.gitignore
  5. 5
      pom.xml

@ -19,6 +19,10 @@
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.google.jimfs</groupId> <groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId> <artifactId>jimfs</artifactId>

@ -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)
}
}

@ -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<RSAPrivateCrtKeyParameters> {
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")
}
}

1
conf/.gitignore vendored

@ -0,0 +1 @@
/private.key

@ -58,6 +58,11 @@
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>28.2-jre</version> <version>28.2-jre</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.2</version>
</dependency>
<dependency> <dependency>
<groupId>com.google.jimfs</groupId> <groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId> <artifactId>jimfs</artifactId>

Loading…
Cancel
Save