9e95ab2ff167b20f533e5cf7ac8f834f1872ff9e
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / aaf / certservice / cmpv2client / external / Factory.java
1 /*
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modifications Copyright (C) 2019 IBM.
8  * ===========================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  *
22  */
23
24 package org.onap.aaf.certservice.cmpv2client.external;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.security.KeyPair;
30 import java.security.KeyPairGenerator;
31 import java.security.NoSuchAlgorithmException;
32 import java.security.SecureRandom;
33
34 public final class Factory {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(Factory.class);
37     private static final KeyPairGenerator KEY_PAIR_GENERATOR;
38     private static final SecureRandom SECURE_RANDOM;
39     private static final String KEY_ALGORITHM = "RSA";
40     private static final int KEY_LENGTH = 2048;
41
42     static {
43         SECURE_RANDOM = new SecureRandom();
44         KeyPairGenerator tempKeygen;
45         try {
46             tempKeygen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
47             tempKeygen.initialize(KEY_LENGTH, SECURE_RANDOM);
48         } catch (NoSuchAlgorithmException e) {
49             tempKeygen = null;
50             LOGGER.error("Given KEY_ALGORITHM is invalid.", e);
51         }
52         KEY_PAIR_GENERATOR = tempKeygen;
53     }
54
55     private Factory() {
56     }
57
58     public static KeyPair generateKeyPair() {
59         return KEY_PAIR_GENERATOR.generateKeyPair();
60     }
61 }