Skip to content

Commit 797bbf5

Browse files
committed
KeyIdentifier changes
1 parent 1af89a8 commit 797bbf5

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.bouncycastle.bcpg;
22

3-
import java.util.Iterator;
43
import java.util.List;
54

65
import org.bouncycastle.util.Arrays;
@@ -17,6 +16,11 @@ public class KeyIdentifier
1716
private final byte[] fingerprint;
1817
private final long keyId;
1918

19+
public KeyIdentifier(String hexEncoded)
20+
{
21+
this(Hex.decode(hexEncoded));
22+
}
23+
2024
/**
2125
* Create a new {@link KeyIdentifier} based on a keys fingerprint.
2226
* For fingerprints matching the format of a v4, v5 or v6 key, the constructor will
@@ -157,6 +161,26 @@ public boolean matches(KeyIdentifier other)
157161
}
158162
}
159163

164+
public static boolean matches(List<KeyIdentifier> identifiers, KeyIdentifier identifier, boolean explicit)
165+
{
166+
for (KeyIdentifier candidate : identifiers)
167+
{
168+
if (!explicit && candidate.isWildcard())
169+
{
170+
return true;
171+
}
172+
173+
if (candidate.getFingerprint() != null &&
174+
Arrays.constantTimeAreEqual(candidate.getFingerprint(), identifier.getFingerprint()))
175+
{
176+
return true;
177+
}
178+
179+
return candidate.getKeyId() == identifier.getKeyId();
180+
}
181+
return false;
182+
}
183+
160184
/**
161185
* Return true, if this {@link KeyIdentifier} is present in the given list of {@link KeyIdentifier} .
162186
* This will return true if a fingerprint matches, or if a key-id matches,
@@ -167,9 +191,9 @@ public boolean matches(KeyIdentifier other)
167191
*/
168192
public boolean isPresentIn(List<KeyIdentifier> others)
169193
{
170-
for (Iterator it = others.iterator(); it.hasNext();)
194+
for (KeyIdentifier other : others)
171195
{
172-
if (this.matches((KeyIdentifier)it.next()))
196+
if (this.matches(other))
173197
{
174198
return true;
175199
}
@@ -178,6 +202,35 @@ public boolean isPresentIn(List<KeyIdentifier> others)
178202
return false;
179203
}
180204

205+
@Override
206+
public boolean equals(Object obj)
207+
{
208+
if (obj == null)
209+
{
210+
return false;
211+
}
212+
if (this == obj)
213+
{
214+
return true;
215+
}
216+
if (!(obj instanceof KeyIdentifier))
217+
{
218+
return false;
219+
}
220+
KeyIdentifier other = (KeyIdentifier) obj;
221+
if (getFingerprint() != null && other.getFingerprint() != null)
222+
{
223+
return Arrays.constantTimeAreEqual(getFingerprint(), other.getFingerprint());
224+
}
225+
return getKeyId() == other.getKeyId();
226+
}
227+
228+
@Override
229+
public int hashCode()
230+
{
231+
return (int) getKeyId();
232+
}
233+
181234
public String toString()
182235
{
183236
if (isWildcard())

0 commit comments

Comments
 (0)