2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.sparky.util;
23 import org.apache.commons.cli.BasicParser;
24 import org.apache.commons.cli.CommandLine;
25 import org.apache.commons.cli.CommandLineParser;
26 import org.apache.commons.cli.Options;
27 import org.apache.commons.cli.ParseException;
28 import org.eclipse.jetty.util.security.Password;
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.eelf.LoggerFactory;
31 import org.onap.aai.sparky.logging.AaiUiMsgs;
34 * The Class Encryptor.
36 public class Encryptor {
38 private static final Logger LOG = LoggerFactory.getInstance().getLogger(Encryptor.class);
39 private static final String START_OBFUSCATION_PATTERN = "OBF:";
42 * Instantiates a new encryptor.
50 * @param value to encrypt
51 * @return the encrypted string
53 public String encryptValue(String value) {
54 String encyptedValue = "";
56 encyptedValue = Password.obfuscate(value);
57 } catch (Exception exc) {
58 LOG.error(AaiUiMsgs.ENCRYPTION_ERROR, value, exc.toString());
66 * @param value the value
69 public String decryptValue(String value) {
70 String decyptedValue = "";
72 decyptedValue = value.startsWith(START_OBFUSCATION_PATTERN)? Password.deobfuscate(value) : value;
73 } catch (Exception exc) {
74 LOG.error(AaiUiMsgs.DECRYPTION_ERROR, value, exc.toString());
83 public static void usage() {
92 public static void usage(String msg) {
94 System.err.println(msg);
96 System.err.println("Usage: java Encryptor -e value");
97 System.err.println("\tEncrypt the given value");
98 System.err.println("Usage: java Encryptor -d value");
99 System.err.println("\tDecrypt the given value");
106 * @param args the arguments
108 public static void main(String[] args) {
110 Options options = new Options();
111 options.addOption("d", true, "value to decrypt");
112 options.addOption("h", false, "show help");
113 options.addOption("?", false, "show help");
116 boolean encrypt = false;
117 boolean decrypt = false;
119 CommandLineParser parser = new BasicParser();
120 CommandLine cmd = null;
123 cmd = parser.parse(options, args);
125 if (cmd.hasOption("d")) {
126 value = cmd.getOptionValue("d");
130 if (cmd.hasOption("?") || cmd.hasOption("h")) {
135 if ((encrypt && decrypt) || (!encrypt && !decrypt)) {
136 usage("Must specify one (and only one) of the -e or -d options");
139 Encryptor encryptor = new Encryptor();
142 String out = encryptor.decryptValue(value);
143 System.out.println(out);
145 } catch (ParseException exc) {
146 System.out.println("Failed to parse command line properties: " + exc.toString());
147 } catch (Exception exc) {
148 System.out.println("Failure: " + exc.toString());