All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.bcpg.sig.Features Maven / Gradle / Ivy

Go to download

The Bouncy Castle Java API for handling the OpenPGP protocol. This jar contains the OpenPGP API for JDK 1.5 to JDK 1.8. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs.

There is a newer version: 1.79
Show newest version
package org.bouncycastle.bcpg.sig;

import org.bouncycastle.bcpg.SignatureSubpacket;
import org.bouncycastle.bcpg.SignatureSubpacketTags;

public class Features
    extends SignatureSubpacket
{

    /** Identifier for the modification detection feature */
    public static final byte FEATURE_MODIFICATION_DETECTION = 1;

    private static final byte[] featureToByteArray(byte feature)
    {
        byte[] data = new byte[1];
        data[0] = feature;
        return data;
    }

    public Features(
        boolean    critical,
        boolean    isLongLength,
        byte[]     data)
    {
        super(SignatureSubpacketTags.FEATURES, critical, isLongLength, data);
    }

    public Features(boolean critical, byte feature)
    {
        super(SignatureSubpacketTags.FEATURES, critical, false, featureToByteArray(feature));
    }

    /**
     * Returns if modification detection is supported.
     */
    public boolean supportsModificationDetection()
    {
        return supportsFeature(FEATURE_MODIFICATION_DETECTION);
    }

    /**
     * Returns if a particular feature is supported.
     */
    public boolean supportsFeature(byte feature)
    {
        for (int i = 0; i < data.length; i++)
        {
            if (data[i] == feature)
            {
                return true;
            }
        }
        return false;
    }


    /**
     * Sets support for a particular feature.
     */
    private void setSupportsFeature(byte feature, boolean support)
    {
        if (feature == 0)
        {
            throw new IllegalArgumentException("feature == 0");
        }
        if (supportsFeature(feature) != support)
        {
            if (support == true)
            {
                byte[] temp = new byte[data.length + 1];
                System.arraycopy(data, 0, temp, 0, data.length);
                temp[data.length] = feature;
                data = temp;
            }
            else
            {
                for (int i = 0; i < data.length; i++)
                {
                    if (data[i] == feature)
                    {
                        byte[] temp = new byte[data.length - 1];
                        System.arraycopy(data, 0, temp, 0, i);
                        System.arraycopy(data, i + 1, temp, i, temp.length - i);
                        data = temp;
                        break;
                    }
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy