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