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