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
18/**
19* @author Vera Y. Petrashkova
20*/
21
22package org.apache.harmony.security.tests.support;
23
24import java.security.InvalidAlgorithmParameterException;
25import java.security.InvalidParameterException;
26import java.security.KeyPair;
27import java.security.KeyPairGenerator;
28import java.security.PrivateKey;
29import java.security.PublicKey;
30import java.security.SecureRandom;
31import java.security.spec.AlgorithmParameterSpec;
32
33/**
34 * Additional class extends KeyPairGenerator
35 *
36 */
37
38public class MyKeyPairGenerator1 extends KeyPairGenerator {
39    public int keySize;
40
41    public SecureRandom secureRandom;
42
43    public AlgorithmParameterSpec paramSpec;
44
45    public MyKeyPairGenerator1() {
46        super("MyKeyPairGenerator1");
47    }
48
49    public MyKeyPairGenerator1(String pp) {
50        super(pp);
51    }
52
53    public String getAlgorithm() {
54        return "MyKeyPairGenerator1";
55    }
56
57    public static final String getResAlgorithm() {
58        return "MyKeyPairGenerator1";
59    }
60
61    public void initialize(int keysize, SecureRandom random) {
62        if ((keysize < 0) || ((keysize % 100) != 0)) {
63            throw new InvalidParameterException("Incorrect keysize parameter");
64        }
65        if (random == null) {
66            throw new InvalidParameterException("Incorrect random");
67        }
68        keySize = keysize;
69        secureRandom = random;
70    }
71
72    public KeyPair generateKeyPair() {
73        try {
74            return new KeyPair(new PubKey(), new PrivKey());
75        } catch (Exception e) {
76            e.printStackTrace();
77            return null;
78        }
79    }
80
81    public void initialize(AlgorithmParameterSpec param, SecureRandom random)
82            throws InvalidAlgorithmParameterException {
83        if (random == null) {
84            throw new InvalidParameterException("Incorrect random");
85        }
86        if (param == null) {
87            throw new InvalidAlgorithmParameterException("Incorrect param");
88        }
89        paramSpec = param;
90        secureRandom = random;
91    }
92
93    public class PubKey implements PublicKey {
94        private String algorithm;
95
96        private String format;
97
98        private byte[] encoded;
99
100        public PubKey() {
101            this.algorithm = "MyKeyPairGenerator1";
102            this.format = "test1";
103            this.encoded = new byte[10];
104        }
105
106        public String getAlgorithm() {
107            return algorithm;
108        }
109
110        public String getFormat() {
111            return format;
112        }
113
114        public byte[] getEncoded() {
115            return encoded;
116        }
117    }
118
119    public class PrivKey implements PrivateKey {
120        private String algorithm;
121
122        private String format;
123
124        private byte[] encoded;
125
126        public PrivKey() {
127            this.algorithm = "MyKeyPairGenerator1";
128            this.format = "test1";
129            this.encoded = new byte[10];
130        }
131
132        public String getAlgorithm() {
133            return algorithm;
134        }
135
136        public String getFormat() {
137            return format;
138        }
139
140        public byte[] getEncoded() {
141            return encoded;
142        }
143    }
144
145}