本文整理汇总了Java中org.bouncycastle.util.IPAddress类的典型用法代码示例。如果您正苦于以下问题:Java IPAddress类的具体用法?Java IPAddress怎么用?Java IPAddress使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IPAddress类属于org.bouncycastle.util包,在下文中一共展示了IPAddress类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testIP
import org.bouncycastle.util.IPAddress; //导入依赖的package包/类
private void testIP(String[] valid, String[] invalid)
{
for (int i = 0; i < valid.length; i++)
{
if (!IPAddress.isValid(valid[i]))
{
fail("Valid input string not accepted: " + valid[i] + ".");
}
}
for (int i = 0; i < invalid.length; i++)
{
if (IPAddress.isValid(invalid[i]))
{
fail("Invalid input string accepted: " + invalid[i] + ".");
}
}
}
示例2: GeneralName
import org.bouncycastle.util.IPAddress; //导入依赖的package包/类
/**
* Create a GeneralName for the given tag from the passed in String.
* <p>
* This constructor can handle:
* <ul>
* <li>rfc822Name
* <li>iPAddress
* <li>directoryName
* <li>dNSName
* <li>uniformResourceIdentifier
* <li>registeredID
* </ul>
* For x400Address, otherName and ediPartyName there is no common string
* format defined.
* <p>
* Note: A directory name can be encoded in different ways into a byte
* representation. Be aware of this if the byte representation is used for
* comparing results.
*
* @param tag tag number
* @param name string representation of name
* @throws IllegalArgumentException if the string encoding is not correct or * not supported.
*/
public GeneralName(
int tag,
String name)
{
this.tag = tag;
if (tag == rfc822Name || tag == dNSName || tag == uniformResourceIdentifier)
{
this.obj = new DERIA5String(name);
}
else if (tag == registeredID)
{
this.obj = new DERObjectIdentifier(name);
}
else if (tag == directoryName)
{
this.obj = new X509Name(name);
}
else if (tag == iPAddress)
{
if (IPAddress.isValid(name))
{
this.obj = new DEROctetString(Strings.toUTF8ByteArray(name));
}
else
{
throw new IllegalArgumentException("IP Address is invalid");
}
}
else
{
throw new IllegalArgumentException("can't process String for tag: " + tag);
}
}
示例3: getSubnetMaskLength
import org.bouncycastle.util.IPAddress; //导入依赖的package包/类
public Integer getSubnetMaskLength(String maskString){
if(!IPAddress.isValidIPv4(maskString)){
return null;
}
String[] octets = maskString.split("\\.");
Integer bits = 0;
for (String o: octets){
switch(o){
case "255":
bits += 8;
continue;
case "254":
bits += 7;
return bits;
case "252":
bits += 6;
return bits;
case "248":
bits += 5;
return bits;
case "240":
bits += 4;
return bits;
case "224":
bits += 3;
return bits;
case "192":
bits += 2;
return bits;
case "128":
bits += 1;
return bits;
case "0":
return bits;
default:
throw new NumberFormatException("non-contiguous subnet mask not supported");
}
}
return bits;
}
示例4: createCASignedCert
import org.bouncycastle.util.IPAddress; //导入依赖的package包/类
/**
* Create a server certificate for the given domain and subject alternative names, signed by the given Certificate Authority.
*/
private X509Certificate createCASignedCert(PublicKey publicKey, X509Certificate certificateAuthorityCert, PrivateKey certificateAuthorityPrivateKey, PublicKey certificateAuthorityPublicKey, String domain, String[] subjectAlternativeNameDomains, String[] subjectAlternativeNameIps) throws Exception {
// signers name
X500Name issuer = new X509CertificateHolder(certificateAuthorityCert.getEncoded()).getSubject();
// subjects name - the same as we are self signed.
X500Name subject = new X500Name("CN=" + domain + ", O=MockServer, L=London, ST=England, C=UK");
// serial
BigInteger serial = BigInteger.valueOf(new Random().nextInt(Integer.MAX_VALUE));
// create the certificate - version 3
X509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(issuer, serial, NOT_BEFORE, NOT_AFTER, subject, publicKey);
builder.addExtension(Extension.subjectKeyIdentifier, false, createSubjectKeyIdentifier(publicKey));
builder.addExtension(Extension.basicConstraints, false, new BasicConstraints(false));
// subject alternative name
List<ASN1Encodable> subjectAlternativeNames = new ArrayList<ASN1Encodable>();
if (subjectAlternativeNameDomains != null) {
subjectAlternativeNames.add(new GeneralName(GeneralName.dNSName, domain));
for (String subjectAlternativeNameDomain : subjectAlternativeNameDomains) {
subjectAlternativeNames.add(new GeneralName(GeneralName.dNSName, subjectAlternativeNameDomain));
}
}
if (subjectAlternativeNameIps != null) {
for (String subjectAlternativeNameIp : subjectAlternativeNameIps) {
if (IPAddress.isValidIPv6WithNetmask(subjectAlternativeNameIp)
|| IPAddress.isValidIPv6(subjectAlternativeNameIp)
|| IPAddress.isValidIPv4WithNetmask(subjectAlternativeNameIp)
|| IPAddress.isValidIPv4(subjectAlternativeNameIp)) {
subjectAlternativeNames.add(new GeneralName(GeneralName.iPAddress, subjectAlternativeNameIp));
}
}
}
if (subjectAlternativeNames.size() > 0) {
DERSequence subjectAlternativeNamesExtension = new DERSequence(subjectAlternativeNames.toArray(new ASN1Encodable[subjectAlternativeNames.size()]));
builder.addExtension(Extension.subjectAlternativeName, false, subjectAlternativeNamesExtension);
}
X509Certificate cert = signCertificate(builder, certificateAuthorityPrivateKey);
cert.checkValidity(new Date());
cert.verify(certificateAuthorityPublicKey);
return cert;
}