Skip to content

Commit 7cfde6b

Browse files
ligefeiBouncycastledghgit
authored andcommitted
Update KeyIdentifier and related classes, update FingerprintUtil, remove OpenPGPV6KeyGenerator
1 parent 4817b8e commit 7cfde6b

File tree

9 files changed

+71
-1261
lines changed

9 files changed

+71
-1261
lines changed

pg/src/main/java/org/bouncycastle/bcpg/FingerprintUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.bouncycastle.util.Pack;
44
import org.bouncycastle.util.encoders.Hex;
55

6+
import java.util.Locale;
7+
68
public class FingerprintUtil
79
{
810

@@ -141,7 +143,7 @@ public static void writeKeyID(long keyID, byte[] bytes)
141143
public static String prettifyFingerprint(byte[] fingerprint)
142144
{
143145
// -DM Hex.toHexString
144-
char[] hex = Hex.toHexString(fingerprint).toUpperCase().toCharArray();
146+
char[] hex = Hex.toHexString(fingerprint).toUpperCase(Locale.getDefault()).toCharArray();
145147
StringBuilder sb = new StringBuilder();
146148
switch (hex.length)
147149
{

pg/src/main/java/org/bouncycastle/bcpg/KeyIdentifier.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ public boolean matches(KeyIdentifier other)
170170
return true;
171171
}
172172

173+
return matchesExplicit(other);
174+
}
175+
176+
public boolean matchesExplicit(KeyIdentifier other)
177+
{
173178
if (fingerprint != null && other.fingerprint != null)
174179
{
175180
return Arrays.constantTimeAreEqual(fingerprint, other.fingerprint);
@@ -214,7 +219,7 @@ public boolean isPresentIn(List<KeyIdentifier> others)
214219
{
215220
for (Iterator it = others.iterator(); it.hasNext();)
216221
{
217-
if (this.matches((KeyIdentifier)it.next()))
222+
if (this.matchesExplicit((KeyIdentifier)it.next()))
218223
{
219224
return true;
220225
}

pg/src/main/java/org/bouncycastle/openpgp/PGPPublicKeyRing.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public PGPPublicKey getPublicKey(KeyIdentifier identifier)
201201
for (Iterator it = keys.iterator(); it.hasNext();)
202202
{
203203
PGPPublicKey k = (PGPPublicKey)it.next();
204-
if (identifier.matches(k.getKeyIdentifier()))
204+
if (identifier.matchesExplicit(k.getKeyIdentifier()))
205205
{
206206
return k;
207207
}
@@ -216,7 +216,7 @@ public Iterator<PGPPublicKey> getPublicKeys(KeyIdentifier identifier)
216216
for (Iterator it = keys.iterator(); it.hasNext();)
217217
{
218218
PGPPublicKey k = (PGPPublicKey)it.next();
219-
if (identifier.matches(k.getKeyIdentifier()))
219+
if (identifier.matchesExplicit(k.getKeyIdentifier()))
220220
{
221221
matches.add(k);
222222
}

pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKeyRing.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public PGPPublicKey getPublicKey(KeyIdentifier identifier)
263263
for (Iterator it = keys.iterator(); it.hasNext();)
264264
{
265265
PGPSecretKey k = (PGPSecretKey)it.next();
266-
if (k.getPublicKey() != null && identifier.matches(k.getKeyIdentifier()))
266+
if (k.getPublicKey() != null && identifier.matchesExplicit(k.getKeyIdentifier()))
267267
{
268268
return k.getPublicKey();
269269
}
@@ -272,7 +272,7 @@ public PGPPublicKey getPublicKey(KeyIdentifier identifier)
272272
for (Iterator it = extraPubKeys.iterator(); it.hasNext();)
273273
{
274274
PGPPublicKey k = (PGPPublicKey)it.next();
275-
if (identifier.matches(k.getKeyIdentifier()))
275+
if (identifier.matchesExplicit(k.getKeyIdentifier()))
276276
{
277277
return k;
278278
}
@@ -287,7 +287,7 @@ public Iterator<PGPPublicKey> getPublicKeys(KeyIdentifier identifier)
287287
for (Iterator it = keys.iterator(); it.hasNext();)
288288
{
289289
PGPSecretKey k = (PGPSecretKey)it.next();
290-
if (k.getPublicKey() != null && identifier.matches(k.getKeyIdentifier()))
290+
if (k.getPublicKey() != null && identifier.matchesExplicit(k.getKeyIdentifier()))
291291
{
292292
matches.add(k.getPublicKey());
293293
}
@@ -296,7 +296,7 @@ public Iterator<PGPPublicKey> getPublicKeys(KeyIdentifier identifier)
296296
for (Iterator it = extraPubKeys.iterator(); it.hasNext();)
297297
{
298298
PGPPublicKey k = (PGPPublicKey)it.next();
299-
if (identifier.matches(k.getKeyIdentifier()))
299+
if (identifier.matchesExplicit(k.getKeyIdentifier()))
300300
{
301301
matches.add(k);
302302
}
@@ -309,7 +309,7 @@ public PGPSecretKey getSecretKey(KeyIdentifier identifier)
309309
for (Iterator it = keys.iterator(); it.hasNext();)
310310
{
311311
PGPSecretKey k = (PGPSecretKey)it.next();
312-
if (identifier.matches(k.getKeyIdentifier()))
312+
if (identifier.matchesExplicit(k.getKeyIdentifier()))
313313
{
314314
return k;
315315
}
@@ -323,7 +323,7 @@ public Iterator<PGPSecretKey> getSecretKeys(KeyIdentifier identifier)
323323
for (Iterator it = keys.iterator(); it.hasNext();)
324324
{
325325
PGPSecretKey k = (PGPSecretKey)it.next();
326-
if (identifier.matches(k.getKeyIdentifier()))
326+
if (identifier.matchesExplicit(k.getKeyIdentifier()))
327327
{
328328
matches.add(k);
329329
}

pg/src/main/java/org/bouncycastle/openpgp/PGPSignature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ public boolean hasKeyIdentifier(KeyIdentifier identifier)
654654
{
655655
for (Iterator it = getKeyIdentifiers().iterator(); it.hasNext(); )
656656
{
657-
if (((KeyIdentifier)it.next()).matches(identifier))
657+
if (((KeyIdentifier)it.next()).matchesExplicit(identifier))
658658
{
659659
return true;
660660
}

pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPCertificate.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public boolean test(OpenPGPComponentKey key, Date time)
303303
*/
304304
public OpenPGPComponentKey getKey(KeyIdentifier identifier)
305305
{
306-
if (identifier.matches(getPrimaryKey().getPGPPublicKey().getKeyIdentifier()))
306+
if (identifier.matchesExplicit(getPrimaryKey().getPGPPublicKey().getKeyIdentifier()))
307307
{
308308
return primaryKey;
309309
}
@@ -636,7 +636,6 @@ public String toAsciiArmoredString()
636636
public String toAsciiArmoredString(PacketFormat packetFormat)
637637
throws IOException
638638
{
639-
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
640639
ArmoredOutputStream.Builder armorBuilder = ArmoredOutputStream.builder()
641640
.clearHeaders();
642641
// Add fingerprint comment
@@ -648,7 +647,24 @@ public String toAsciiArmoredString(PacketFormat packetFormat)
648647
armorBuilder.addEllipsizedComment(it.next().getUserId());
649648
}
650649

650+
return toAsciiArmoredString(packetFormat, armorBuilder);
651+
}
652+
653+
/**
654+
* Return an ASCII armored {@link String} containing the certificate.
655+
* The {@link ArmoredOutputStream.Builder} can be used to customize the ASCII armor (headers, CRC etc.).
656+
*
657+
* @param packetFormat packet length encoding format
658+
* @param armorBuilder builder for the ASCII armored output stream
659+
* @return armored certificate
660+
* @throws IOException if the cert cannot be encoded
661+
*/
662+
public String toAsciiArmoredString(PacketFormat packetFormat, ArmoredOutputStream.Builder armorBuilder)
663+
throws IOException
664+
{
665+
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
651666
ArmoredOutputStream aOut = armorBuilder.build(bOut);
667+
652668
aOut.write(getEncoded(packetFormat));
653669
aOut.close();
654670
return bOut.toString();

0 commit comments

Comments
 (0)