Add publicKey property test

master
Graham 5 years ago
parent 1cdf67e182
commit db11c5536d
  1. 6
      common/src/main/java/dev/openrs2/common/crypto/Rsa.kt
  2. 7
      common/src/test/java/dev/openrs2/common/crypto/RsaTest.kt

@ -24,6 +24,9 @@ import java.math.BigInteger
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
val RSAPrivateCrtKeyParameters.publicKey
get() = RSAKeyParameters(false, modulus, publicExponent)
private fun ByteBuf.toBigInteger(): BigInteger { private fun ByteBuf.toBigInteger(): BigInteger {
val bytes: ByteArray val bytes: ByteArray
if (hasArray() && arrayOffset() == 0 && readerIndex() == 0 && readableBytes() == array().size) { if (hasArray() && arrayOffset() == 0 && readerIndex() == 0 && readableBytes() == array().size) {
@ -67,9 +70,6 @@ object Rsa {
// 1 in 2^80 // 1 in 2^80
private const val CERTAINTY = 80 private const val CERTAINTY = 80
val RSAPrivateCrtKeyParameters.publicKey
get() = RSAKeyParameters(false, modulus, publicExponent)
fun generateKeyPair(): Pair<RSAKeyParameters, RSAPrivateCrtKeyParameters> { fun generateKeyPair(): Pair<RSAKeyParameters, RSAPrivateCrtKeyParameters> {
val generator = RSAKeyPairGenerator() val generator = RSAKeyPairGenerator()
generator.init(RSAKeyGenerationParameters(F4, secureRandom, KEY_LENGTH, CERTAINTY)) generator.init(RSAKeyGenerationParameters(F4, secureRandom, KEY_LENGTH, CERTAINTY))

@ -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 <T> allowUnsafeMod(f: () -> T): T { private fun <T> allowUnsafeMod(f: () -> T): T {
Properties.setThreadOverride(ALLOW_UNSAFE_MOD, true) Properties.setThreadOverride(ALLOW_UNSAFE_MOD, true)
try { try {

Loading…
Cancel
Save