Skip to content

Commit 0588317

Browse files
committed
Merge branch 'pgpainless-useFingerprintUtil'
2 parents c3bb3ec + 9f80ac6 commit 0588317

File tree

5 files changed

+91
-61
lines changed

5 files changed

+91
-61
lines changed

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

+50-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.bouncycastle.bcpg;
22

3+
import org.bouncycastle.util.Pack;
4+
35
public class FingerprintUtil
46
{
57

@@ -47,18 +49,7 @@ public static long keyIdFromV4Fingerprint(byte[] v4Fingerprint)
4749
*/
4850
public static long longFromLeftMostBytes(byte[] bytes)
4951
{
50-
if (bytes.length < 8)
51-
{
52-
throw new IllegalArgumentException("Byte array MUST contain at least 8 bytes");
53-
}
54-
return ((bytes[0] & 0xffL) << 56) |
55-
((bytes[1] & 0xffL) << 48) |
56-
((bytes[2] & 0xffL) << 40) |
57-
((bytes[3] & 0xffL) << 32) |
58-
((bytes[4] & 0xffL) << 24) |
59-
((bytes[5] & 0xffL) << 16) |
60-
((bytes[6] & 0xffL) << 8) |
61-
((bytes[7] & 0xffL));
52+
return readKeyID(bytes);
6253
}
6354

6455
/**
@@ -68,19 +59,57 @@ public static long longFromLeftMostBytes(byte[] bytes)
6859
* @return long
6960
*/
7061
public static long longFromRightMostBytes(byte[] bytes)
62+
{
63+
return readKeyID(bytes, bytes.length - 8);
64+
}
65+
66+
/**
67+
* Read a key-ID from the first 8 octets of the given byte array.
68+
* @param bytes byte array
69+
* @return key-ID
70+
*/
71+
public static long readKeyID(byte[] bytes)
72+
{
73+
return readKeyID(bytes, 0);
74+
}
75+
76+
/**
77+
* Read a key-ID from 8 octets of the given byte array starting at offset.
78+
* @param bytes byte array
79+
* @param offset offset
80+
* @return key-ID
81+
*/
82+
public static long readKeyID(byte[] bytes, int offset)
7183
{
7284
if (bytes.length < 8)
7385
{
7486
throw new IllegalArgumentException("Byte array MUST contain at least 8 bytes");
7587
}
76-
int i = bytes.length;
77-
return ((bytes[i - 8] & 0xffL) << 56) |
78-
((bytes[i - 7] & 0xffL) << 48) |
79-
((bytes[i - 6] & 0xffL) << 40) |
80-
((bytes[i - 5] & 0xffL) << 32) |
81-
((bytes[i - 4] & 0xffL) << 24) |
82-
((bytes[i - 3] & 0xffL) << 16) |
83-
((bytes[i - 2] & 0xffL) << 8) |
84-
((bytes[i - 1] & 0xffL));
88+
return Pack.bigEndianToLong(bytes, offset);
89+
}
90+
91+
/**
92+
* Write the key-ID encoded as 8 octets to the given byte array, starting at index offset.
93+
* @param keyID keyID
94+
* @param bytes byte array
95+
* @param offset starting offset
96+
*/
97+
public static void writeKeyID(long keyID, byte[] bytes, int offset)
98+
{
99+
if (bytes.length - offset < 8)
100+
{
101+
throw new IllegalArgumentException("Not enough space to write key-ID to byte array.");
102+
}
103+
Pack.longToBigEndian(keyID, bytes, offset);
104+
}
105+
106+
/**
107+
* Write the key-ID to the first 8 octets of the given byte array.
108+
* @param keyID keyID
109+
* @param bytes byte array
110+
*/
111+
public static void writeKeyID(long keyID, byte[] bytes)
112+
{
113+
writeKeyID(keyID, bytes, 0);
85114
}
86115
}

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

+2-18
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,7 @@ else if (version == VERSION_6)
8282
fingerprint = new byte[32];
8383
in.readFully(fingerprint);
8484

