Skip to content

Commit aabba7e

Browse files
committed
Merge branch '1911-0-1-keyIdentifier' into 'main'
Update of KeyIdentifier class and test See merge request root/bc-java!91
2 parents ffa16b1 + d01a0d6 commit aabba7e

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Iterator;
44
import java.util.List;
5+
import java.util.Locale;
56

67
import org.bouncycastle.util.Arrays;
78
import org.bouncycastle.util.encoders.Hex;
@@ -31,6 +32,14 @@ public KeyIdentifier(String hexEncoded)
3132
*/
3233
public KeyIdentifier(byte[] fingerprint)
3334
{
35+
// Long KeyID
36+
if (fingerprint.length == 8)
37+
{
38+
keyId = FingerprintUtil.longFromRightMostBytes(fingerprint);
39+
this.fingerprint = null;
40+
return;
41+
}
42+
3443
this.fingerprint = Arrays.clone(fingerprint);
3544

3645
// v4
@@ -69,15 +78,24 @@ public KeyIdentifier(byte[] fingerprint, long keyId)
6978
*/
7079
public KeyIdentifier(long keyId)
7180
{
72-
this(null, keyId);
81+
if (keyId == 0L)
82+
{
83+
this.keyId = 0L;
84+
this.fingerprint = new byte[0];
85+
}
86+
else
87+
{
88+
this.keyId = keyId;
89+
this.fingerprint = null;
90+
}
7391
}
7492

7593
/**
7694
* Create a wildcard {@link KeyIdentifier}.
7795
*/
7896
private KeyIdentifier()
7997
{
80-
this(new byte[0], 0L);
98+
this(0L);
8199
}
82100

83101
/**
@@ -123,7 +141,7 @@ public long getKeyId()
123141
*/
124142
public boolean isWildcard()
125143
{
126-
return keyId == 0L && fingerprint.length == 0;
144+
return keyId == 0L && (fingerprint == null || fingerprint.length == 0);
127145
}
128146

129147
/**
@@ -247,6 +265,19 @@ public String toString()
247265
}
248266

249267
// -DM Hex.toHexString
250-
return Hex.toHexString(fingerprint).toUpperCase();
268+
return Hex.toHexString(fingerprint).toUpperCase(Locale.getDefault());
269+
}
270+
271+
public String toPrettyPrint()
272+
{
273+
if (isWildcard())
274+
{
275+
return "*";
276+
}
277+
if (fingerprint == null)
278+
{
279+
return "0x" + Long.toHexString(keyId).toUpperCase(Locale.getDefault());
280+
}
281+
return FingerprintUtil.prettifyFingerprint(fingerprint);
251282
}
252283
}

pg/src/test/java/org/bouncycastle/openpgp/test/KeyIdentifierTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public void performTest()
3939
testWildcardIdentifier();
4040
testIdentifierFromKeyId();
4141

42+
testIdentifierFromLongKeyId();
43+
4244
testIdentifierFromV4Fingerprint();
4345
testIdentifierFromV6Fingerprint();
4446

@@ -57,6 +59,9 @@ private void testWildcardIdentifier()
5759
wildcard.isWildcard());
5860

5961
isEquals("*", wildcard.toString());
62+
63+
KeyIdentifier id = new KeyIdentifier(0L);
64+
isTrue(id.isWildcard());
6065
}
6166

6267
private void testIdentifierFromKeyId()
@@ -70,6 +75,17 @@ private void testIdentifierFromKeyId()
7075
isEquals("1234", identifier.toString());
7176
}
7277

78+
private void testIdentifierFromLongKeyId()
79+
{
80+
isEquals(5145070902336167606L, new KeyIdentifier("4766F6B9D5F21EB6").getKeyId());
81+
isEquals(5145070902336167606L, new KeyIdentifier("4766f6b9d5f21eb6").getKeyId());
82+
83+
isEquals(5507497285755629956L, new KeyIdentifier("4C6E8F99F6E47184").getKeyId());
84+
isEquals(1745434690267590572L, new KeyIdentifier("1839079A640B2FAC").getKeyId());
85+
86+
isTrue(new KeyIdentifier("1839079A640B2FAC").getFingerprint() == null);
87+
}
88+
7389
private void testIdentifierFromV4Fingerprint()
7490
{
7591
String hexFingerprint = "D1A66E1A23B182C9980F788CFBFCC82A015E7330";

0 commit comments

Comments
 (0)