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
8 * http://www.apache.org/licenses/LICENSE-2.0
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============================================
17 package org.onap.ccsdk.distribution.lighty;
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;
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}.
36 public class AAAEncryptionServiceLightyImpl implements AAAEncryptionService {
38 private static final Logger LOG = LoggerFactory.getLogger(AAAEncryptionServiceLightyImpl.class);
40 private final Cipher encryptCipher;
41 private final Cipher decryptCipher;
43 public AAAEncryptionServiceLightyImpl(Cipher encryptCipher, Cipher decryptCipher) {
44 this.encryptCipher = encryptCipher;
45 this.decryptCipher = decryptCipher;
49 public String encrypt(String data) {
50 if (data != null && data.length() != 0 ) {
52 synchronized (encryptCipher) {
53 byte[] cryptobytes = encryptCipher.doFinal(data.getBytes(Charset.defaultCharset()));
54 String cryptostring = DatatypeConverter.printBase64Binary(cryptobytes);
57 } catch (IllegalBlockSizeException | BadPaddingException e) {
58 LOG.error("Failed to encrypt data.", e);
62 LOG.warn("data is empty or null.");
68 public byte[] encrypt(byte[] data) {
69 if (data != null && data.length != 0) {
71 synchronized (encryptCipher) {
72 return encryptCipher.doFinal(data);
74 } catch (IllegalBlockSizeException | BadPaddingException e) {
75 LOG.error("Failed to encrypt data.", e);
79 LOG.warn("data is empty or null.");
85 public String decrypt(String encryptedData) {
86 if (encryptedData != null && encryptedData.length() != 0) {
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);
96 LOG.warn("encryptedData is empty or null.");
102 public byte[] decrypt(byte[] encryptedData) {
103 if (encryptedData != null && encryptedData.length != 0) {
105 return decryptCipher.doFinal(encryptedData);
106 } catch (IllegalBlockSizeException | BadPaddingException e) {
107 LOG.error("Failed to decrypt encoded data", e);
108 return encryptedData;
111 LOG.warn("encryptedData is empty or null.");
112 return encryptedData;