86f57dceea23fa0aefd570b0bc8d777ad8913bd1
[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.annotation.PostConstruct;
29 import javax.crypto.Cipher;
30 import javax.crypto.spec.SecretKeySpec;
31
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.core.env.Environment;
34 import org.springframework.core.annotation.Order;
35 import org.springframework.core.Ordered;
36 import org.springframework.stereotype.Component;
37
38 /**
39  * CryptoUtils for encrypting/decrypting string based on a Key defined in
40  * application.properties (Spring config file).
41  * 
42  */
43 @Component("CryptoUtils")
44 @Order(Ordered.HIGHEST_PRECEDENCE)
45 public final class CryptoUtils {
46     public static final String AES       = "AES";
47     public static final String KEY_PARAM = "org.onap.clamp.encryption.aes.key";
48     @Autowired
49     private Environment        springEnv;
50     private SecretKeySpec      secretKeySpec;
51
52     /**
53      * Initialize Method
54      * 
55      */
56     @PostConstruct
57     public void init() {
58         secretKeySpec = getSecretKeySpec(springEnv.getProperty(KEY_PARAM));
59     }
60
61     /**
62      * Encrypt a value based on the Clamp Encryption Key.
63      * 
64      * @param value
65      * @return The encrypted string
66      * @throws GeneralSecurityException
67      *             In case of issue with the encryption
68      */
69     public String encrypt(String value) throws GeneralSecurityException {
70         Cipher cipher = Cipher.getInstance(CryptoUtils.AES);
71         cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, cipher.getParameters());
72         byte[] encrypted = cipher.doFinal(value.getBytes());
73         return byteArrayToHexString(encrypted);
74     }
75
76     /**
77      * Decrypt a value.
78      * 
79      * @param message
80      *            The encrypted string that must be decrypted using the Clamp
81      *            Encryption Key
82      * @return The String decrypted
83      * @throws GeneralSecurityException
84      *             In case of issue with the encryption
85      */
86     public String decrypt(String message) throws GeneralSecurityException {
87         Cipher cipher = Cipher.getInstance(CryptoUtils.AES);
88         cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
89         byte[] decrypted = cipher.doFinal(hexStringToByteArray(message));
90         return new String(decrypted);
91     }
92
93     private SecretKeySpec getSecretKeySpec(String keyString) {
94         byte[] key = hexStringToByteArray(keyString);
95         return new SecretKeySpec(key, CryptoUtils.AES);
96     }
97
98     private String byteArrayToHexString(byte[] b) {
99         StringBuilder sb = new StringBuilder(b.length * 2);
100         for (int i = 0; i < b.length; i++) {
101             int v = b[i] & 0xff;
102             if (v < 16) {
103                 sb.append('0');
104             }
105             sb.append(Integer.toHexString(v));
106         }
107         return sb.toString().toUpperCase();
108     }
109
110     private byte[] hexStringToByteArray(String s) {
111         byte[] b = new byte[s.length() / 2];
112         for (int i = 0; i < b.length; i++) {
113             int index = i * 2;
114             int v = Integer.parseInt(s.substring(index, index + 2), 16);
115             b[i] = (byte) v;
116         }
117         return b;
118     }
119 }