michael-o commented on a change in pull request #2: URL: https://github.com/apache/maven-studies/pull/2#discussion_r499370719
########## File path: src/main/java/org/apache/maven/plugins/sign/pgp/PGPSigner.java ########## @@ -0,0 +1,155 @@ +package org.apache.maven.plugins.sign.pgp; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +import org.bouncycastle.bcpg.ArmoredOutputStream; +import org.bouncycastle.bcpg.BCPGOutputStream; +import org.bouncycastle.bcpg.HashAlgorithmTags; +import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPPrivateKey; +import org.bouncycastle.openpgp.PGPSecretKey; +import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; +import org.bouncycastle.openpgp.PGPSignature; +import org.bouncycastle.openpgp.PGPSignatureGenerator; +import org.bouncycastle.openpgp.PGPUtil; +import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * Signig data by PGP. + * + * @author Slawomir Jaranowski + */ +public class PGPSigner +{ + + private static final Logger LOGGER = LoggerFactory.getLogger( PGPSigner.class ); + + private final PGPSecretKeyInfo keyInfo; + + private PGPSecretKey secretKey; + private PGPPrivateKey pgpPrivateKey; + + public PGPSigner( PGPSecretKeyInfo keyInfo ) throws PGPSignerException + { + + this.keyInfo = keyInfo; + try + { + loadKey(); + } + catch ( IOException | PGPException e ) + { + throw new PGPSignerException( e ); + } + + List<String> uIds = new ArrayList<>(); + secretKey.getUserIDs().forEachRemaining( uIds::add ); + + LOGGER.info( "Loaded keyId: {}, uIds: {}", String.format( "%16X", secretKey.getKeyID() ), uIds ); + } + + /** + * Find and load private key form file. + */ + private void loadKey() throws IOException, PGPException, PGPSignerException + { + InputStream inputStream = PGPUtil.getDecoderStream( new FileInputStream( keyInfo.getFile() ) ); + PGPSecretKeyRingCollection pgpSecretKeyRingCollection = new PGPSecretKeyRingCollection( inputStream, + new JcaKeyFingerprintCalculator() ); + + Long keyId = keyInfo.getKeyId(); + if ( keyId != null ) + { + secretKey = pgpSecretKeyRingCollection.getSecretKey( keyId ); + } + else + { + // retrieve first master key + Iterator<PGPSecretKeyRing> keyRings = pgpSecretKeyRingCollection.getKeyRings(); Review comment: Interesting. I have always used the short format in my settings file. Which of the are unique? We should stick to the unique ones... ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org