// Path: src/main/java/org/simplify4u/plugins/utils/HexUtils.java
// @UtilityClass
// public class HexUtils {
// 
//     /**
//      * Convert byte array of fingerprint to string of hex
//      * @param bytes fingerprint
//      * @return fingerprint as string
//      */
//     public static String fingerprintToString(byte[] bytes) {
//         StringBuilder ret = new StringBuilder();
//         ret.append("0x");
//         for (byte b : bytes) {
//             ret.append(String.format("%02X", b));
//         }
//         return ret.toString();
//     }
// 
//     /**
//      * Convert fingerprint in string format to byte array.
//      * @param key fingerprint as string
//      * @return fingerprint as byte array
//      */
//     public static byte[] stringToFingerprint(String key) {
//         byte[] bytes;
// 
//         try {
//             bytes = Hex.decode(key.substring(2));
//         } catch (DecoderException e) {
//             throw new IllegalArgumentException("Malformed keyID hex string " + key, e);
//         }
// 
//         if (bytes.length < 8 || bytes.length > 20) {
//             throw new IllegalArgumentException(
//                     String.format("Key length for = %s is %d bits, should be between 64 and 160 bits",
//                             key, bytes.length * 8));
//         }
// 
//         return bytes;
//     }
// }


import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import io.vavr.control.Try;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
import org.simplify4u.plugins.utils.HexUtils;
/*
 * Copyright 2020-2021 Slawomir Jaranowski
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.simplify4u.plugins.pgp;



/**
 * Utility for PGPPublicKey
 */
@Slf4j
public final class PublicKeyUtils {

    private PublicKeyUtils() {
        // No need to instantiate utility class.
    }

    /**
     * Generate string version of key fingerprint
     *
     * @param publicKey given key
     *
     * @return fingerprint as string
     */
    static String fingerprint(PGPPublicKey publicKey) {
// Path: src/main/java/org/simplify4u/plugins/utils/HexUtils.java
// @UtilityClass
// public class HexUtils {
// 
//     /**
//      * Convert byte array of fingerprint to string of hex
//      * @param bytes fingerprint
//      * @return fingerprint as string
//      */
//     public static String fingerprintToString(byte[] bytes) {
//         StringBuilder ret = new StringBuilder();
//         ret.append("0x");
//         for (byte b : bytes) {
//             ret.append(String.format("%02X", b));
//         }
//         return ret.toString();
//     }
// 
//     /**
//      * Convert fingerprint in string format to byte array.
//      * @param key fingerprint as string
//      * @return fingerprint as byte array
//      */
//     public static byte[] stringToFingerprint(String key) {
//         byte[] bytes;
// 
//         try {
//             bytes = Hex.decode(key.substring(2));
//         } catch (DecoderException e) {
//             throw new IllegalArgumentException("Malformed keyID hex string " + key, e);
//         }
// 
//         if (bytes.length < 8 || bytes.length > 20) {
//             throw new IllegalArgumentException(
//                     String.format("Key length for = %s is %d bits, should be between 64 and 160 bits",
//                             key, bytes.length * 8));
//         }
// 
//         return bytes;
//     }
// }


// Path: src/main/java/org/simplify4u/plugins/pgp/PublicKeyUtils.java
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import io.vavr.control.Try;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
import org.simplify4u.plugins.utils.HexUtils;

/*
 * Copyright 2020-2021 Slawomir Jaranowski
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.simplify4u.plugins.pgp;



/**
 * Utility for PGPPublicKey
 */
@Slf4j
public final class PublicKeyUtils {

    private PublicKeyUtils() {
        // No need to instantiate utility class.
    }

    /**
     * Generate string version of key fingerprint
     *
     * @param publicKey given key
     *
     * @return fingerprint as string
     */
    static String fingerprint(PGPPublicKey publicKey) {
        return HexUtils.fingerprintToString(publicKey.getFingerprint());