java_src/src/main/java/com/google/crypto/tink/jwt/JwkSetConverter.java
/**
   * Converts a Json Web Key (JWK) set into a Tink KeysetHandle.
   *
   * <p>It requires that all keys in the set have the "algo" field set. Currently, only public keys
   * for algorithms ES256, ES384, ES512, RS256, RS384 and RS512 are supported. JWK is defined in
   * https://www.rfc-editor.org/rfc/rfc7517.txt.
   */
Converts a Json Web Key (JWK) set into a Tink KeysetHandle.
It requires that all keys in the set have the "algo" field set.
Converts a Json Web Key ( JWK ) set into a Tink KeysetHandle . It requires that all keys in the set have the " algo " field set .
public static KeysetHandle toKeysetHandle(String jwkSet, KeyAccess keyAccess)
      throws IOException, GeneralSecurityException {
    JsonObject jsonKeyset;
    try {
      JsonReader jsonReader = new JsonReader(new StringReader(jwkSet));
      jsonReader.setLenient(false);
      jsonKeyset = Streams.parse(jsonReader).getAsJsonObject();
    } catch (IllegalStateException | JsonParseException | StackOverflowError ex) {
      throw new IOException("JWK set is invalid JSON", ex);
    }
    KeysetManager manager = KeysetManager.withEmptyKeyset();
    JsonArray jsonKeys = jsonKeyset.get("keys").getAsJsonArray();
    for (JsonElement element : jsonKeys) {
      JsonObject jsonKey = element.getAsJsonObject();
      String algPrefix = getStringItem(jsonKey, "alg").substring(0, 2);
      KeyData keyData;
      switch (algPrefix) {
        case "RS":
          keyData = convertToRsaSsaPkcs1Key(jsonKey);
          break;
        case "PS":
          keyData = convertToRsaSsaPssKey(jsonKey);
          break;
        case "ES":
          keyData = convertToEcdsaKey(jsonKey);
          break;
        default:
          throw new IOException("unexpected alg value: " + getStringItem(jsonKey, "alg"));
      }
      manager.add(
          KeyHandle.createFromKey(
              new ProtoKey(keyData, com.google.crypto.tink.KeyTemplate.OutputPrefixType.RAW),
              keyAccess));
    }
    KeysetInfo info = manager.getKeysetHandle().getKeysetInfo();
    if (info.getKeyInfoCount() <= 0) {
      throw new IOException("empty keyset");
    }
    manager.setPrimary(info.getKeyInfo(0).getKeyId());
    return manager.getKeysetHandle();
  }
public static KeysetHandle toKeysetHandle ( String jwkSet , KeyAccess keyAccess ) throws IOException , GeneralSecurityException { JsonObject jsonKeyset ; try { JsonReader jsonReader = new JsonReader ( new StringReader ( jwkSet ) ) ; jsonReader . setLenient ( false ) ; jsonKeyset = Streams . parse ( jsonReader ) . getAsJsonObject ( ) ; } catch ( IllegalStateException | JsonParseException | StackOverflowError ex ) { throw new IOException ( "JWK set is invalid JSON" , ex ) ; } KeysetManager manager = KeysetManager . withEmptyKeyset ( ) ; JsonArray jsonKeys = jsonKeyset . get ( "keys" ) . getAsJsonArray ( ) ; for ( JsonElement element : jsonKeys ) { JsonObject jsonKey = element . getAsJsonObject ( ) ; String algPrefix = getStringItem ( jsonKey , "alg" ) . substring ( 0 , 2 ) ; KeyData keyData ; switch ( algPrefix ) { case "RS" : keyData = convertToRsaSsaPkcs1Key ( jsonKey ) ; break ; case "PS" : keyData = convertToRsaSsaPssKey ( jsonKey ) ; break ; case "ES" : keyData = convertToEcdsaKey ( jsonKey ) ; break ; default : throw new IOException ( "unexpected alg value: " + getStringItem ( jsonKey , "alg" ) ) ; } manager . add ( KeyHandle . createFromKey ( new ProtoKey ( keyData , com . google . crypto . tink . KeyTemplate . OutputPrefixType . RAW ) , keyAccess ) ) ; } KeysetInfo info = manager . getKeysetHandle ( ) . getKeysetInfo ( ) ; if ( info . getKeyInfoCount ( ) <= 0 ) { throw new IOException ( "empty keyset" ) ; } manager . setPrimary ( info . getKeyInfo ( 0 ) . getKeyId ( ) ) ; return manager . getKeysetHandle ( ) ; }
Converts a Json Web Key (JWK) set into a Tink KeysetHandle.
Converts a Json Web Key ( JWK ) set into a Tink KeysetHandle .