Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / util / Encryptor.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.util;
27
28 import org.apache.commons.cli.BasicParser;
29 import org.apache.commons.cli.CommandLine;
30 import org.apache.commons.cli.CommandLineParser;
31 import org.apache.commons.cli.Options;
32 import org.apache.commons.cli.ParseException;
33 import org.eclipse.jetty.util.security.Password;
34
35 /**
36  * The Class Encryptor.
37  */
38 public class Encryptor {
39
40   /**
41    * Instantiates a new encryptor.
42    */
43   public Encryptor() {  
44   }
45   
46   /**
47    * Decrypt value.
48    *
49    * @param value the value
50    * @return the string
51    */
52   public String decryptValue(String value) {
53     String decyptedValue = "";
54     
55     try {
56       decyptedValue = Password.deobfuscate(value);
57     } catch (Exception exc) {
58       System.err.println("Cannot decrypt '" + value + "': " + exc.toString());
59     }
60
61     return decyptedValue;
62   }
63
64   /**
65    * Usage.
66    */
67   public static void usage() {
68     usage(null);
69   }
70
71   /**
72    * Usage.
73    *
74    * @param msg the msg
75    */
76   public static void usage(String msg) {
77     if (msg != null) {
78       System.err.println(msg);
79     }
80     System.err.println("Usage: java Encryptor -e value");
81     System.err.println("\tEncrypt the given value");
82     System.err.println("Usage: java Encryptor -d value");
83     System.err.println("\tDecrypt the given value");
84     System.exit(1);
85   }
86
87   /**
88    * The main method.
89    *
90    * @param args the arguments
91    */
92   public static void main(String[] args) {
93
94     Options options = new Options();
95     options.addOption("d", true, "value to decrypt");
96     options.addOption("h", false, "show help");
97     options.addOption("?", false, "show help");
98
99     String value = null;
100     boolean encrypt = false;
101     boolean decrypt = false;
102
103     CommandLineParser parser = new BasicParser();
104     CommandLine cmd = null;
105
106     try {
107       cmd = parser.parse(options, args);
108
109       if (cmd.hasOption("d")) {
110         value = cmd.getOptionValue("d");
111         decrypt = true;
112       }
113
114       if (cmd.hasOption("?") || cmd.hasOption("h")) {
115         usage();
116         System.exit(0);
117       }
118
119       if ((encrypt && decrypt) || (!encrypt && !decrypt)) {
120         usage("Must specify one (and only one) of the -e or -d options");
121       }
122
123       Encryptor encryptor = new Encryptor();
124
125       if (decrypt) {
126         String out = encryptor.decryptValue(value);
127         System.out.println(out);
128       }
129     } catch (ParseException exc) {
130       System.out.println("Failed to parse command line properties: " + exc.toString());
131     } catch (Exception exc) {
132       System.out.println("Failure: " + exc.toString());
133     }
134
135     System.exit(0);
136   }
137 }