pgpainless/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyTest.java

64 lines
2.4 KiB
Java
Raw Normal View History

2020-01-12 19:17:58 +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.generation;
2020-01-12 19:17:58 +01:00
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
2020-04-11 11:35:48 +02:00
import java.util.logging.Level;
import java.util.logging.Logger;
2020-01-12 19:17:58 +01:00
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPException;
2020-11-13 16:31:59 +01:00
import org.junit.jupiter.api.Test;
2020-01-12 19:17:58 +01:00
import org.pgpainless.PGPainless;
import org.pgpainless.key.OpenPgpV4Fingerprint;
2020-01-12 19:17:58 +01:00
import org.pgpainless.key.collection.PGPKeyRing;
public class GenerateKeyTest {
2020-04-11 11:35:48 +02:00
private static final Logger LOGGER = Logger.getLogger(GenerateKeyTest.class.getName());
2020-01-12 19:17:58 +01:00
@Test
public void generateKey() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
2020-04-11 11:35:48 +02:00
PGPKeyRing keyRing = PGPainless.generateKeyRing().simpleEcKeyRing("fresh@encrypted.key", "password123");
2020-01-12 19:17:58 +01:00
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ArmoredOutputStream armor = new ArmoredOutputStream(bytes);
keyRing.getPublicKeys().encode(armor);
armor.close();
2020-04-11 11:35:48 +02:00
String publicKey = new String(bytes.toByteArray());
2020-01-12 19:17:58 +01:00
bytes = new ByteArrayOutputStream();
armor = new ArmoredOutputStream(bytes);
keyRing.getSecretKeys().encode(armor);
armor.close();
2020-04-11 11:35:48 +02:00
String privateKey = new String(bytes.toByteArray());
LOGGER.log(Level.INFO, String.format("Generated random fresh EC key ring.\n" +
"User-ID: %s\n" +
"Fingerprint: %s\n" +
"Key-ID: %s\n" +
"%s\n" +
"%s\n", keyRing.getPublicKeys().getPublicKey().getUserIDs().next(),
new OpenPgpV4Fingerprint(keyRing.getPublicKeys()),
keyRing.getPublicKeys().getPublicKey().getKeyID(),
publicKey, privateKey));
2020-01-12 19:17:58 +01:00
}
}