public void prepare()

in src/main/java/org/apache/maven/plugins/gpg/BcSigner.java [280:366]


    public void prepare() throws MojoFailureException {
        try {
            List<Loader> loaders = Stream.of(new GpgEnvLoader(), new GpgConfLoader(), new GpgAgentPasswordLoader())
                    .collect(Collectors.toList());

            byte[] keyRingMaterial = null;
            for (Loader loader : loaders) {
                keyRingMaterial = loader.loadKeyRingMaterial(session);
                if (keyRingMaterial != null) {
                    break;
                }
            }
            if (keyRingMaterial == null) {
                throw new MojoFailureException("Key ring material not found");
            }

            byte[] fingerprint = null;
            for (Loader loader : loaders) {
                fingerprint = loader.loadKeyFingerprint(session);
                if (fingerprint != null) {
                    break;
                }
            }

            PGPSecretKeyRingCollection pgpSecretKeyRingCollection = new PGPSecretKeyRingCollection(
                    PGPUtil.getDecoderStream(new ByteArrayInputStream(keyRingMaterial)),
                    new BcKeyFingerprintCalculator());

            PGPSecretKey secretKey = null;
            for (PGPSecretKeyRing ring : pgpSecretKeyRingCollection) {
                for (PGPSecretKey key : ring) {
                    if (!key.isPrivateKeyEmpty()) {
                        if (fingerprint == null || Arrays.equals(fingerprint, key.getFingerprint())) {
                            secretKey = key;
                            break;
                        }
                    }
                }
            }
            if (secretKey == null) {
                throw new MojoFailureException("Secret key not found");
            }
            if (secretKey.isPrivateKeyEmpty()) {
                throw new MojoFailureException("Private key not found in Secret key");
            }

            long validSeconds = secretKey.getPublicKey().getValidSeconds();
            if (validSeconds > 0) {
                LocalDateTime expireDateTime = secretKey
                        .getPublicKey()
                        .getCreationTime()
                        .toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDateTime()
                        .plusSeconds(validSeconds);
                if (LocalDateTime.now().isAfter(expireDateTime)) {
                    throw new MojoFailureException("Secret key expired at: " + expireDateTime);
                }
            }

            char[] keyPassword = passphrase != null ? passphrase.toCharArray() : null;
            final boolean keyPassNeeded = secretKey.getKeyEncryptionAlgorithm() != SymmetricKeyAlgorithmTags.NULL;
            if (keyPassNeeded && keyPassword == null) {
                for (Loader loader : loaders) {
                    keyPassword = loader.loadPassword(session, secretKey.getFingerprint());
                    if (keyPassword != null) {
                        break;
                    }
                }
                if (keyPassword == null) {
                    throw new MojoFailureException("Secret key is encrypted but no passphrase provided");
                }
            }

            this.secretKey = secretKey;
            this.privateKey = secretKey.extractPrivateKey(
                    new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(keyPassword));
            if (keyPassword != null) {
                Arrays.fill(keyPassword, ' ');
            }
            PGPSignatureSubpacketGenerator subPacketGenerator = new PGPSignatureSubpacketGenerator();
            subPacketGenerator.setIssuerFingerprint(false, secretKey);
            this.hashSubPackets = subPacketGenerator.generate();
        } catch (PGPException | IOException e) {
            throw new MojoFailureException(e);
        }
    }