Improve RSA method naming

Signed-off-by: Graham <gpe@openrs2.org>
Graham 4 years ago
parent 2c7cb5e054
commit 5e6afb88f6
  1. 6
      cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt
  2. 6
      crypto/src/main/kotlin/org/openrs2/crypto/Rsa.kt
  3. 14
      crypto/src/test/kotlin/org/openrs2/crypto/RsaTest.kt

@ -7,7 +7,7 @@ import org.openrs2.buffer.crc32
import org.openrs2.buffer.use import org.openrs2.buffer.use
import org.openrs2.crypto.Rsa import org.openrs2.crypto.Rsa
import org.openrs2.crypto.Whirlpool import org.openrs2.crypto.Whirlpool
import org.openrs2.crypto.rsaCrypt import org.openrs2.crypto.rsa
import org.openrs2.crypto.whirlpool import org.openrs2.crypto.whirlpool
public data class Js5MasterIndex( public data class Js5MasterIndex(
@ -91,7 +91,7 @@ public data class Js5MasterIndex(
plaintext.writeByte(Rsa.MAGIC) plaintext.writeByte(Rsa.MAGIC)
plaintext.writeBytes(digest) plaintext.writeBytes(digest)
plaintext.rsaCrypt(key).use { ciphertext -> plaintext.rsa(key).use { ciphertext ->
buf.writeBytes(ciphertext) buf.writeBytes(ciphertext)
} }
} }
@ -217,7 +217,7 @@ public data class Js5MasterIndex(
private fun decrypt(buf: ByteBuf, key: RSAKeyParameters?): ByteBuf { private fun decrypt(buf: ByteBuf, key: RSAKeyParameters?): ByteBuf {
return if (key != null) { return if (key != null) {
buf.rsaCrypt(key) buf.rsa(key)
} else { } else {
buf.retain() buf.retain()
} }

@ -40,8 +40,8 @@ private fun BigInteger.toByteBuf(): ByteBuf {
return Unpooled.wrappedBuffer(toByteArray()) return Unpooled.wrappedBuffer(toByteArray())
} }
public fun ByteBuf.rsaCrypt(key: RSAKeyParameters): ByteBuf { public fun ByteBuf.rsa(key: RSAKeyParameters): ByteBuf {
return Rsa.crypt(toBigInteger(), key).toByteBuf() return Rsa.apply(toBigInteger(), key).toByteBuf()
} }
public fun RSAPrivateCrtKeyParameters.toKeySpec(): KeySpec { public fun RSAPrivateCrtKeyParameters.toKeySpec(): KeySpec {
@ -97,7 +97,7 @@ public object Rsa {
} }
} }
public fun crypt(ciphertext: BigInteger, key: RSAKeyParameters): BigInteger { public fun apply(ciphertext: BigInteger, key: RSAKeyParameters): BigInteger {
if (key is RSAPrivateCrtKeyParameters) { if (key is RSAPrivateCrtKeyParameters) {
// blind the input // blind the input
val e = key.publicExponent val e = key.publicExponent

@ -51,34 +51,34 @@ object RsaTest {
val (public, private) = Rsa.generateKeyPair(Rsa.CLIENT_KEY_LENGTH) val (public, private) = Rsa.generateKeyPair(Rsa.CLIENT_KEY_LENGTH)
val expectedPlaintext = BigInteger("1337") val expectedPlaintext = BigInteger("1337")
val ciphertext = Rsa.crypt(expectedPlaintext, public) val ciphertext = Rsa.apply(expectedPlaintext, public)
val actualPlaintext = Rsa.crypt(ciphertext, private) val actualPlaintext = Rsa.apply(ciphertext, private)
assertEquals(expectedPlaintext, actualPlaintext) assertEquals(expectedPlaintext, actualPlaintext)
} }
@Test @Test
fun testEncrypt() { fun testEncrypt() {
val ciphertext = Rsa.crypt(BigInteger("65"), PUBLIC_KEY) val ciphertext = Rsa.apply(BigInteger("65"), PUBLIC_KEY)
assertEquals(BigInteger("2790"), ciphertext) assertEquals(BigInteger("2790"), ciphertext)
} }
@Test @Test
fun testDecrypt() { fun testDecrypt() {
val ciphertext = Rsa.crypt(BigInteger("2790"), PRIVATE_KEY) val ciphertext = Rsa.apply(BigInteger("2790"), PRIVATE_KEY)
assertEquals(BigInteger("65"), ciphertext) assertEquals(BigInteger("65"), ciphertext)
} }
@Test @Test
fun testDecryptCrt() { fun testDecryptCrt() {
val ciphertext = Rsa.crypt(BigInteger("2790"), PRIVATE_KEY_CRT) val ciphertext = Rsa.apply(BigInteger("2790"), PRIVATE_KEY_CRT)
assertEquals(BigInteger("65"), ciphertext) assertEquals(BigInteger("65"), ciphertext)
} }
@Test @Test
fun testEncryptByteBuf() { fun testEncryptByteBuf() {
wrappedBuffer(65).use { plaintext -> wrappedBuffer(65).use { plaintext ->
plaintext.rsaCrypt(PUBLIC_KEY).use { ciphertext -> plaintext.rsa(PUBLIC_KEY).use { ciphertext ->
wrappedBuffer(10, 230.toByte()).use { expectedCiphertext -> wrappedBuffer(10, 230.toByte()).use { expectedCiphertext ->
assertEquals(expectedCiphertext, ciphertext) assertEquals(expectedCiphertext, ciphertext)
} }
@ -89,7 +89,7 @@ object RsaTest {
@Test @Test
fun testDecryptByteBuf() { fun testDecryptByteBuf() {
wrappedBuffer(10, 230.toByte()).use { ciphertext -> wrappedBuffer(10, 230.toByte()).use { ciphertext ->
ciphertext.rsaCrypt(PRIVATE_KEY).use { plaintext -> ciphertext.rsa(PRIVATE_KEY).use { plaintext ->
wrappedBuffer(65).use { expectedPlaintext -> wrappedBuffer(65).use { expectedPlaintext ->
assertEquals(expectedPlaintext, plaintext) assertEquals(expectedPlaintext, plaintext)
} }

Loading…
Cancel
Save