2020-11-13 14:32:29 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Paul Schaub.
|
|
|
|
*
|
|
|
|
* 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.pgpainless.key.util;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-11-29 16:04:15 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
import org.bouncycastle.openpgp.PGPPrivateKey;
|
2020-11-13 14:32:29 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
2020-11-29 16:04:15 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKey;
|
2020-11-13 14:32:29 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
2020-11-29 16:04:15 +01:00
|
|
|
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
|
|
|
|
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
2020-11-13 14:32:29 +01:00
|
|
|
|
|
|
|
public class KeyRingUtils {
|
|
|
|
|
|
|
|
public static PGPPublicKeyRing publicKeyRingFrom(PGPSecretKeyRing secretKeys) {
|
|
|
|
List<PGPPublicKey> publicKeyList = new ArrayList<>();
|
|
|
|
Iterator<PGPPublicKey> publicKeyIterator = secretKeys.getPublicKeys();
|
|
|
|
while (publicKeyIterator.hasNext()) {
|
|
|
|
publicKeyList.add(publicKeyIterator.next());
|
|
|
|
}
|
|
|
|
PGPPublicKeyRing publicKeyRing = new PGPPublicKeyRing(publicKeyList);
|
|
|
|
return publicKeyRing;
|
|
|
|
}
|
2020-11-29 16:04:15 +01:00
|
|
|
|
|
|
|
public static PGPPrivateKey unlockSecretKey(PGPSecretKey secretKey, SecretKeyRingProtector protector) throws PGPException {
|
|
|
|
PBESecretKeyDecryptor secretKeyDecryptor = protector.getDecryptor(secretKey.getKeyID());
|
|
|
|
PGPPrivateKey privateKey = secretKey.extractPrivateKey(secretKeyDecryptor);
|
|
|
|
return privateKey;
|
|
|
|
}
|
2020-11-13 14:32:29 +01:00
|
|
|
}
|