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 964c8904ac..1737baefcc 100644 --- a/common/src/main/java/dev/openrs2/common/crypto/Rsa.kt +++ b/common/src/main/java/dev/openrs2/common/crypto/Rsa.kt @@ -24,6 +24,9 @@ import java.math.BigInteger import java.nio.file.Files import java.nio.file.Path +val RSAPrivateCrtKeyParameters.publicKey + get() = RSAKeyParameters(false, modulus, publicExponent) + private fun ByteBuf.toBigInteger(): BigInteger { val bytes: ByteArray if (hasArray() && arrayOffset() == 0 && readerIndex() == 0 && readableBytes() == array().size) { @@ -67,9 +70,6 @@ object Rsa { // 1 in 2^80 private const val CERTAINTY = 80 - val RSAPrivateCrtKeyParameters.publicKey - get() = RSAKeyParameters(false, modulus, publicExponent) - fun generateKeyPair(): Pair { val generator = RSAKeyPairGenerator() generator.init(RSAKeyGenerationParameters(F4, secureRandom, KEY_LENGTH, CERTAINTY)) 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 4d997fceb0..5f19e7015e 100644 --- a/common/src/test/java/dev/openrs2/common/crypto/RsaTest.kt +++ b/common/src/test/java/dev/openrs2/common/crypto/RsaTest.kt @@ -178,6 +178,13 @@ object RsaTest { } } + @Test + fun testPrivateToPublicKey() { + val public = allowUnsafeMod { PRIVATE_KEY_CRT.publicKey } + assertEquals(PUBLIC_KEY.modulus, public.modulus) + assertEquals(PUBLIC_KEY.exponent, public.exponent) + } + private fun allowUnsafeMod(f: () -> T): T { Properties.setThreadOverride(ALLOW_UNSAFE_MOD, true) try {