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