JoseUtils { public static String keyAlgorithm(JsonWebKey jwk) { if (jwk instanceof EllipticCurveJsonWebKey) { EllipticCurveJsonWebKey ecjwk = (EllipticCurveJsonWebKey) jwk; switch (ecjwk.getCurveName()) { case "P-256": return AlgorithmIdentifiers.ECDSA_USING_P256_CURVE_AND_SHA256; case "P-384": return AlgorithmIdentifiers.ECDSA_USING_P384_CURVE_AND_SHA384; case "P-521": return AlgorithmIdentifiers.ECDSA_USING_P521_CURVE_AND_SHA512; default: throw new IllegalArgumentException("Unknown EC name " + ecjwk.getCurveName()); } } else if (jwk instanceof RsaJsonWebKey) { return AlgorithmIdentifiers.RSA_USING_SHA256; } else { throw new IllegalArgumentException("Unknown algorithm " + jwk.getAlgorithm()); } } private  JoseUtils(); static JSONBuilder createJoseRequest(URL url, KeyPair keypair,
                @Nullable JSONBuilder payload, @Nullable String nonce, @Nullable String kid); static Map<String, Object> createExternalAccountBinding(String kid,
            PublicKey accountKey, SecretKey macKey, URL resource); static Map<String, Object> publicKeyToJWK(PublicKey key); static PublicKey jwkToPublicKey(Map<String, Object> jwk); static byte[] thumbprint(PublicKey key); static String keyAlgorithm(JsonWebKey jwk); static String macKeyAlgorithm(SecretKey macKey);  }
@Test public void testRsaKey() throws Exception { KeyPair rsaKeyPair = TestUtils.createKeyPair(); final PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(rsaKeyPair.getPublic()); String type = JoseUtils.keyAlgorithm(jwk); assertThat(type, is("RS256")); }
@Test public void testP256ECKey() throws Exception { KeyPair ecKeyPair = TestUtils.createECKeyPair("secp256r1"); final PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(ecKeyPair.getPublic()); String type = JoseUtils.keyAlgorithm(jwk); assertThat(type, is("ES256")); }
@Test public void testP384ECKey() throws Exception { KeyPair ecKeyPair = TestUtils.createECKeyPair("secp384r1"); final PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(ecKeyPair.getPublic()); String type = JoseUtils.keyAlgorithm(jwk); assertThat(type, is("ES384")); }
@Test public void testP521ECKey() throws Exception { KeyPair ecKeyPair = TestUtils.createECKeyPair("secp521r1"); final PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(ecKeyPair.getPublic()); String type = JoseUtils.keyAlgorithm(jwk); assertThat(type, is("ES512")); }