85-
// TODO: Replace with FingerprintUtil
86-
keyID = ((fingerprint[0] & 0xffL) << 56) |
87-
((fingerprint[1] & 0xffL) << 48) |
88-
((fingerprint[2] & 0xffL) << 40) |
89-
((fingerprint[3] & 0xffL) << 32) |
90-
((fingerprint[4] & 0xffL) << 24) |
91-
((fingerprint[5] & 0xffL) << 16) |
92-
((fingerprint[6] & 0xffL) << 8) |
93-
((fingerprint[7] & 0xffL));
85+
keyID = FingerprintUtil.keyIdFromV6Fingerprint(fingerprint);
9486
}
9587
else
9688
{
@@ -162,15 +154,7 @@ public OnePassSignaturePacket(
162154
this.salt = salt;
163155
this.fingerprint = fingerprint;
164156
this.isContaining = (isNested) ? 0 : 1;
165-
// TODO: Replace with FingerprintUtil
166-
keyID = ((fingerprint[0] & 0xffL) << 56) |
167-
((fingerprint[1] & 0xffL) << 48) |
168-
((fingerprint[2] & 0xffL) << 40) |
169-
((fingerprint[3] & 0xffL) << 32) |
170-
((fingerprint[4] & 0xffL) << 24) |
171-
((fingerprint[5] & 0xffL) << 16) |
172-
((fingerprint[6] & 0xffL) << 8) |
173-
((fingerprint[7] & 0xffL));
157+
keyID = FingerprintUtil.keyIdFromV6Fingerprint(fingerprint);
174158
}
175159

176160
/**

pg/src/main/java/org/bouncycastle/bcpg/sig/IssuerKeyID.java

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.bouncycastle.bcpg.sig;
22

3+
import org.bouncycastle.bcpg.FingerprintUtil;
34
import org.bouncycastle.bcpg.SignatureSubpacket;
45
import org.bouncycastle.bcpg.SignatureSubpacketTags;
56

@@ -13,16 +14,7 @@ protected static byte[] keyIDToBytes(
1314
long keyId)
1415
{
1516
byte[] data = new byte[8];
16-
17-
data[0] = (byte)(keyId >> 56);
18-
data[1] = (byte)(keyId >> 48);
19-
data[2] = (byte)(keyId >> 40);
20-
data[3] = (byte)(keyId >> 32);
21-
data[4] = (byte)(keyId >> 24);
22-
data[5] = (byte)(keyId >> 16);
23-
data[6] = (byte)(keyId >> 8);
24-
data[7] = (byte)keyId;
25-
17+
FingerprintUtil.writeKeyID(keyId, data);
2618
return data;
2719
}
2820

@@ -43,9 +35,6 @@ public IssuerKeyID(
4335

4436
public long getKeyID()
4537
{
46-
long keyID = ((long)(data[0] & 0xff) << 56) | ((long)(data[1] & 0xff) << 48) | ((long)(data[2] & 0xff) << 40) | ((long)(data[3] & 0xff) << 32)
47-
| ((long)(data[4] & 0xff) << 24) | ((data[5] & 0xff) << 16) | ((data[6] & 0xff) << 8) | (data[7] & 0xff);
48-
49-
return keyID;
38+
return FingerprintUtil.readKeyID(data);
5039
}
5140
}

pg/src/test/java/org/bouncycastle/bcpg/test/FingerprintUtilTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.bouncycastle.bcpg.test;
22

33
import org.bouncycastle.bcpg.FingerprintUtil;
4+
import org.bouncycastle.util.Arrays;
45
import org.bouncycastle.util.encoders.Hex;
56
import org.bouncycastle.util.test.SimpleTest;
67

@@ -46,6 +47,38 @@ private void testLibrePgpKeyIdFromFingerprint()
4647
-3812177997909612905L, FingerprintUtil.keyIdFromLibrePgpFingerprint(decoded));
4748
}
4849

50+
private void testLeftMostEqualsRightMostFor8Bytes()
51+
{
52+
byte[] bytes = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
53+
isEquals(
54+
FingerprintUtil.longFromLeftMostBytes(bytes),
55+
FingerprintUtil.longFromRightMostBytes(bytes));
56+
byte[] b = new byte[8];
57+
FingerprintUtil.writeKeyID(FingerprintUtil.longFromLeftMostBytes(bytes), b);
58+
isTrue(Arrays.areEqual(bytes, b));
59+
}
60+
61+
private void testWriteKeyIdToBytes()
62+
{
63+
byte[] bytes = new byte[12];
64+
long keyId = 72623859790382856L;
65+
FingerprintUtil.writeKeyID(keyId, bytes, 2);
66+
isTrue(Arrays.areEqual(
67+
new byte[] {0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00},
68+
bytes));
69+
70+
try
71+
{
72+
byte[] b = new byte[7];
73+
FingerprintUtil.writeKeyID(0, b);
74+
fail("Expected IllegalArgumentException for too short byte array.");
75+
}
76+
catch (IllegalArgumentException e)
77+
{
78+
// Expected
79+
}
80+
}
81+
4982
@Override
5083
public String getName()
5184
{
@@ -60,6 +93,8 @@ public void performTest()
6093
testV6KeyIdFromFingerprint();
6194
testKeyIdFromTooShortFails();
6295
testLibrePgpKeyIdFromFingerprint();
96+
testLeftMostEqualsRightMostFor8Bytes();
97+
testWriteKeyIdToBytes();
6398
}
6499

65100
public static void main(String[] args)

pg/src/test/java/org/bouncycastle/bcpg/test/OnePassSignaturePacketTest.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,7 @@ private void roundtripV6Packet()
117117
{
118118
byte[] salt = new byte[32];
119119
byte[] fingerprint = Hex.decode("CB186C4F0609A697E4D52DFA6C722B0C1F1E27C18A56708F6525EC27BAD9ACC9");
120-
long keyID = ((fingerprint[0] & 0xffL) << 56) |
121-
((fingerprint[1] & 0xffL) << 48) |
122-
((fingerprint[2] & 0xffL) << 40) |
123-
((fingerprint[3] & 0xffL) << 32) |
124-
((fingerprint[4] & 0xffL) << 24) |
125-
((fingerprint[5] & 0xffL) << 16) |
126-
((fingerprint[6] & 0xffL) << 8) |
127-
((fingerprint[7] & 0xffL));
120+
long keyID = FingerprintUtil.keyIdFromV6Fingerprint(fingerprint);
128121

129122
new SecureRandom().nextBytes(salt);
130123
OnePassSignaturePacket before = new OnePassSignaturePacket(

0 commit comments

Comments
 (0)