1+ /*
2+ * Copyright (C) 2012 The Android Open Source Project
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package com .datatheorem .android .trustkit .pinning ;
17+
18+ import java .util .regex .Pattern ;
19+
20+ /** Junk drawer of utility methods. */
21+ final class Utils {
22+ /**
23+ * Quick and dirty pattern to differentiate IP addresses from hostnames. This is an approximation
24+ * of Android's private InetAddress#isNumeric API.
25+ *
26+ * <p>This matches IPv6 addresses as a hex string containing at least one colon, and possibly
27+ * including dots after the first colon. It matches IPv4 addresses as strings containing only
28+ * decimal digits and dots. This pattern matches strings like "a:.23" and "54" that are neither IP
29+ * addresses nor hostnames; they will be verified as IP addresses (which is a more strict
30+ * verification).
31+ */
32+ private static final Pattern VERIFY_AS_IP_ADDRESS = Pattern .compile (
33+ "([0-9a-fA-F]*:[0-9a-fA-F:.]*)|([\\ d.]+)" );
34+
35+
36+ /** Returns true if {@code host} is not a host name and might be an IP address. */
37+ public static boolean verifyAsIpAddress (String host ) {
38+ return VERIFY_AS_IP_ADDRESS .matcher (host ).matches ();
39+ }
40+ }
0 commit comments