Search in sources :

Example 1 with ASN1UniversalString

use of com.github.zhenwei.core.asn1.ASN1UniversalString in project jmulticard by ctt-gob-es.

the class IETFUtils method valueToString.

public static String valueToString(ASN1Encodable value) {
    StringBuffer vBuf = new StringBuffer();
    if (value instanceof ASN1String && !(value instanceof ASN1UniversalString)) {
        String v = ((ASN1String) value).getString();
        if (v.length() > 0 && v.charAt(0) == '#') {
            vBuf.append('\\');
        }
        vBuf.append(v);
    } else {
        try {
            vBuf.append('#');
            // -DM Hex.toHexString
            vBuf.append(Hex.toHexString(value.toASN1Primitive().getEncoded(ASN1Encoding.DER)));
        } catch (IOException e) {
            throw new IllegalArgumentException("Other value has no encoded form");
        }
    }
    int end = vBuf.length();
    int index = 0;
    if (vBuf.length() >= 2 && vBuf.charAt(0) == '\\' && vBuf.charAt(1) == '#') {
        index += 2;
    }
    while (index != end) {
        switch(vBuf.charAt(index)) {
            case ',':
            case '"':
            case '\\':
            case '+':
            case '=':
            case '<':
            case '>':
            case ';':
                {
                    vBuf.insert(index, "\\");
                    index += 2;
                    ++end;
                    break;
                }
            default:
                {
                    ++index;
                    break;
                }
        }
    }
    int start = 0;
    if (vBuf.length() > 0) {
        while (vBuf.length() > start && vBuf.charAt(start) == ' ') {
            vBuf.insert(start, "\\");
            start += 2;
        }
    }
    int endBuf = vBuf.length() - 1;
    while (endBuf >= 0 && vBuf.charAt(endBuf) == ' ') {
        vBuf.insert(endBuf, '\\');
        endBuf--;
    }
    return vBuf.toString();
}
Also used : ASN1UniversalString(org.bouncycastle.asn1.ASN1UniversalString) ASN1String(org.bouncycastle.asn1.ASN1String) ASN1UniversalString(org.bouncycastle.asn1.ASN1UniversalString) ASN1String(org.bouncycastle.asn1.ASN1String) IOException(java.io.IOException)

Example 2 with ASN1UniversalString

use of com.github.zhenwei.core.asn1.ASN1UniversalString in project LinLong-Java by zhenwei1108.

the class IETFUtils method valueToString.

public static String valueToString(ASN1Encodable value) {
    StringBuffer vBuf = new StringBuffer();
    if (value instanceof ASN1String && !(value instanceof ASN1UniversalString)) {
        String v = ((ASN1String) value).getString();
        if (v.length() > 0 && v.charAt(0) == '#') {
            vBuf.append('\\');
        }
        vBuf.append(v);
    } else {
        try {
            vBuf.append('#');
            vBuf.append(Hex.toHexString(value.toASN1Primitive().getEncoded(ASN1Encoding.DER)));
        } catch (IOException e) {
            throw new IllegalArgumentException("Other value has no encoded form");
        }
    }
    int end = vBuf.length();
    int index = 0;
    if (vBuf.length() >= 2 && vBuf.charAt(0) == '\\' && vBuf.charAt(1) == '#') {
        index += 2;
    }
    while (index != end) {
        switch(vBuf.charAt(index)) {
            case ',':
            case '"':
            case '\\':
            case '+':
            case '=':
            case '<':
            case '>':
            case ';':
                {
                    vBuf.insert(index, "\\");
                    index += 2;
                    ++end;
                    break;
                }
            default:
                {
                    ++index;
                    break;
                }
        }
    }
    int start = 0;
    if (vBuf.length() > 0) {
        while (vBuf.length() > start && vBuf.charAt(start) == ' ') {
            vBuf.insert(start, "\\");
            start += 2;
        }
    }
    int endBuf = vBuf.length() - 1;
    while (endBuf >= 0 && vBuf.charAt(endBuf) == ' ') {
        vBuf.insert(endBuf, '\\');
        endBuf--;
    }
    return vBuf.toString();
}
Also used : ASN1UniversalString(com.github.zhenwei.core.asn1.ASN1UniversalString) ASN1String(com.github.zhenwei.core.asn1.ASN1String) ASN1UniversalString(com.github.zhenwei.core.asn1.ASN1UniversalString) ASN1String(com.github.zhenwei.core.asn1.ASN1String) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 ASN1String (com.github.zhenwei.core.asn1.ASN1String)1 ASN1UniversalString (com.github.zhenwei.core.asn1.ASN1UniversalString)1 ASN1String (org.bouncycastle.asn1.ASN1String)1 ASN1UniversalString (org.bouncycastle.asn1.ASN1UniversalString)1