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);
40 * Instantiates a new encryptor.
48 * @param value to encrypt
49 * @return the encrypted string
51 public String encryptValue(String value) {
52 String encyptedValue = "";
54 encyptedValue = Password.obfuscate(value);
55 } catch (Exception exc) {
56 LOG.error(AaiUiMsgs.ENCRYPTION_ERROR, value, exc.toString());
64 * @param value the value
67 public String decryptValue(String value) {
68 String decyptedValue = "";
70 decyptedValue = Password.deobfuscate(value);
71 } catch (Exception exc) {
72 LOG.error(AaiUiMsgs.DECRYPTION_ERROR, value, exc.toString());
81 public static void usage() {
90 public static void usage(String msg) {
92 System.err.println(msg);
94 System.err.println("Usage: java Encryptor -e value");
95 System.err.println("\tEncrypt the given value");
96 System.err.println("Usage: java Encryptor -d value");
97 System.err.println("\tDecrypt the given value");
104 * @param args the arguments
106 public static void main(String[] args) {
108 Options options = new Options();
109 options.addOption("d", true, "value to decrypt");
110 options.addOption("h", false, "show help");
111 options.addOption("?", false, "show help");
114 boolean encrypt = false;
115 boolean decrypt = false;
117 CommandLineParser parser = new BasicParser();
118 CommandLine cmd = null;
121 cmd = parser.parse(options, args);
123 if (cmd.hasOption("d")) {
124 value = cmd.getOptionValue("d");
128 if (cmd.hasOption("?") || cmd.hasOption("h")) {
133 if ((encrypt && decrypt) || (!encrypt && !decrypt)) {
134 usage("Must specify one (and only one) of the -e or -d options");
137 Encryptor encryptor = new Encryptor();
140 String out = encryptor.decryptValue(value);
141 System.out.println(out);
143 } catch (ParseException exc) {
144 System.out.println("Failed to parse command line properties: " + exc.toString());
145 } catch (Exception exc) {
146 System.out.println("Failure: " + exc.toString());