Add more tests for V6 fingerprints

This commit is contained in:
Paul Schaub 2023-08-01 15:29:24 +02:00
parent 15af265e3f
commit 8cdb7ee4e0
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
2 changed files with 85 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.encoders.Hex;
@ -22,6 +23,17 @@ public abstract class OpenPgpFingerprint implements CharSequence, Comparable<Ope
protected static final Charset utf8 = Charset.forName("UTF-8");
protected final String fingerprint;
/**
* Return the fingerprint of the given key.
* This method automatically matches key versions to fingerprint implementations.
*
* @param key key
* @return fingerprint
*/
public static OpenPgpFingerprint of(PGPSecretKey key) {
return of(key.getPublicKey());
}
/**
* Return the fingerprint of the given key.
* This method automatically matches key versions to fingerprint implementations.

View File

@ -11,9 +11,14 @@ import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.encoders.Hex;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import java.io.IOException;
import java.util.Iterator;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
@ -143,6 +148,74 @@ public class OpenPgpV6FingerprintTest {
assertEquals(hex, fingerprint.toString());
}
@Test
public void fromSampleV6Certificate() throws IOException {
String CERT = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
"\n" +
"xioGY4d/4xsAAAAg+U2nu0jWCmHlZ3BqZYfQMxmZu52JGggkLq2EVD34laPCsQYf\n" +
"GwoAAABCBYJjh3/jAwsJBwUVCg4IDAIWAAKbAwIeCSIhBssYbE8GCaaX5NUt+mxy\n" +
"KwwfHifBilZwj2Ul7Ce62azJBScJAgcCAAAAAK0oIBA+LX0ifsDm185Ecds2v8lw\n" +
"gyU2kCcUmKfvBXbAf6rhRYWzuQOwEn7E/aLwIwRaLsdry0+VcallHhSu4RN6HWaE\n" +
"QsiPlR4zxP/TP7mhfVEe7XWPxtnMUMtf15OyA51YBM4qBmOHf+MZAAAAIIaTJINn\n" +
"+eUBXbki+PSAld2nhJh/LVmFsS+60WyvXkQ1wpsGGBsKAAAALAWCY4d/4wKbDCIh\n" +
"BssYbE8GCaaX5NUt+mxyKwwfHifBilZwj2Ul7Ce62azJAAAAAAQBIKbpGG2dWTX8\n" +
"j+VjFM21J0hqWlEg+bdiojWnKfA5AQpWUWtnNwDEM0g12vYxoWM8Y81W+bHBw805\n" +
"I8kWVkXU6vFOi+HWvv/ira7ofJu16NnoUkhclkUrk0mXubZvyl4GBg==\n" +
"-----END PGP PUBLIC KEY BLOCK-----";
PGPPublicKeyRing cert = PGPainless.readKeyRing().publicKeyRing(CERT);
assertNotNull(cert);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.of(cert);
assertEquals(6, fingerprint.getVersion());
assertEquals("CB186C4F0609A697E4D52DFA6C722B0C1F1E27C18A56708F6525EC27BAD9ACC9", fingerprint.toString());
Iterator<PGPPublicKey> keys = cert.getPublicKeys();
fingerprint = OpenPgpFingerprint.of(keys.next());
assertEquals(6, fingerprint.getVersion());
assertEquals("CB186C4F0609A697E4D52DFA6C722B0C1F1E27C18A56708F6525EC27BAD9ACC9",
fingerprint.toString());
fingerprint = OpenPgpFingerprint.of(keys.next());
assertEquals(6, fingerprint.getVersion());
assertEquals("12C83F1E706F6308FE151A417743A1F033790E93E9978488D1DB378DA9930885",
fingerprint.toString());
}
@Test
public void fromSampleV6SecretKey() throws IOException {
String KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
"\n" +
"xUsGY4d/4xsAAAAg+U2nu0jWCmHlZ3BqZYfQMxmZu52JGggkLq2EVD34laMAGXKB\n" +
"exK+cH6NX1hs5hNhIB00TrJmosgv3mg1ditlsLfCsQYfGwoAAABCBYJjh3/jAwsJ\n" +
"BwUVCg4IDAIWAAKbAwIeCSIhBssYbE8GCaaX5NUt+mxyKwwfHifBilZwj2Ul7Ce6\n" +
"2azJBScJAgcCAAAAAK0oIBA+LX0ifsDm185Ecds2v8lwgyU2kCcUmKfvBXbAf6rh\n" +
"RYWzuQOwEn7E/aLwIwRaLsdry0+VcallHhSu4RN6HWaEQsiPlR4zxP/TP7mhfVEe\n" +
"7XWPxtnMUMtf15OyA51YBMdLBmOHf+MZAAAAIIaTJINn+eUBXbki+PSAld2nhJh/\n" +
"LVmFsS+60WyvXkQ1AE1gCk95TUR3XFeibg/u/tVY6a//1q0NWC1X+yui3O24wpsG\n" +
"GBsKAAAALAWCY4d/4wKbDCIhBssYbE8GCaaX5NUt+mxyKwwfHifBilZwj2Ul7Ce6\n" +
"2azJAAAAAAQBIKbpGG2dWTX8j+VjFM21J0hqWlEg+bdiojWnKfA5AQpWUWtnNwDE\n" +
"M0g12vYxoWM8Y81W+bHBw805I8kWVkXU6vFOi+HWvv/ira7ofJu16NnoUkhclkUr\n" +
"k0mXubZvyl4GBg==\n" +
"-----END PGP PRIVATE KEY BLOCK-----";
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
assertNotNull(secretKeys);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.of(secretKeys);
assertEquals(6, fingerprint.getVersion());
assertEquals("CB186C4F0609A697E4D52DFA6C722B0C1F1E27C18A56708F6525EC27BAD9ACC9", fingerprint.toString());
Iterator<PGPSecretKey> keys = secretKeys.getSecretKeys();
fingerprint = OpenPgpFingerprint.of(keys.next());
assertEquals(6, fingerprint.getVersion());
assertEquals("CB186C4F0609A697E4D52DFA6C722B0C1F1E27C18A56708F6525EC27BAD9ACC9",
fingerprint.toString());
fingerprint = OpenPgpFingerprint.of(keys.next());
assertEquals(6, fingerprint.getVersion());
assertEquals("12C83F1E706F6308FE151A417743A1F033790E93E9978488D1DB378DA9930885",
fingerprint.toString());
}
private PGPPublicKey getMockedPublicKey(String hex) {
byte[] binary = Hex.decode(hex);