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

org.apache.sshd.common.util.io.der.ASN1Class Maven / Gradle / Ivy

There is a newer version: 2.4.1.Final
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.sshd.common.util.io.der;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.sshd.common.util.GenericUtils;

/**
 * @author Apache MINA SSHD Project
 */
public enum ASN1Class {
    // NOTE: order is crucial, so DON'T change it
    UNIVERSAL((byte) 0x00),
    APPLICATION((byte) 0x01),
    CONTEXT((byte) 0x02),
    PRIVATE((byte) 0x03);

    public static final List VALUES = Collections.unmodifiableList(Arrays.asList(values()));

    private final byte byteValue;

    ASN1Class(byte classValue) {
        byteValue = classValue;
    }

    public byte getClassValue() {
        return byteValue;
    }

    public static ASN1Class fromName(String s) {
        if (GenericUtils.isEmpty(s)) {
            return null;
        }

        for (ASN1Class c : VALUES) {
            if (s.equalsIgnoreCase(c.name())) {
                return c;
            }
        }

        return null;
    }

    /**
     * 

* The first byte in DER encoding is made of following fields *

* *
     *-------------------------------------------------
     *|Bit 8|Bit 7|Bit 6|Bit 5|Bit 4|Bit 3|Bit 2|Bit 1|
     *-------------------------------------------------
     *|  Class    | CF  |        Type                 |
     *-------------------------------------------------
     * 
* * @param value The original DER encoded byte * @return The {@link ASN1Class} value - {@code null} if no match found * @see #fromTypeValue(int) */ public static ASN1Class fromDERValue(int value) { return fromTypeValue((value >> 6) & 0x03); } /** * @param value The "pure" value - unshifted and with no extras * @return The {@link ASN1Class} value - {@code null} if no match found */ public static ASN1Class fromTypeValue(int value) { // all 4 values are defined if ((value < 0) || (value >= VALUES.size())) { return null; } return VALUES.get(value); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy