pgpainless/pgpainless-core/src/main/java/org/pgpainless/algorithm/KeyFlag.java

113 lines
2.7 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.algorithm;
2018-06-02 21:21:35 +02:00
import java.util.ArrayList;
import java.util.List;
2018-06-02 21:21:35 +02:00
import org.bouncycastle.bcpg.sig.KeyFlags;
2021-04-25 13:28:33 +02:00
/**
* Enumeration of different key flags.
* Key flags denote different capabilities of a key pair.
*
* @see <a href="https://tools.ietf.org/html/rfc4880#section-5.2.3.21">RFC4880: Key Flags</a>
*/
2018-06-02 21:21:35 +02:00
public enum KeyFlag {
2021-04-25 13:28:33 +02:00
/**
* This key may be used to certify other keys.
*/
2018-07-02 21:40:59 +02:00
CERTIFY_OTHER (KeyFlags.CERTIFY_OTHER),
2021-04-25 13:28:33 +02:00
/**
* This key may be used to sign data.
*/
2018-07-02 21:40:59 +02:00
SIGN_DATA (KeyFlags.SIGN_DATA),
2021-04-25 13:28:33 +02:00
/**
* This key may be used to encrypt communications.
*/
2018-07-02 21:40:59 +02:00
ENCRYPT_COMMS (KeyFlags.ENCRYPT_COMMS),
2021-04-25 13:28:33 +02:00
/**
* This key may be used to encrypt storage.
*/
2018-06-02 21:21:35 +02:00
ENCRYPT_STORAGE(KeyFlags.ENCRYPT_STORAGE),
2021-04-25 13:28:33 +02:00
/**
* The private component of this key may have been split by a secret-sharing mechanism.
*/
2018-07-02 21:40:59 +02:00
SPLIT (KeyFlags.SPLIT),
2021-04-25 13:28:33 +02:00
/**
* This key may be used for authentication.
*/
2018-07-02 21:40:59 +02:00
AUTHENTICATION (KeyFlags.AUTHENTICATION),
2021-04-25 13:28:33 +02:00
/**
* The private component of this key may be in the possession of more than one person.
*/
2018-07-02 21:40:59 +02:00
SHARED (KeyFlags.SHARED),
2018-06-02 21:21:35 +02:00
;
private final int flag;
KeyFlag(int flag) {
this.flag = flag;
}
2021-04-25 13:28:33 +02:00
/**
* Return the numeric id of the {@link KeyFlag}.
*
* @return numeric id
*/
2018-06-02 21:21:35 +02:00
public int getFlag() {
return flag;
}
2021-04-25 13:28:33 +02:00
/**
* Convert a bitmask into a list of {@link KeyFlag KeyFlags}.
*
* @param bitmask bitmask
* @return list of key flags encoded by the bitmask
*/
public static List<KeyFlag> fromBitmask(int bitmask) {
List<KeyFlag> flags = new ArrayList<>();
for (KeyFlag f : KeyFlag.values()) {
if ((bitmask & f.flag) != 0) {
flags.add(f);
}
}
2018-06-21 15:18:48 +02:00
return flags;
}
2020-01-12 15:40:50 +01:00
2021-04-25 13:28:33 +02:00
/**
* Encode a list of {@link KeyFlag KeyFlags} into a bitmask.
*
* @param flags list of flags
* @return bitmask
*/
2020-01-12 16:37:24 +01:00
public static int toBitmask(KeyFlag... flags) {
int mask = 0;
for (KeyFlag f : flags) {
mask |= f.getFlag();
}
return mask;
}
2021-04-25 13:28:33 +02:00
/**
* Return true if the provided bitmask has the bit for the provided flag set.
* Return false if the mask does not contain the flag.
*
* @param mask bitmask
* @param flag flag to be tested for
* @return true if flag is set, false otherwise
*/
2020-01-12 15:40:50 +01:00
public static boolean hasKeyFlag(int mask, KeyFlag flag) {
2020-01-12 16:37:24 +01:00
return (mask & flag.getFlag()) == flag.getFlag();
2020-01-12 15:40:50 +01:00
}
2018-06-02 21:21:35 +02:00
}