169b2c13d24b88b7f8de84058bf7c27e23b687a3
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / main / java / org / onap / appc / adapter / chef / chefclient / impl / Utils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 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
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  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.chef.chefclient.impl;
25
26 import java.io.BufferedReader;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.security.InvalidKeyException;
30 import java.security.KeyPair;
31 import java.security.MessageDigest;
32 import java.security.NoSuchAlgorithmException;
33 import java.security.PrivateKey;
34 import java.security.Security;
35 import java.security.Signature;
36 import java.security.SignatureException;
37
38 import org.bouncycastle.jce.provider.BouncyCastleProvider;
39 import org.bouncycastle.openssl.PEMParser;
40 import org.bouncycastle.util.encoders.Base64;
41 import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
42 import org.bouncycastle.openssl.PEMKeyPair;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class Utils {
47     private final static Logger logger = LoggerFactory.getLogger(Utils.class);
48     private Utils(){}
49     
50     public static String sha1AndBase64(String inStr) {
51         MessageDigest md = null;
52         byte[] outbty = null;
53         try {
54             md = MessageDigest.getInstance("SHA-1");
55             byte[] digest = md.digest(inStr.getBytes());
56             outbty = Base64.encode(digest);
57         } catch (NoSuchAlgorithmException nsae) {
58             logger.error(nsae.getMessage());
59         }
60         return new String(outbty);
61     }
62     
63     public static String signWithRSA(String inStr, String pemPath) {
64         byte[] outStr = null;
65         try ( BufferedReader br  = new BufferedReader(new FileReader(pemPath))) {
66             Security.addProvider(new BouncyCastleProvider());
67             PEMParser pemParser = new PEMParser(br);
68             JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
69             Object object = pemParser.readObject();
70             KeyPair kp =  converter.getKeyPair((PEMKeyPair) object);;
71             PrivateKey privateKey = kp.getPrivate();
72             Signature instance = Signature.getInstance("RSA");
73             instance.initSign(privateKey);
74             instance.update(inStr.getBytes());
75             byte[] signature = instance.sign();
76             outStr = Base64.encode(signature);
77             String tmp = new String(outStr);
78         } catch (InvalidKeyException e) {
79             logger.error(e.getMessage());
80         } catch (IOException e) {
81             logger.error(e.getMessage());
82         } catch (SignatureException e) {
83             logger.error(e.getMessage());
84         } catch (NoSuchAlgorithmException e) {
85             logger.error(e.getMessage());
86         }
87         return new String(outStr);
88     }
89     
90     public static String[] splitAs60(String inStr) {
91         int count = inStr.length() / 60;
92         String[] out = new String[count + 1];
93         for (int i = 0; i < count; i++) {
94             String tmp = inStr.substring(i * 60, i * 60 + 60);
95             out[i] = tmp;
96         }
97         if (inStr.length() > count * 60) {
98             String tmp = inStr.substring(count * 60, inStr.length());
99             out[count] = tmp;
100         }
101         return out;
102     }
103     
104 }