Change nexus values to properties
[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.PEMReader;
39 import org.bouncycastle.util.encoders.Base64;
40
41 public class Utils {
42         private Utils(){}
43         
44         public static String sha1AndBase64(String inStr) {
45                 MessageDigest md = null;
46                 String outStr = null;
47                 byte[] outbty = null;
48                 try {
49                         md = MessageDigest.getInstance("SHA-1"); 
50                         byte[] digest = md.digest(inStr.getBytes()); 
51                         outbty = Base64.encode(digest);
52                 } catch (NoSuchAlgorithmException nsae) {
53                         nsae.printStackTrace();
54                 }
55                 return new String(outbty);
56         }
57         
58         public static String signWithRSA(String inStr, String pemPath) {
59                 byte[] outStr = null;
60                 BufferedReader br = null;
61                 try {
62                         br = new BufferedReader(new FileReader(pemPath));
63                 } catch (FileNotFoundException e) {
64                         e.printStackTrace();
65                 }
66                 Security.addProvider(new BouncyCastleProvider());
67                 try {
68
69                         KeyPair kp = (KeyPair) new PEMReader(br).readObject();
70                         PrivateKey privateKey = kp.getPrivate();
71                         Signature instance = Signature.getInstance("RSA");
72                         instance.initSign(privateKey);
73                         instance.update(inStr.getBytes());
74
75                         byte[] signature = instance.sign();
76                         outStr = Base64.encode(signature);
77                         String tmp = new String(outStr);
78                 } catch (InvalidKeyException e) {
79                         e.printStackTrace();
80                 } catch (IOException e) {
81                         e.printStackTrace();
82                 } catch (SignatureException e) {
83                         e.printStackTrace();
84                 } catch (NoSuchAlgorithmException e) {
85                         e.printStackTrace();
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
94                 for (int i = 0; i < count; i++) {
95                         String tmp = inStr.substring(i * 60, i * 60 + 60);
96                         out[i] = tmp;
97                 }
98                 if (inStr.length() > count * 60) {
99                         String tmp = inStr.substring(count * 60, inStr.length());
100                         out[count] = tmp;
101                 }
102                 return out;
103         }
104         
105 }