diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java index 5572f449..c53d91e8 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java @@ -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); } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpecBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpecBuilder.java index bbc7f14a..d6095201 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpecBuilder.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpecBuilder.java @@ -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 preferredHashAlgorithms = algorithmSuite.getHashAlgorithms(); private Set 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; diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/CollectionUtils.java b/pgpainless-core/src/main/java/org/pgpainless/util/CollectionUtils.java index 6ad81991..cb845df4 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/CollectionUtils.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/CollectionUtils.java @@ -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[] 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; + } }