Skip to content

Commit 33a678b

Browse files
author
gefeili
committed
refactor PGPPublicKey.join
1 parent 7a4fc94 commit 33a678b

File tree

1 file changed

+36
-64
lines changed

1 file changed

+36
-64
lines changed

pg/src/main/java/org/bouncycastle/openpgp/PGPPublicKey.java

Lines changed: 36 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,28 +1137,7 @@ public static PGPPublicKey join(
11371137
}
11381138

11391139
// key signatures
1140-
for (Iterator<PGPSignature> it = copy.keySigs.iterator(); it.hasNext(); )
1141-
{
1142-
PGPSignature keySig = (PGPSignature)it.next();
1143-
boolean found = false;
1144-
for (int i = 0; i < keySigs.size(); i++)
1145-
{
1146-
PGPSignature existingKeySig = (PGPSignature)keySigs.get(i);
1147-
if (PGPSignature.isSignatureEncodingEqual(existingKeySig, keySig))
1148-
{
1149-
found = true;
1150-
// join existing sig with copy to apply modifications in unhashed subpackets
1151-
existingKeySig = PGPSignature.join(existingKeySig, keySig);
1152-
keySigs.set(i, existingKeySig);
1153-
break;
1154-
}
1155-
}
1156-
if (found)
1157-
{
1158-
break;
1159-
}
1160-
keySigs.add(keySig);
1161-
}
1140+
joinPgpSignatureList(copy.keySigs, keySigs, true, true);
11621141

11631142
// user-ids and id sigs
11641143
for (int idIdx = 0; idIdx < copy.ids.size(); idIdx++)
@@ -1198,27 +1177,7 @@ public static PGPPublicKey join(
11981177
}
11991178

12001179
List<PGPSignature> existingIdSigs = (List<PGPSignature>)idSigs.get(existingIdIndex);
1201-
for (Iterator<PGPSignature> it = copyIdSigs.iterator(); it.hasNext(); )
1202-
{
1203-
PGPSignature newSig = (PGPSignature)it.next();
1204-
boolean found = false;
1205-
for (int i = 0; i < existingIdSigs.size(); i++)
1206-
{
1207-
PGPSignature existingSig = (PGPSignature)existingIdSigs.get(i);
1208-
if (PGPSignature.isSignatureEncodingEqual(newSig, existingSig))
1209-
{
1210-
found = true;
1211-
// join existing sig with copy to apply modifications in unhashed subpackets
1212-
existingSig = PGPSignature.join(existingSig, newSig);
1213-
existingIdSigs.set(i, existingSig);
1214-
break;
1215-
}
1216-
}
1217-
if (!found)
1218-
{
1219-
existingIdSigs.add(newSig);
1220-
}
1221-
}
1180+
joinPgpSignatureList(copyIdSigs, existingIdSigs, false, true);
12221181
}
12231182

12241183
// subSigs
@@ -1230,27 +1189,7 @@ public static PGPPublicKey join(
12301189
}
12311190
else
12321191
{
1233-
for (Iterator<PGPSignature> it = copy.subSigs.iterator(); it.hasNext(); )
1234-
{
1235-
PGPSignature copySubSig = (PGPSignature)it.next();
1236-
boolean found = false;
1237-
for (int i = 0; subSigs != null && i < subSigs.size(); i++)
1238-
{
1239-
PGPSignature existingSubSig = (PGPSignature)subSigs.get(i);
1240-
if (PGPSignature.isSignatureEncodingEqual(existingSubSig, copySubSig))
1241-
{
1242-
found = true;
1243-
// join existing sig with copy to apply modifications in unhashed subpackets
1244-
existingSubSig = PGPSignature.join(existingSubSig, copySubSig);
1245-
subSigs.set(i, existingSubSig);
1246-
break;
1247-
}
1248-
}
1249-
if (!found && subSigs != null)
1250-
{
1251-
subSigs.add(copySubSig);
1252-
}
1253-
}
1192+
joinPgpSignatureList(copy.subSigs, subSigs, false, subSigs != null);
12541193
}
12551194
}
12561195

@@ -1259,4 +1198,37 @@ public static PGPPublicKey join(
12591198

12601199
return merged;
12611200
}
1201+
1202+
private static void joinPgpSignatureList(List<PGPSignature> source,
1203+
List<PGPSignature> rlt,
1204+
boolean needBreak,
1205+
boolean isNotNull)
1206+
throws PGPException
1207+
{
1208+
for (Iterator<PGPSignature> it = source.iterator(); it.hasNext(); )
1209+
{
1210+
PGPSignature copySubSig = (PGPSignature)it.next();
1211+
boolean found = false;
1212+
for (int i = 0; isNotNull && i < rlt.size(); i++)
1213+
{
1214+
PGPSignature existingSubSig = (PGPSignature)rlt.get(i);
1215+
if (PGPSignature.isSignatureEncodingEqual(existingSubSig, copySubSig))
1216+
{
1217+
found = true;
1218+
// join existing sig with copy to apply modifications in unhashed subpackets
1219+
existingSubSig = PGPSignature.join(existingSubSig, copySubSig);
1220+
rlt.set(i, existingSubSig);
1221+
break;
1222+
}
1223+
}
1224+
if (found && needBreak)
1225+
{
1226+
break;
1227+
}
1228+
else if (!found && isNotNull)
1229+
{
1230+
rlt.add(copySubSig);
1231+
}
1232+
}
1233+
}
12621234
}

0 commit comments

Comments
 (0)