1 /*
2  * Copyright (C) 2015 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 
17 package android.security.net.config;
18 
19 import java.util.Arrays;
20 
21 /** @hide */
22 public final class Pin {
23     public final String digestAlgorithm;
24     public final byte[] digest;
25 
26     private final int mHashCode;
27 
Pin(String digestAlgorithm, byte[] digest)28     public Pin(String digestAlgorithm, byte[] digest) {
29         this.digestAlgorithm = digestAlgorithm;
30         this.digest = digest;
31         mHashCode = Arrays.hashCode(digest) ^ digestAlgorithm.hashCode();
32     }
33 
34     /**
35      * @hide
36      */
isSupportedDigestAlgorithm(String algorithm)37     public static boolean isSupportedDigestAlgorithm(String algorithm) {
38         // Currently only SHA-256 is supported. SHA-512 if/once Chromium networking stack
39         // supports it.
40         return "SHA-256".equalsIgnoreCase(algorithm);
41     }
42 
43     /**
44      * @hide
45      */
getDigestLength(String algorithm)46     public static int getDigestLength(String algorithm) {
47         if ("SHA-256".equalsIgnoreCase(algorithm)) {
48             return 32;
49         }
50         throw new IllegalArgumentException("Unsupported digest algorithm: " + algorithm);
51     }
52 
53     @Override
hashCode()54     public int hashCode() {
55         return mHashCode;
56     }
57 
58     @Override
equals(Object obj)59     public boolean equals(Object obj) {
60         if (this == obj) {
61             return true;
62         }
63         if (!(obj instanceof Pin)) {
64             return false;
65         }
66         Pin other = (Pin) obj;
67         if (other.hashCode() != mHashCode) {
68             return false;
69         }
70         if (!Arrays.equals(digest, other.digest)) {
71             return false;
72         }
73         if (!digestAlgorithm.equals(other.digestAlgorithm)) {
74             return false;
75         }
76         return true;
77     }
78 }
79