@@ -14,6 +14,7 @@ import java.security.PublicKey
1414import java .security .interfaces .ECPublicKey
1515import java .security .interfaces .RSAPublicKey
1616import java .security .spec .PKCS8EncodedKeySpec
17+ import scala .util .Try
1718
1819/** Re-exports from [[WebAuthnCodecs ]] and [[Crypto ]] so tests can use it
1920 */
@@ -73,18 +74,15 @@ object WebAuthnTestCodecs {
7374
7475 def eddsaPublicKeyToCose (key : BCEdDSAPublicKey ): ByteArray = {
7576 val encoded = key.getEncoded
76- val (alg, crv, keyBytesLength) =
77- if (
78- encoded.length > 9 && encoded
79- .slice(2 , 9 )
80- .sameElements(WebAuthnCodecs .ED25519_ALG_ID .getBytes)
77+ val algOid : Array [Byte ] =
78+ Try (encoded.slice(2 , 9 )).getOrElse(
79+ throw new IllegalArgumentException (" Unknown EdDSA ASN.1 OID prefix" )
8180 )
81+
82+ val (alg, crv, keyBytesLength) =
83+ if (algOid.sameElements(WebAuthnCodecs .ED25519_ALG_ID .getBytes))
8284 (COSEAlgorithmIdentifier .EdDSA , COSE_CRV_ED25519 , 32 )
83- else if (
84- encoded.length > 9 && encoded
85- .slice(2 , 9 )
86- .sameElements(WebAuthnCodecs .ED448_ALG_ID .getBytes)
87- )
85+ else if (algOid.sameElements(WebAuthnCodecs .ED448_ALG_ID .getBytes))
8886 (COSEAlgorithmIdentifier .Ed448 , COSE_CRV_ED448 , 57 )
8987 else throw new IllegalArgumentException (" Unknown EdDSA ASN.1 OID prefix" )
9088
0 commit comments