Skip to content

Commit

Permalink
Integrate Pull request changes
Browse files Browse the repository at this point in the history
  • Loading branch information
n0900 authored and JesusMcCloud committed Oct 20, 2023
1 parent 67ac603 commit 2a31187
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,15 @@ data class JsonWebKey(
}

override fun toString() =
when (type) {
JwkType.EC -> "JsonWebKey(" +
"type=$type, " +
"curve=$curve, " +
"keyId=$keyId," +
"x=${x?.encodeToString(Base64Strict)}," +
"y=${y?.encodeToString(Base64Strict)}" +
")"

JwkType.RSA -> "JsonWebKey(" +
"type=$type, " +
"keyId=$keyId," +
"n=${n?.encodeToString(Base64Strict)})" +
"e=${e?.encodeToString(Base64Strict)}" +
")"

null -> "JsonWebKey(" +
"type=$type, " +
"curve=$curve, " +
"keyId=$keyId," +
"x=${x?.encodeToString(Base64Strict)}," +
"y=${y?.encodeToString(Base64Strict)}" +
"n=${n?.encodeToString(Base64Strict)})" +
"e=${e?.encodeToString(Base64Strict)}" +
")"
}
"JsonWebKey(" +
"type=$type, " +
"curve=$curve, " +
"keyId=$keyId," +
"x=${x?.encodeToString(Base64Strict)}," +
"y=${y?.encodeToString(Base64Strict)}" +
"n=${n?.encodeToString(Base64Strict)})" +
"e=${e?.encodeToString(Base64Strict)}" +
")"

fun toCryptoPublicKey(): CryptoPublicKey? =
when (type) {
Expand All @@ -153,9 +136,9 @@ data class JsonWebKey(
}

JwkType.RSA -> {
this.n?.let {
this.let {
CryptoPublicKey.Rsa(
n = it,
n = n ?: return null,
e = e?.let { bytes -> Int.decodeFromDer(bytes) } ?: return null
)
}
Expand All @@ -175,15 +158,15 @@ fun CryptoPublicKey.toJsonWebKey(): JsonWebKey =
curve = curve,
x = x,
y = y
)
).apply { jwkId = identifier }

is CryptoPublicKey.Rsa ->
JsonWebKey(
type = JwkType.RSA,
keyId = jwkId,
n = n,
e = e.encodeToByteArray()
)
).apply { jwkId = identifier }
}

private const val JWK_ID = "jwkIdentifier"
Expand Down
24 changes: 0 additions & 24 deletions datatypes-jws/src/jvmTest/kotlin/JsonWebKeyJvmTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,6 @@ class JsonWebKeyJvmTest : FreeSpec({
}
}

//Todo move to CryptoPublicKeyTest
// "JWK can be created from ANSI X962" - {
// val xFromBc = (keyPair.public as ECPublicKey).w.affineX.toByteArray().ensureSize(ecCurve.coordinateLengthBytes)
// val yFromBc = (keyPair.public as ECPublicKey).w.affineY.toByteArray().ensureSize(ecCurve.coordinateLengthBytes)
// val ansiX962 = byteArrayOf(0x04) + xFromBc + yFromBc
// val jsonWebKey = CryptoPublicKey.Ec.fromAnsiX963Bytes(ansiX962)!!.toJsonWebKey()
//
// jsonWebKey.shouldNotBeNull()
// jsonWebKey.x shouldBe xFromBc
// jsonWebKey.y shouldBe yFromBc
// jsonWebKey.keyId.shouldNotBeNull()
// jsonWebKey.keyId shouldHaveMinLength 32
// jsonWebKey.toAnsiX963ByteArray().getOrThrow() shouldBe ansiX962
//
// "it can be recreated" {
// val recreatedJwk = JsonWebKey.fromKeyId(jsonWebKey.keyId!!)
// recreatedJwk.shouldNotBeNull()
// recreatedJwk.keyId shouldBe jsonWebKey.keyId
// recreatedJwk.x shouldBe jsonWebKey.x
// recreatedJwk.y shouldBe jsonWebKey.y
// jsonWebKey.toAnsiX963ByteArray().getOrThrow() shouldBe ansiX962
// }
// }

"JWK can be created from n and e" - {
val nFromBc = (keyPairRSA.public as RSAPublicKey).modulus.toByteArray()
val eFromBc = (keyPairRSA.public as RSAPublicKey).publicExponent.toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ object MultibaseHelper {

private fun multibaseWrapBase64(it: ByteArray) = "m${it.encodeToString(Base64Strict)}"

// 0x1200 would be with compression, so we'll use 0x1290
private fun multicodecWrapEC(it: ByteArray) = byteArrayOf(0x12.toByte(), 0x90.toByte()) + it
private fun multicodecWrapRSA(it: ByteArray) = byteArrayOf(0x12.toByte(), 0x05.toByte()) + it

// 0x1200 would be with compression, so we'll use 0x1290
private fun multicodecWrapRSA(it: ByteArray) = byteArrayOf(0x12.toByte(), 0x05.toByte()) + it
private fun multicodecWrapEC(it: ByteArray) = byteArrayOf(0x12.toByte(), 0x90.toByte()) + it

// No compression, because decompression would need some EC math
private fun encodeEcKey(x: ByteArray, y: ByteArray, curve: EcCurve) =
Expand Down
4 changes: 2 additions & 2 deletions datatypes/src/jvmTest/kotlin/PublicKeyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class PublicKeyTest : FreeSpec({
}
}
}

"RSA" - {
withData(512, 1024, 2048, 3072, 4096) { bits ->
val keys = List<RSAPublicKey>(13000 / bits) {
Expand All @@ -70,7 +71,7 @@ class PublicKeyTest : FreeSpec({

val own = CryptoPublicKey.Rsa(pubKey.modulus.toByteArray(), pubKey.publicExponent.toInt())
val own1 = CryptoPublicKey.Rsa(
byteArrayOf(0, 0, 0) + pubKey.modulus.toByteArray(),
ByteArray((0..10).random()) { 0 } + pubKey.modulus.toByteArray(),
pubKey.publicExponent.toInt()
)

Expand All @@ -90,5 +91,4 @@ class PublicKeyTest : FreeSpec({
}
}
}

})

0 comments on commit 2a31187

Please sign in to comment.