Merge "Release version 0.6.0"
[ccsdk/distribution.git] / lighty / ccsdk-lighty-module / src / main / java / org / onap / ccsdk / distribution / lighty / AAAEncryptionServiceLightyImpl.java
1 /*
2  * ============LICENSE_START==========================================
3  * Copyright (c) 2019 PANTHEON.tech s.r.o.
4  * ===================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6  * the License. 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 distributed under the License is distributed on
11  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
12  * OF ANY KIND, either express or implied. See the License for the specific language governing permissions and
13  * limitations under the License.
14  * ============LICENSE_END============================================
15  *
16  */
17 package org.onap.ccsdk.distribution.lighty;
18
19 import java.nio.charset.Charset;
20 import javax.crypto.BadPaddingException;
21 import javax.crypto.Cipher;
22 import javax.crypto.IllegalBlockSizeException;
23 import javax.xml.bind.DatatypeConverter;
24 import org.onap.ccsdk.sli.core.dblib.DBLIBResourceProvider;
25 import org.onap.ccsdk.sli.core.dblib.DBLIBResourceProviderLighty;
26 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Custom implementation of the {@link AAAEncryptionService}. Original class {@link DBLIBResourceProvider} was accessing
32  * instance of this interface via OGSi (BundleContext) and java reflection. The implementation of
33  * the {@link AAAEncryptionService} has to be injected via constructor into the lighty.io version of the class with
34  * removed OSGi dependency {@link DBLIBResourceProviderLighty}.
35  */
36 public class AAAEncryptionServiceLightyImpl implements AAAEncryptionService {
37
38     private static final Logger LOG = LoggerFactory.getLogger(AAAEncryptionServiceLightyImpl.class);
39
40     private final Cipher encryptCipher;
41     private final Cipher decryptCipher;
42
43     public AAAEncryptionServiceLightyImpl(Cipher encryptCipher, Cipher decryptCipher) {
44         this.encryptCipher = encryptCipher;
45         this.decryptCipher = decryptCipher;
46     }
47
48     @Override
49     public String encrypt(String data) {
50         if (data != null && data.length() != 0 ) {
51             try {
52                 synchronized (encryptCipher) {
53                     byte[] cryptobytes = encryptCipher.doFinal(data.getBytes(Charset.defaultCharset()));
54                     String cryptostring = DatatypeConverter.printBase64Binary(cryptobytes);
55                     return cryptostring;
56                 }
57             } catch (IllegalBlockSizeException | BadPaddingException e) {
58                 LOG.error("Failed to encrypt data.", e);
59                 return data;
60             }
61         } else {
62             LOG.warn("data is empty or null.");
63             return data;
64         }
65     }
66
67     @Override
68     public byte[] encrypt(byte[] data) {
69         if (data != null && data.length != 0) {
70             try {
71                 synchronized (encryptCipher) {
72                     return encryptCipher.doFinal(data);
73                 }
74             } catch (IllegalBlockSizeException | BadPaddingException e) {
75                 LOG.error("Failed to encrypt data.", e);
76                 return data;
77             }
78         } else {
79             LOG.warn("data is empty or null.");
80             return data;
81         }
82     }
83
84     @Override
85     public String decrypt(String encryptedData) {
86         if (encryptedData != null && encryptedData.length() != 0) {
87             try {
88                 byte[] cryptobytes = DatatypeConverter.parseBase64Binary(encryptedData);
89                 byte[] clearbytes = decryptCipher.doFinal(cryptobytes);
90                 return new String(clearbytes, Charset.defaultCharset());
91             } catch (IllegalBlockSizeException | BadPaddingException e) {
92                 LOG.error("Failed to decrypt encoded data", e);
93                 return encryptedData;
94             }
95         } else {
96             LOG.warn("encryptedData is empty or null.");
97             return encryptedData;
98         }
99     }
100
101     @Override
102     public byte[] decrypt(byte[] encryptedData) {
103         if (encryptedData != null && encryptedData.length != 0) {
104             try {
105                 return decryptCipher.doFinal(encryptedData);
106             } catch (IllegalBlockSizeException | BadPaddingException e) {
107                 LOG.error("Failed to decrypt encoded data", e);
108                 return encryptedData;
109             }
110         } else {
111             LOG.warn("encryptedData is empty or null.");
112             return encryptedData;
113         }
114     }
115
116 }

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use