2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.util;
27 import org.apache.commons.cli.BasicParser;
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.CommandLineParser;
30 import org.apache.commons.cli.Options;
31 import org.apache.commons.cli.ParseException;
32 import org.eclipse.jetty.util.security.Password;
33 import org.onap.aai.cl.api.Logger;
34 import org.onap.aai.cl.eelf.LoggerFactory;
35 import org.onap.aai.sparky.logging.AaiUiMsgs;
38 * The Class Encryptor.
40 public class Encryptor {
42 private static final Logger LOG = LoggerFactory.getInstance().getLogger(Encryptor.class);
44 * Instantiates a new encryptor.
52 * @param value to encrypt
53 * @return the encrypted string
55 public String encryptValue(String value) {
56 String encyptedValue = "";
58 encyptedValue = Password.obfuscate(value);
59 } catch (Exception exc) {
60 LOG.error(AaiUiMsgs.ENCRYPTION_ERROR, value, exc.toString());
68 * @param value the value
71 public String decryptValue(String value) {
72 String decyptedValue = "";
74 decyptedValue = Password.deobfuscate(value);
75 } catch (Exception exc) {
76 LOG.error(AaiUiMsgs.DECRYPTION_ERROR, value, exc.toString());
85 public static void usage() {
94 public static void usage(String msg) {
96 System.err.println(msg);
98 System.err.println("Usage: java Encryptor -e value");
99 System.err.println("\tEncrypt the given value");
100 System.err.println("Usage: java Encryptor -d value");
101 System.err.println("\tDecrypt the given value");
108 * @param args the arguments
110 public static void main(String[] args) {
112 Options options = new Options();
113 options.addOption("d", true, "value to decrypt");
114 options.addOption("h", false, "show help");
115 options.addOption("?", false, "show help");
118 boolean encrypt = false;
119 boolean decrypt = false;
121 CommandLineParser parser = new BasicParser();
122 CommandLine cmd = null;
125 cmd = parser.parse(options, args);
127 if (cmd.hasOption("d")) {
128 value = cmd.getOptionValue("d");
132 if (cmd.hasOption("?") || cmd.hasOption("h")) {
137 if ((encrypt && decrypt) || (!encrypt && !decrypt)) {
138 usage("Must specify one (and only one) of the -e or -d options");
141 Encryptor encryptor = new Encryptor();
144 String out = encryptor.decryptValue(value);
145 System.out.println(out);
147 } catch (ParseException exc) {
148 System.out.println("Failed to parse command line properties: " + exc.toString());
149 } catch (Exception exc) {
150 System.out.println("Failure: " + exc.toString());