![JAR search and dependency download from the Maven repository](/logo.png)
org.spongycastle.jsse.BCSNIHostName Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bctls-jdk15on Show documentation
Show all versions of bctls-jdk15on Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle
intended for the Android platform. Android unfortunately ships with a stripped-down version of
Bouncy Castle, which prevents easy upgrades - Spongy Castle overcomes this and provides a full,
up-to-date version of the Bouncy Castle cryptographic libs.
The newest version!
package org.spongycastle.jsse;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.regex.Pattern;
import org.spongycastle.tls.NameType;
public final class BCSNIHostName extends BCSNIServerName
{
public static BCSNIMatcher createSNIMatcher(String regex)
{
if (regex == null)
{
throw new NullPointerException("'regex' cannot be null");
}
return new BCSNIHostNameMatcher(regex);
}
private final String hostName;
private static String fromAscii(byte[] bs)
{
try
{
return new String(bs, "ASCII");
}
catch (UnsupportedEncodingException e)
{
throw new RuntimeException(e);
}
}
private static byte[] toAscii(String s)
{
try
{
return s.getBytes("ASCII");
}
catch (UnsupportedEncodingException e)
{
throw new RuntimeException(e);
}
}
public BCSNIHostName(String hostName)
{
super(NameType.host_name, toAscii(hostName));
this.hostName = hostName;
}
public BCSNIHostName(byte[] asciiEncoding)
{
super(NameType.host_name, asciiEncoding);
this.hostName = fromAscii(asciiEncoding);
}
public String getAsciiName()
{
return hostName;
}
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (!(obj instanceof BCSNIHostName))
{
return false;
}
BCSNIHostName other = (BCSNIHostName)obj;
return hostName.equalsIgnoreCase(other.hostName);
}
public int hashCode()
{
return hostName.toUpperCase(Locale.ENGLISH).hashCode();
}
private static final class BCSNIHostNameMatcher
extends BCSNIMatcher
{
private final Pattern pattern;
BCSNIHostNameMatcher(String regex)
{
super(NameType.host_name);
this.pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
}
public boolean matches(BCSNIServerName serverName)
{
if (serverName == null || serverName.getType() != NameType.host_name)
{
return false;
}
String hostName = fromAscii(serverName.getEncoded());
return pattern.matcher(hostName).matches();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy