1
1
package org .bouncycastle .bcpg ;
2
2
3
- import java .util .Iterator ;
4
3
import java .util .List ;
5
4
6
5
import org .bouncycastle .util .Arrays ;
@@ -17,6 +16,11 @@ public class KeyIdentifier
17
16
private final byte [] fingerprint ;
18
17
private final long keyId ;
19
18
19
+ public KeyIdentifier (String hexEncoded )
20
+ {
21
+ this (Hex .decode (hexEncoded ));
22
+ }
23
+
20
24
/**
21
25
* Create a new {@link KeyIdentifier} based on a keys fingerprint.
22
26
* For fingerprints matching the format of a v4, v5 or v6 key, the constructor will
@@ -157,6 +161,26 @@ public boolean matches(KeyIdentifier other)
157
161
}
158
162
}
159
163
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
+
160
184
/**
161
185
* Return true, if this {@link KeyIdentifier} is present in the given list of {@link KeyIdentifier} .
162
186
* This will return true if a fingerprint matches, or if a key-id matches,
@@ -167,9 +191,9 @@ public boolean matches(KeyIdentifier other)
167
191
*/
168
192
public boolean isPresentIn (List <KeyIdentifier > others )
169
193
{
170
- for (Iterator it = others . iterator (); it . hasNext (); )
194
+ for (KeyIdentifier other : others )
171
195
{
172
- if (this .matches (( KeyIdentifier ) it . next () ))
196
+ if (this .matches (other ))
173
197
{
174
198
return true ;
175
199
}
@@ -178,6 +202,35 @@ public boolean isPresentIn(List<KeyIdentifier> others)
178
202
return false ;
179
203
}
180
204
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
+
181
234
public String toString ()
182
235
{
183
236
if (isWildcard ())
0 commit comments