618956fcc50b4d0d86c125858cc4a7eccb54d88f
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.adapter.chef.chefclient;
24
25 import java.io.BufferedReader;
26 import java.io.FileNotFoundException;
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
44 public class Utils {
45         private Utils(){}
46         
47         public static String sha1AndBase64(String inStr) {
48                 MessageDigest md = null;
49                 String outStr = null;
50                 byte[] outbty = null;
51                 try {
52                         md = MessageDigest.getInstance("SHA-1"); 
53                         byte[] digest = md.digest(inStr.getBytes()); 
54                         outbty = Base64.encode(digest);
55                 } catch (NoSuchAlgorithmException nsae) {
56                         nsae.printStackTrace();
57                 }
58                 return new String(outbty);
59         }
60         
61         public static String signWithRSA(String inStr, String pemPath) {
62                 byte[] outStr = null;
63                 BufferedReader br = null;
64                 try {
65                         br = new BufferedReader(new FileReader(pemPath));
66                 } catch (FileNotFoundException e) {
67                         e.printStackTrace();
68                 }
69                 Security.addProvider(new BouncyCastleProvider());
70                 try {
71
72                         PEMParser pemParser = new PEMParser(br);
73                         JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
74                         Object object = pemParser.readObject();
75                         KeyPair kp =  converter.getKeyPair((PEMKeyPair) object);;
76                         PrivateKey privateKey = kp.getPrivate();
77                         Signature instance = Signature.getInstance("RSA");
78                         instance.initSign(privateKey);
79                         instance.update(inStr.getBytes());
80
81                         byte[] signature = instance.sign();
82                         outStr = Base64.encode(signature);
83                         String tmp = new String(outStr);
84                 } catch (InvalidKeyException e) {
85                         e.printStackTrace();
86                 } catch (IOException e) {
87                         e.printStackTrace();
88                 } catch (SignatureException e) {
89                         e.printStackTrace();
90                 } catch (NoSuchAlgorithmException e) {
91                         e.printStackTrace();
92                 }
93                 return new String(outStr);
94         }
95         
96         public static String[] splitAs60(String inStr) {
97                 int count = inStr.length() / 60;
98                 String[] out = new String[count + 1];
99
100                 for (int i = 0; i < count; i++) {
101                         String tmp = inStr.substring(i * 60, i * 60 + 60);
102                         out[i] = tmp;
103                 }
104                 if (inStr.length() > count * 60) {
105                         String tmp = inStr.substring(count * 60, inStr.length());
106                         out[count] = tmp;
107                 }
108                 return out;
109         }
110         
111 }