Fix Clamp crash
[clamp.git] / src / main / java / org / onap / clamp / clds / util / CryptoUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.util;
25
26 import java.security.GeneralSecurityException;
27
28 import javax.crypto.Cipher;
29 import javax.crypto.spec.SecretKeySpec;
30
31 import org.springframework.core.Ordered;
32 import org.springframework.core.annotation.Order;
33 import org.springframework.stereotype.Component;
34
35 /**
36  * CryptoUtils for encrypting/decrypting string based on a Key defined in
37  * application.properties (Spring config file).
38  * 
39  */
40 @Component("CryptoUtils")
41 @Order(Ordered.HIGHEST_PRECEDENCE)
42 public final class CryptoUtils {
43     public static final String AES           = "AES";
44     public static final String KEY_PARAM     = "org.onap.clamp.encryption.aes.key";
45     private SecretKeySpec      secretKeySpec = getSecretKeySpec("aa3871669d893c7fb8abbcda31b88b4f");
46
47     /**
48      * Encrypt a value based on the Clamp Encryption Key.
49      * 
50      * @param value
51      * @return The encrypted string
52      * @throws GeneralSecurityException
53      *             In case of issue with the encryption
54      */
55     public String encrypt(String value) throws GeneralSecurityException {
56         Cipher cipher = Cipher.getInstance(CryptoUtils.AES);
57         cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, cipher.getParameters());
58         byte[] encrypted = cipher.doFinal(value.getBytes());
59         return byteArrayToHexString(encrypted);
60     }
61
62     /**
63      * Decrypt a value.
64      * 
65      * @param message
66      *            The encrypted string that must be decrypted using the Clamp
67      *            Encryption Key
68      * @return The String decrypted
69      * @throws GeneralSecurityException
70      *             In case of issue with the encryption
71      */
72     public String decrypt(String message) throws GeneralSecurityException {
73         Cipher cipher = Cipher.getInstance(CryptoUtils.AES);
74         cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
75         byte[] decrypted = cipher.doFinal(hexStringToByteArray(message));
76         return new String(decrypted);
77     }
78
79     private SecretKeySpec getSecretKeySpec(String keyString) {
80         byte[] key = hexStringToByteArray(keyString);
81         return new SecretKeySpec(key, CryptoUtils.AES);
82     }
83
84     private String byteArrayToHexString(byte[] b) {
85         StringBuilder sb = new StringBuilder(b.length * 2);
86         for (int i = 0; i < b.length; i++) {
87             int v = b[i] & 0xff;
88             if (v < 16) {
89                 sb.append('0');
90             }
91             sb.append(Integer.toHexString(v));
92         }
93         return sb.toString().toUpperCase();
94     }
95
96     private byte[] hexStringToByteArray(String s) {
97         byte[] b = new byte[s.length() / 2];
98         for (int i = 0; i < b.length; i++) {
99             int index = i * 2;
100             int v = Integer.parseInt(s.substring(index, index + 2), 16);
101             b[i] = (byte) v;
102         }
103         return b;
104     }
105 }