From 2fa55b3ce1a15c1af52b389415f5535e033c8d5e Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 15 Mar 2020 12:25:13 +0000 Subject: [PATCH] Add length parameter to Rsa.generateKeyPair() --- common/src/main/java/dev/openrs2/common/crypto/Rsa.kt | 6 +++--- .../main/java/dev/openrs2/common/crypto/RsaKeyProvider.kt | 2 +- common/src/test/java/dev/openrs2/common/crypto/RsaTest.kt | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/dev/openrs2/common/crypto/Rsa.kt b/common/src/main/java/dev/openrs2/common/crypto/Rsa.kt index 57e8841e01..a8241436ac 100644 --- a/common/src/main/java/dev/openrs2/common/crypto/Rsa.kt +++ b/common/src/main/java/dev/openrs2/common/crypto/Rsa.kt @@ -59,14 +59,14 @@ object Rsa { * The maximum output length of RSA encryption is the key size plus one, so * the maximum key size supported by the client is 126 bytes - or 1008 bits. */ - private const val KEY_LENGTH = 1008 + const val KEY_LENGTH = 1008 // 1 in 2^80 private const val CERTAINTY = 80 - fun generateKeyPair(): Pair { + fun generateKeyPair(length: Int): Pair { val generator = RSAKeyPairGenerator() - generator.init(RSAKeyGenerationParameters(F4, secureRandom, KEY_LENGTH, CERTAINTY)) + generator.init(RSAKeyGenerationParameters(F4, secureRandom, length, CERTAINTY)) val keyPair = generator.generateKeyPair() return Pair(keyPair.public as RSAKeyParameters, keyPair.private as RSAPrivateCrtKeyParameters) diff --git a/common/src/main/java/dev/openrs2/common/crypto/RsaKeyProvider.kt b/common/src/main/java/dev/openrs2/common/crypto/RsaKeyProvider.kt index a4f3ff28cb..807acdf5ea 100644 --- a/common/src/main/java/dev/openrs2/common/crypto/RsaKeyProvider.kt +++ b/common/src/main/java/dev/openrs2/common/crypto/RsaKeyProvider.kt @@ -10,7 +10,7 @@ class RsaKeyProvider : Provider { return if (Files.exists(PRIVATE_PATH)) { Rsa.readPrivateKey(PRIVATE_PATH) } else { - val (public, private) = Rsa.generateKeyPair() + val (public, private) = Rsa.generateKeyPair(Rsa.KEY_LENGTH) Rsa.writePublicKey(PUBLIC_PATH, public) Rsa.writePrivateKey(PRIVATE_PATH, private) private diff --git a/common/src/test/java/dev/openrs2/common/crypto/RsaTest.kt b/common/src/test/java/dev/openrs2/common/crypto/RsaTest.kt index 0f51167b80..c11be80cc6 100644 --- a/common/src/test/java/dev/openrs2/common/crypto/RsaTest.kt +++ b/common/src/test/java/dev/openrs2/common/crypto/RsaTest.kt @@ -46,7 +46,7 @@ object RsaTest { @Test fun testGenerateKeyPair() { - val (public, private) = Rsa.generateKeyPair() + val (public, private) = Rsa.generateKeyPair(Rsa.KEY_LENGTH) val expectedPlaintext = BigInteger("1337") val ciphertext = Rsa.encrypt(expectedPlaintext, public)