mirror of https://github.com/usbharu/Hideout.git
fix: 鍵の形式を修正
This commit is contained in:
parent
70c3e408f4
commit
bc4807ddc9
|
@ -152,7 +152,7 @@ class KtorKeyMap(private val userAuthRepository: IUserAuthService) : KeyMap {
|
||||||
userAuthRepository.findByUsername(
|
userAuthRepository.findByUsername(
|
||||||
username
|
username
|
||||||
).publicKey?.replace("-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----")?.replace("", "")
|
).publicKey?.replace("-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----")?.replace("", "")
|
||||||
?.replace("\\n", "")
|
?.replace("\n", "")
|
||||||
)
|
)
|
||||||
val x509EncodedKeySpec = X509EncodedKeySpec(publicBytes)
|
val x509EncodedKeySpec = X509EncodedKeySpec(publicBytes)
|
||||||
return@runBlocking KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec)
|
return@runBlocking KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec)
|
||||||
|
@ -165,7 +165,7 @@ class KtorKeyMap(private val userAuthRepository: IUserAuthService) : KeyMap {
|
||||||
userAuthRepository.findByUsername(
|
userAuthRepository.findByUsername(
|
||||||
username
|
username
|
||||||
).privateKey?.replace("-----BEGIN RSA PRIVATE KEY-----", "")?.replace("-----END RSA PRIVATE KEY-----", "")
|
).privateKey?.replace("-----BEGIN RSA PRIVATE KEY-----", "")?.replace("-----END RSA PRIVATE KEY-----", "")
|
||||||
?.replace("\\n", "")
|
?.replace("\n", "")
|
||||||
)
|
)
|
||||||
val x509EncodedKeySpec = PKCS8EncodedKeySpec(publicBytes)
|
val x509EncodedKeySpec = PKCS8EncodedKeySpec(publicBytes)
|
||||||
return@runBlocking KeyFactory.getInstance("RSA").generatePrivate(x509EncodedKeySpec)
|
return@runBlocking KeyFactory.getInstance("RSA").generatePrivate(x509EncodedKeySpec)
|
||||||
|
|
|
@ -12,6 +12,8 @@ import io.ktor.util.*
|
||||||
import java.security.KeyPair
|
import java.security.KeyPair
|
||||||
import java.security.KeyPairGenerator
|
import java.security.KeyPairGenerator
|
||||||
import java.security.MessageDigest
|
import java.security.MessageDigest
|
||||||
|
import java.security.PrivateKey
|
||||||
|
import java.security.PublicKey
|
||||||
import java.security.interfaces.RSAPrivateKey
|
import java.security.interfaces.RSAPrivateKey
|
||||||
import java.security.interfaces.RSAPublicKey
|
import java.security.interfaces.RSAPublicKey
|
||||||
import java.util.*
|
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 {
|
companion object {
|
||||||
val sha256: MessageDigest = MessageDigest.getInstance("SHA-256")
|
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-----"
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import dev.usbharu.hideout.domain.model.UserEntity
|
||||||
import dev.usbharu.hideout.repository.IUserAuthRepository
|
import dev.usbharu.hideout.repository.IUserAuthRepository
|
||||||
import dev.usbharu.hideout.repository.IUserRepository
|
import dev.usbharu.hideout.repository.IUserRepository
|
||||||
import dev.usbharu.hideout.service.UserAuthService
|
import dev.usbharu.hideout.service.UserAuthService
|
||||||
|
import dev.usbharu.hideout.service.toPem
|
||||||
import io.ktor.client.*
|
import io.ktor.client.*
|
||||||
import io.ktor.client.engine.mock.*
|
import io.ktor.client.engine.mock.*
|
||||||
import io.ktor.client.plugins.logging.*
|
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-----"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import dev.usbharu.hideout.domain.model.UserEntity
|
||||||
import dev.usbharu.hideout.repository.IUserAuthRepository
|
import dev.usbharu.hideout.repository.IUserAuthRepository
|
||||||
import dev.usbharu.hideout.repository.IUserRepository
|
import dev.usbharu.hideout.repository.IUserRepository
|
||||||
import dev.usbharu.hideout.service.UserAuthService
|
import dev.usbharu.hideout.service.UserAuthService
|
||||||
|
import dev.usbharu.hideout.service.toPem
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import java.security.KeyPairGenerator
|
import java.security.KeyPairGenerator
|
||||||
import java.security.interfaces.RSAPrivateKey
|
import java.security.interfaces.RSAPrivateKey
|
||||||
|
@ -88,16 +89,4 @@ class KtorKeyMapTest {
|
||||||
|
|
||||||
ktorKeyMap.getPrivateKey("test")
|
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-----"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue