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

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

Go to download

The Bouncy Castle Java APIs for the OpenPGP Protocol. The APIs are designed primarily to be used in conjunction with the BC LTS provider but may also be used with other providers providing cryptographic services.

The newest version!
package org.bouncycastle.bcpg.sig;

import org.bouncycastle.bcpg.SignatureSubpacket;
import org.bouncycastle.bcpg.SignatureSubpacketTags;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Strings;

/**
 * Signature Subpacket containing a regular expression limiting the scope of the signature.
 * Note: the RFC says the byte encoding is to be null terminated.
 *
 * @see 
 *     RFC4880 - Regular Expression
 * @see 
 *     RFC9580 - Regular Expression
 */
public class RegularExpression
    extends SignatureSubpacket
{
    public RegularExpression(
            boolean critical,
            boolean isLongLength,
            byte[] data)
    {
        super(SignatureSubpacketTags.REG_EXP, critical, isLongLength, data);
        if (data[data.length - 1] != 0)
        {
            throw new IllegalArgumentException("data in regex missing null termination");
        }
    }

    public RegularExpression(
            boolean critical,
            String regex)
    {
        super(
                SignatureSubpacketTags.REG_EXP,
                critical,
                false,
                toNullTerminatedUTF8ByteArray(regex));
    }

    public String getRegex()
    {
        // last byte is null terminator
        return Strings.fromUTF8ByteArray(data, 0, data.length - 1);
    }

    public byte[] getRawRegex()
    {
        return Arrays.clone(data);
    }

    private static byte[] toNullTerminatedUTF8ByteArray(String string)
    {
        byte[] utf8 = Strings.toUTF8ByteArray(string);
        return Arrays.append(utf8, (byte)0);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy