1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package javax.crypto.spec;
19
20import libcore.util.EmptyArray;
21
22/**
23 * The source of the label <code>L</code> as specified in <a
24 * href="http://www.ietf.org/rfc/rfc3447.txt"> PKCS #1</a>.
25 */
26public class PSource {
27
28    private String pSrcName;
29
30    private PSource() {}
31
32    /**
33     * Creates a new <code>PSource</code> instance with the specified source
34     * algorithm identifier.
35     *
36     * @param pSrcName
37     *            the source algorithm identifier.
38     * @throws NullPointerException
39     *             if pSrcName is null.
40     */
41    protected PSource(String pSrcName) {
42        if (pSrcName == null) {
43            throw new NullPointerException("pSrcName == null");
44        }
45        this.pSrcName = pSrcName;
46    }
47
48    /**
49     * Returns the source algorithm identifier.
50     *
51     * @return the source algorithm identifier.
52     */
53    public String getAlgorithm() {
54        return pSrcName;
55    }
56
57    /**
58     * The explicit specification of the parameter <code>P</code> used in the
59     * source algorithm.
60     */
61    public static final class PSpecified extends PSource {
62
63        private final byte[] p;
64
65        /**
66         * The instance of <code>PSpecified</code> with the default value
67         * <code>byte[0]</code> for <code>P</code>
68         */
69        public static final PSpecified DEFAULT = new PSpecified();
70
71        private PSpecified() {
72            super("PSpecified");
73            p = EmptyArray.BYTE;
74        }
75
76        /**
77         * Creates a new instance of <code>PSpecified</code> with the specified
78         * parameter <code>P</code>.
79         *
80         * @param p
81         *            the parameter <code>P</code>.
82         * @throws NullPointerException
83         *             if <code>p</code> is null.
84         */
85        public PSpecified(byte[] p) {
86            super("PSpecified");
87            if (p == null) {
88                throw new NullPointerException("p == null");
89            }
90            //TODO: It is unknown which name should be used!
91            //super("");
92            this.p = new byte[p.length];
93            System.arraycopy(p, 0, this.p, 0, p.length);
94        }
95
96        /**
97         * Returns a copy of the value of the parameter <code>P</code>.
98         *
99         * @return a copy of the value of the parameter <code>P</code>
100         */
101        public byte[] getValue() {
102            byte[] result = new byte[p.length];
103            System.arraycopy(p, 0, result, 0, p.length);
104            return result;
105        }
106    }
107}
108