Skip to content

Commit 0939cf4

Browse files
author
gefeili
committed
Update KeyIdentifier
1 parent 6b80483 commit 0939cf4

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public class KeyIdentifier
1717
private final byte[] fingerprint;
1818
private final long keyId;
1919

20+
public KeyIdentifier(String hexEncoded)
21+
{
22+
this(Hex.decode(hexEncoded));
23+
}
24+
2025
/**
2126
* Create a new {@link KeyIdentifier} based on a keys fingerprint.
2227
* For fingerprints matching the format of a v4, v5 or v6 key, the constructor will
@@ -157,6 +162,26 @@ public boolean matches(KeyIdentifier other)
157162
}
158163
}
159164

165+
public static boolean matches(List<KeyIdentifier> identifiers, KeyIdentifier identifier, boolean explicit)
166+
{
167+
for (KeyIdentifier candidate : identifiers)
168+
{
169+
if (!explicit && candidate.isWildcard())
170+
{
171+
return true;
172+
}
173+
174+
if (candidate.getFingerprint() != null &&
175+
Arrays.constantTimeAreEqual(candidate.getFingerprint(), identifier.getFingerprint()))
176+
{
177+
return true;
178+
}
179+
180+
return candidate.getKeyId() == identifier.getKeyId();
181+
}
182+
return false;
183+
}
184+
160185
/**
161186
* Return true, if this {@link KeyIdentifier} is present in the given list of {@link KeyIdentifier} .
162187
* This will return true if a fingerprint matches, or if a key-id matches,
@@ -178,6 +203,35 @@ public boolean isPresentIn(List<KeyIdentifier> others)
178203
return false;
179204
}
180205

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

0 commit comments

Comments
 (0)