Ensure that KeySpecBuilder gets at least one key flag

This commit is contained in:
Paul Schaub 2021-09-20 11:26:00 +02:00
parent cff69006f7
commit 387b2b4b43
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
3 changed files with 18 additions and 5 deletions

View File

@ -55,7 +55,7 @@ public class KeySpec {
return inheritedSubPackets;
}
public static KeySpecBuilder getBuilder(KeyType type, KeyFlag... flags) {
return new KeySpecBuilder(type, flags);
public static KeySpecBuilder getBuilder(KeyType type, KeyFlag flag, KeyFlag... flags) {
return new KeySpecBuilder(type, flag, flags);
}
}

View File

@ -30,6 +30,7 @@ import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.util.CollectionUtils;
public class KeySpecBuilder implements KeySpecBuilderInterface {
@ -41,10 +42,14 @@ public class KeySpecBuilder implements KeySpecBuilderInterface {
private Set<HashAlgorithm> preferredHashAlgorithms = algorithmSuite.getHashAlgorithms();
private Set<SymmetricKeyAlgorithm> preferredSymmetricAlgorithms = algorithmSuite.getSymmetricKeyAlgorithms();
KeySpecBuilder(@Nonnull KeyType type, KeyFlag... flags) {
if (flags == null || flags.length == 0) {
throw new IllegalArgumentException("KeyFlags cannot be empty.");
KeySpecBuilder(@Nonnull KeyType type, KeyFlag flag, KeyFlag... flags) {
if (flag == null) {
throw new IllegalArgumentException("Key MUST carry at least one key flag");
}
if (flags == null) {
throw new IllegalArgumentException("List of additional flags MUST NOT be null.");
}
flags = CollectionUtils.concat(flag, flags);
assureKeyCanCarryFlags(type, flags);
this.type = type;
this.keyFlags = flags;

View File

@ -15,6 +15,7 @@
*/
package org.pgpainless.util;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ -33,4 +34,11 @@ public final class CollectionUtils {
}
return items;
}
public static <T> T[] concat(T t, T[] ts) {
T[] concat = (T[]) Array.newInstance(t.getClass(), ts.length + 1);
concat[0] = t;
System.arraycopy(ts, 0, concat, 1, ts.length);
return concat;
}
}