1/*
2 * Copyright 2013 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
17package org.conscrypt;
18
19import java.security.PublicKey;
20import java.util.Arrays;
21
22/**
23 * A simple but useless key class that holds X.509 public key information when
24 * the appropriate KeyFactory for the key algorithm is not available.
25 */
26public class X509PublicKey implements PublicKey {
27    private static final long serialVersionUID = -8610156854731664298L;
28
29    private final String algorithm;
30
31    private final byte[] encoded;
32
33    public X509PublicKey(String algorithm, byte[] encoded) {
34        this.algorithm = algorithm;
35        this.encoded = encoded;
36    }
37
38    @Override
39    public String getAlgorithm() {
40        return algorithm;
41    }
42
43    @Override
44    public String getFormat() {
45        return "X.509";
46    }
47
48    @Override
49    public byte[] getEncoded() {
50        return encoded;
51    }
52
53    @Override
54    public String toString() {
55        return "X509PublicKey [algorithm=" + algorithm + ", encoded=" + Arrays.toString(encoded)
56                + "]";
57    }
58
59    @Override
60    public int hashCode() {
61        final int prime = 31;
62        int result = 1;
63        result = prime * result + ((algorithm == null) ? 0 : algorithm.hashCode());
64        result = prime * result + Arrays.hashCode(encoded);
65        return result;
66    }
67
68    @Override
69    public boolean equals(Object obj) {
70        if (this == obj)
71            return true;
72        if (obj == null)
73            return false;
74        if (getClass() != obj.getClass())
75            return false;
76        X509PublicKey other = (X509PublicKey) obj;
77        if (algorithm == null) {
78            if (other.algorithm != null)
79                return false;
80        } else if (!algorithm.equals(other.algorithm))
81            return false;
82        if (!Arrays.equals(encoded, other.encoded))
83            return false;
84        return true;
85    }
86}
87