Exemple #1
0
 @Override
 public String toString() {
   byte[] data = getKeyData();
   return getKeyType()
       + " "
       + (NumberUtils.isEmpty(data) ? "<no-key>" : Base64.encodeToString(data));
 }
Exemple #2
0
  /**
   * Encodes a public key data the same way as the {@link #parsePublicKeyEntry(String)} expects it
   *
   * @param <A> The generic appendable class
   * @param sb The {@link Appendable} instance to encode the data into
   * @param key The {@link PublicKey} - ignored if {@code null}
   * @return The updated appendable instance
   * @throws IOException If failed to append the data
   */
  public static <A extends Appendable> A appendPublicKeyEntry(A sb, PublicKey key)
      throws IOException {
    if (key == null) {
      return sb;
    }

    @SuppressWarnings("unchecked")
    PublicKeyEntryDecoder<PublicKey, ?> decoder =
        (PublicKeyEntryDecoder<PublicKey, ?>) KeyUtils.getPublicKeyEntryDecoder(key);
    if (decoder == null) {
      throw new StreamCorruptedException("Cannot retrieve decoder for key=" + key.getAlgorithm());
    }

    try (ByteArrayOutputStream s = new ByteArrayOutputStream(Byte.MAX_VALUE)) {
      String keyType = decoder.encodePublicKey(s, key);
      byte[] bytes = s.toByteArray();
      String b64Data = Base64.encodeToString(bytes);
      sb.append(keyType).append(' ').append(b64Data);
    }

    return sb;
  }