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 *
26 * @hide
27 */
28@Internal
29public class X509PublicKey implements PublicKey {
30    private static final long serialVersionUID = -8610156854731664298L;
31
32    private final String algorithm;
33
34    private final byte[] encoded;
35
36    public X509PublicKey(String algorithm, byte[] encoded) {
37        this.algorithm = algorithm;
38        this.encoded = encoded;
39    }
40
41    @Override
42    public String getAlgorithm() {
43        return algorithm;
44    }
45
46    @Override
47    public String getFormat() {
48        return "X.509";
49    }
50
51    @Override
52    public byte[] getEncoded() {
53        return encoded;
54    }
55
56    @Override
57    public String toString() {
58        return "X509PublicKey [algorithm=" + algorithm + ", encoded=" + Arrays.toString(encoded)
59                + "]";
60    }
61
62    @Override
63    public int hashCode() {
64        final int prime = 31;
65        int result = 1;
66        result = prime * result + ((algorithm == null) ? 0 : algorithm.hashCode());
67        result = prime * result + Arrays.hashCode(encoded);
68        return result;
69    }
70
71    @Override
72    public boolean equals(Object obj) {
73        if (this == obj)
74            return true;
75        if (obj == null)
76            return false;
77        if (getClass() != obj.getClass())
78            return false;
79        X509PublicKey other = (X509PublicKey) obj;
80        if (algorithm == null) {
81            if (other.algorithm != null)
82                return false;
83        } else if (!algorithm.equals(other.algorithm))
84            return false;
85        if (!Arrays.equals(encoded, other.encoded))
86            return false;
87        return true;
88    }
89}
90