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