|
|
@ -2,6 +2,7 @@ package dev.openrs2.crypto |
|
|
|
|
|
|
|
|
|
|
|
import com.google.common.jimfs.Configuration |
|
|
|
import com.google.common.jimfs.Configuration |
|
|
|
import com.google.common.jimfs.Jimfs |
|
|
|
import com.google.common.jimfs.Jimfs |
|
|
|
|
|
|
|
import dev.openrs2.util.io.useTempFile |
|
|
|
import io.netty.buffer.Unpooled |
|
|
|
import io.netty.buffer.Unpooled |
|
|
|
import org.bouncycastle.crypto.params.RSAKeyParameters |
|
|
|
import org.bouncycastle.crypto.params.RSAKeyParameters |
|
|
|
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters |
|
|
|
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters |
|
|
@ -116,16 +117,13 @@ object RsaTest { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun testReadPublicKey() { |
|
|
|
fun testReadPublicKey() { |
|
|
|
Jimfs.newFileSystem(Configuration.unix()).use { fs -> |
|
|
|
Jimfs.newFileSystem(Configuration.unix()).use { fs -> |
|
|
|
val file = Files.createTempFile(fs.getPath("/"), "public", ".key") |
|
|
|
fs.getPath("/").useTempFile("public", ".key") { file -> |
|
|
|
try { |
|
|
|
|
|
|
|
Files.write(file, PUBLIC_KEY_PEM) |
|
|
|
Files.write(file, PUBLIC_KEY_PEM) |
|
|
|
|
|
|
|
|
|
|
|
val key = allowUnsafeMod { Rsa.readPublicKey(file) } |
|
|
|
val key = allowUnsafeMod { Rsa.readPublicKey(file) } |
|
|
|
assertFalse(key.isPrivate) |
|
|
|
assertFalse(key.isPrivate) |
|
|
|
assertEquals(PUBLIC_KEY.modulus, key.modulus) |
|
|
|
assertEquals(PUBLIC_KEY.modulus, key.modulus) |
|
|
|
assertEquals(PUBLIC_KEY.exponent, key.exponent) |
|
|
|
assertEquals(PUBLIC_KEY.exponent, key.exponent) |
|
|
|
} finally { |
|
|
|
|
|
|
|
Files.deleteIfExists(file) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -133,13 +131,10 @@ object RsaTest { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun testWritePublicKey() { |
|
|
|
fun testWritePublicKey() { |
|
|
|
Jimfs.newFileSystem(Configuration.unix()).use { fs -> |
|
|
|
Jimfs.newFileSystem(Configuration.unix()).use { fs -> |
|
|
|
val file = Files.createTempFile(fs.getPath("/"), "public", ".key") |
|
|
|
fs.getPath("/").useTempFile("public", ".key") { file -> |
|
|
|
try { |
|
|
|
|
|
|
|
Rsa.writePublicKey(file, PUBLIC_KEY) |
|
|
|
Rsa.writePublicKey(file, PUBLIC_KEY) |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(PUBLIC_KEY_PEM, Files.readAllLines(file)) |
|
|
|
assertEquals(PUBLIC_KEY_PEM, Files.readAllLines(file)) |
|
|
|
} finally { |
|
|
|
|
|
|
|
Files.deleteIfExists(file) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -147,8 +142,7 @@ object RsaTest { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun testReadPrivateKey() { |
|
|
|
fun testReadPrivateKey() { |
|
|
|
Jimfs.newFileSystem(Configuration.unix()).use { fs -> |
|
|
|
Jimfs.newFileSystem(Configuration.unix()).use { fs -> |
|
|
|
val file = Files.createTempFile(fs.getPath("/"), "private", ".key") |
|
|
|
fs.getPath("/").useTempFile("private", ".key") { file -> |
|
|
|
try { |
|
|
|
|
|
|
|
Files.write(file, PRIVATE_KEY_PEM) |
|
|
|
Files.write(file, PRIVATE_KEY_PEM) |
|
|
|
|
|
|
|
|
|
|
|
val key = allowUnsafeMod { Rsa.readPrivateKey(file) } |
|
|
|
val key = allowUnsafeMod { Rsa.readPrivateKey(file) } |
|
|
@ -161,8 +155,6 @@ object RsaTest { |
|
|
|
assertEquals(PRIVATE_KEY_CRT.dp, key.dp) |
|
|
|
assertEquals(PRIVATE_KEY_CRT.dp, key.dp) |
|
|
|
assertEquals(PRIVATE_KEY_CRT.dq, key.dq) |
|
|
|
assertEquals(PRIVATE_KEY_CRT.dq, key.dq) |
|
|
|
assertEquals(PRIVATE_KEY_CRT.qInv, key.qInv) |
|
|
|
assertEquals(PRIVATE_KEY_CRT.qInv, key.qInv) |
|
|
|
} finally { |
|
|
|
|
|
|
|
Files.deleteIfExists(file) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -170,13 +162,10 @@ object RsaTest { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun testWritePrivateKey() { |
|
|
|
fun testWritePrivateKey() { |
|
|
|
Jimfs.newFileSystem(Configuration.unix()).use { fs -> |
|
|
|
Jimfs.newFileSystem(Configuration.unix()).use { fs -> |
|
|
|
val file = Files.createTempFile(fs.getPath("/"), "private", ".key") |
|
|
|
fs.getPath("/").useTempFile("private", ".key") { file -> |
|
|
|
try { |
|
|
|
|
|
|
|
Rsa.writePrivateKey(file, PRIVATE_KEY_CRT) |
|
|
|
Rsa.writePrivateKey(file, PRIVATE_KEY_CRT) |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(PRIVATE_KEY_PEM, Files.readAllLines(file)) |
|
|
|
assertEquals(PRIVATE_KEY_PEM, Files.readAllLines(file)) |
|
|
|
} finally { |
|
|
|
|
|
|
|
Files.deleteIfExists(file) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|