c3e59b9e8e86711f6bbc0b6d24d56cc19eb082fe
[vfc/nfvo/wfengine.git] / CommonLibrary / common-util / src / main / java / org / openo / baseservice / encrypt / cbb / CipherCreator.java
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
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 package org.openo.baseservice.encrypt.cbb;
17
18 import org.openo.baseservice.encrypt.cbb.impl.AesCipherFactory;
19 import org.openo.baseservice.encrypt.cbb.inf.AbstractCipher;
20 import org.openo.baseservice.encrypt.cbb.inf.AbstractCipherFactory;
21
22 /**
23  * Helps create cipher instances from factory.<br/>
24  * <p>
25  * Creates the cipher instances using cipher factory. By default it uses AesCipherFactory.
26  * Can be changed through spring.
27  * </p>
28  * 
29  * @author
30  * @version SDNO 0.5 08-Jun-2016
31  */
32 public final class CipherCreator {
33
34     private static CipherCreator instance = new CipherCreator();
35
36     private AbstractCipherFactory factory = new AesCipherFactory();
37
38     /**
39      * Constructor<br/>
40      * <p>
41      * private
42      * </p>
43      * 
44      * @since SDNO 0.5
45      */
46     private CipherCreator() {
47
48     }
49
50     /**
51      * Singleton instance.
52      * <br/>
53      * 
54      * @return
55      * @since SDNO 0.5
56      */
57     public static CipherCreator instance() {
58         return instance;
59     }
60
61     /**
62      * Creates cipher with default key.
63      * <br/>
64      * 
65      * @return cipher instance with default key.
66      * @since SDNO 0.5
67      */
68     public AbstractCipher create() {
69         return factory.createCipherManager();
70     }
71
72     /**
73      * Creates cipher instance with a key.
74      * <br/>
75      * 
76      * @param key the key to be used for encryption and decryption.
77      * @return cipher instance with specified key.
78      * @since SDNO 0.5
79      */
80     public AbstractCipher create(final String key) {
81         return factory.createCipherManager(key);
82     }
83
84     /**
85      * Sets the cipher factory instance.
86      * <br/>
87      * 
88      * @param factory cipher factory.
89      * @since SDNO 0.5
90      */
91     public void setFactory(final AbstractCipherFactory factory) {
92         this.factory = factory;
93     }
94
95 }