fix: 鍵の形式を修正

This commit is contained in:
usbharu 2023-03-31 17:39:38 +09:00
parent 6d7bc25946
commit 7086322a39
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
4 changed files with 18 additions and 36 deletions

View File

@ -152,7 +152,7 @@ class KtorKeyMap(private val userAuthRepository: IUserAuthService) : KeyMap {
userAuthRepository.findByUsername(
username
).publicKey?.replace("-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----")?.replace("", "")
?.replace("\\n", "")
?.replace("\n", "")
)
val x509EncodedKeySpec = X509EncodedKeySpec(publicBytes)
return@runBlocking KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec)
@ -165,7 +165,7 @@ class KtorKeyMap(private val userAuthRepository: IUserAuthService) : KeyMap {
userAuthRepository.findByUsername(
username
).privateKey?.replace("-----BEGIN RSA PRIVATE KEY-----", "")?.replace("-----END RSA PRIVATE KEY-----", "")
?.replace("\\n", "")
?.replace("\n", "")
)
val x509EncodedKeySpec = PKCS8EncodedKeySpec(publicBytes)
return@runBlocking KeyFactory.getInstance("RSA").generatePrivate(x509EncodedKeySpec)

View File

@ -12,6 +12,8 @@ import io.ktor.util.*
import java.security.KeyPair
import java.security.KeyPairGenerator
import java.security.MessageDigest
import java.security.PrivateKey
import java.security.PublicKey
import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey
import java.util.*
@ -81,19 +83,21 @@ class UserAuthService(
}
private fun RSAPublicKey.toPem(): String {
return "-----BEGIN RSA PUBLIC KEY-----" +
Base64.getEncoder().encodeToString(encoded) +
"-----END RSA PUBLIC KEY-----"
}
private fun RSAPrivateKey.toPem(): String {
return "-----BEGIN RSA PRIVATE KEY-----" +
Base64.getEncoder().encodeToString(encoded) +
"-----END RSA PRIVATE KEY-----"
}
companion object {
val sha256: MessageDigest = MessageDigest.getInstance("SHA-256")
}
}
public fun PublicKey.toPem(): String {
return "-----BEGIN RSA PUBLIC KEY-----" +
Base64.getEncoder().encodeToString(encoded).chunked(64).joinToString("\n") +
"-----END RSA PUBLIC KEY-----"
}
public fun PrivateKey.toPem(): String {
return "-----BEGIN RSA PRIVATE KEY-----" +
Base64.getEncoder().encodeToString(encoded).chunked(64).joinToString("\n") +
"-----END RSA PRIVATE KEY-----"
}

View File

@ -8,6 +8,7 @@ import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.repository.IUserAuthRepository
import dev.usbharu.hideout.repository.IUserRepository
import dev.usbharu.hideout.service.UserAuthService
import dev.usbharu.hideout.service.toPem
import io.ktor.client.*
import io.ktor.client.engine.mock.*
import io.ktor.client.plugins.logging.*
@ -107,16 +108,4 @@ class ActivityPubKtTest {
}
fun RSAPublicKey.toPem(): String {
return "-----BEGIN RSA PUBLIC KEY-----" +
Base64.getEncoder().encodeToString(encoded) +
"-----END RSA PUBLIC KEY-----"
}
fun RSAPrivateKey.toPem(): String {
return "-----BEGIN RSA PRIVATE KEY-----" +
Base64.getEncoder().encodeToString(encoded) +
"-----END RSA PRIVATE KEY-----"
}
}

View File

@ -7,6 +7,7 @@ import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.repository.IUserAuthRepository
import dev.usbharu.hideout.repository.IUserRepository
import dev.usbharu.hideout.service.UserAuthService
import dev.usbharu.hideout.service.toPem
import org.junit.jupiter.api.Test
import java.security.KeyPairGenerator
import java.security.interfaces.RSAPrivateKey
@ -88,16 +89,4 @@ class KtorKeyMapTest {
ktorKeyMap.getPrivateKey("test")
}
private fun RSAPublicKey.toPem(): String {
return "-----BEGIN RSA PUBLIC KEY-----" +
Base64.getEncoder().encodeToString(encoded) +
"-----END RSA PUBLIC KEY-----"
}
private fun RSAPrivateKey.toPem(): String {
return "-----BEGIN RSA PRIVATE KEY-----" +
Base64.getEncoder().encodeToString(encoded) +
"-----END RSA PRIVATE KEY-----"
}
}