📄 name.java
字号:
}
if (org != null)
{
name.addComponent(createRDN(ASN1OID.organization, org));
}
for (int i = 0; i < orgUnit.size(); i++)
{
String myou = (String) orgUnit.elementAt(i);
if (myou != null && myou.trim().length() > 0)
name.addComponent(createRDN(
ASN1OID.organizationalUnit, myou));
}
if (emailAdd != null)
{
name.addComponent(createEmail(ASN1OID.emailAddress, emailAdd));
}
if (commonName != null)
{
name.addComponent(createRDN(ASN1OID.commonName, commonName));
}
if (uniqueID != null)
{
name.addComponent(createEmail(
ASN1OID.unstructuredName, uniqueID));
}
if (addressIP != null)
{
name.addComponent(createRDN(
ASN1OID.unstructuredAddress, addressIP));
}
}
catch(ASN1Exception asn1e)
{
name = null;
return;
}
name.initByteArray();
}
/**
* Creates a relative distinguished name while the value is an
* ASN.1 PrintableString.
*/
private ASN1Object createRDN(String t, String v)
throws ASN1Exception
{
ASN1Object rdn, ava, type, value;
rdn = ASN1Object.create(ASN1Type.SET);
ava = ASN1Object.create(ASN1Type.SEQUENCE);
type = ASN1Object.create(ASN1Type.OBJECT_ID, t);
value = ASN1Object.create(ASN1Type.PrintableString, v);
ava.addComponent(type);
ava.addComponent(value);
rdn.addComponent(ava);
return rdn;
}
/**
* Creates a relative distinguished name while the value is an
* ASN.1 IA5String.
*/
private ASN1Object createEmail(String t, String v)
throws ASN1Exception
{
ASN1Object rdn, ava, type, value;
rdn = ASN1Object.create(ASN1Type.SET);
ava = ASN1Object.create(ASN1Type.SEQUENCE);
type = ASN1Object.create(ASN1Type.OBJECT_ID, t);
value = ASN1Object.create(ASN1Type.IA5String, v);
ava.addComponent(type);
ava.addComponent(value);
rdn.addComponent(ava);
return rdn;
}
/**
* Converts the Name to an ASN1Object.
*/
public ASN1Object toASN1Object()
{
if (name == null)
{
createName();
}
return name;
}
/**
* Gets the DER encoded byte array of the Name.
*/
public byte [] toByteArrayDER()
{
if (name == null)
{
createName();
}
if (name == null)
{
return null;
}
return name.toDERBytes();
}
/* the following methods retrieve each attribute */
/**
* Gets the country code.
*/
public String country()
{
return country;
}
/**
* Gets the state or province name.
*/
public String stateOrProvince()
{
return state;
}
/**
* Gets the locality.
*/
public String locality()
{
return locality;
}
/**
* Gets the organization.
*/
public String organization()
{
return org;
}
/**
* Gets the organizational unit.
*/
public String organizationalUnit()
{
if (orgUnit.size() >= 1)
return (String) orgUnit.elementAt(0);
else
return null;
}
/**
* Gets the organizational units.
*/
public Vector organizationalUnits()
{
return orgUnit;
}
/**
* Gets the common name.
*/
public String commonName()
{
if (commonName != null && commonName.trim().length() != 0)
return commonName;
else
return toString();
}
/**
* Gets the email address.
*/
public String emailAddress()
{
return emailAdd;
}
/**
* Gets the unique identifier.
*/
public String uniqueID()
{
return uniqueID;
}
/**
* Gets the IP address.
*/
public String addressIP()
{
return addressIP;
}
/**
* Sets the name to be the same as 'o'.
*/
public void set(Name o)
{
this.country = o.country;
this.state = o.state;
this.locality = o.locality;
this.org = o.org;
this.orgUnit = o.orgUnit;
this.commonName = o.commonName;
this.emailAdd = o.emailAdd;
this.uniqueID = o.uniqueID;
this.addressIP = o.addressIP;
this.name = o.name;
}
/**
* Tests whether this Name is the same as the given object.
*/
public boolean equals(Object n)
{
if (!(n instanceof Name))
{
return false;
}
Name o = (Name) n;
return this.toASN1Object().equals(o.toASN1Object());
}
/**
* Checks the validity of the Name. The essential attributes cannot
* be all nulls.
*/
public boolean valid()
{
if ((country == null || country.length() == 0)
&& (state == null || state.length() == 0)
&& (locality == null || locality.length() == 0)
&& (org == null || org.length() == 0)
&& (orgUnit == null || orgUnit.size() == 0)
&& (commonName == null || commonName.length() == 0)
&& (emailAdd == null || emailAdd.length() == 0))
{
return false;
}
return true;
}
/**
* A string description of the Name.
*/
public String toString()
{
String result = null;
if (country != null && !country.equals("null") && country.trim().length() > 0)
result = "C = " + country;
if (state != null && !state.equals("null") && state.trim().length() > 0)
result += ", ST = " + state;
if (locality != null && !locality.equals("null") && locality.trim().length() > 0)
result += ", L = " + locality;
if (org != null && !org.equals("null") && org.trim().length() > 0)
result += ", O = " + org;
for (int i = 0; i < orgUnit.size(); i++)
{
String anOrgUnit = (String) orgUnit.elementAt(i);
if (anOrgUnit != null && !anOrgUnit.equals("null") && anOrgUnit.trim().length() > 0)
result += ", OU = " + anOrgUnit;
}
if (commonName != null && !commonName.equals("null") && commonName.trim().length() > 0)
result += ", CN = " + commonName;
if (emailAdd != null && !emailAdd.equals("null") && emailAdd.trim().length() > 0)
result += ", EM = " + emailAdd;
if (uniqueID != null && !uniqueID.equals("null") && uniqueID.trim().length() > 0)
result += ", unstructuredName = " + uniqueID;
if (addressIP != null && !addressIP.equals("null") && addressIP.trim().length() > 0)
result += ", unstructuredAddress = " + addressIP;
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -