0e30d07cb68df577a68f143d8e571a171ae34873
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / main / java / org / onap / appc / adapter / chef / chefclient / impl / Utils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * 
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.chef.chefclient.impl;
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 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class Utils {
48     private final static Logger logger = LoggerFactory.getLogger(Utils.class);
49     private Utils(){}
50     
51     public static String sha1AndBase64(String inStr) {
52         MessageDigest md = null;
53         byte[] outbty = null;
54         try {
55             md = MessageDigest.getInstance("SHA-1");
56             byte[] digest = md.digest(inStr.getBytes());
57             outbty = Base64.encode(digest);
58         } catch (NoSuchAlgorithmException nsae) {
59             logger.error(nsae.getMessage());
60         }
61         return new String(outbty);
62     }
63     
64     public static String signWithRSA(String inStr, String pemPath) {
65         byte[] outStr = null;
66         try ( BufferedReader br  = new BufferedReader(new FileReader(pemPath))) {
67             Security.addProvider(new BouncyCastleProvider());
68             PEMParser pemParser = new PEMParser(br);
69             JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
70             Object object = pemParser.readObject();
71             KeyPair kp =  converter.getKeyPair((PEMKeyPair) object);;
72             PrivateKey privateKey = kp.getPrivate();
73             Signature instance = Signature.getInstance("RSA");
74             instance.initSign(privateKey);
75             instance.update(inStr.getBytes());
76             byte[] signature = instance.sign();
77             outStr = Base64.encode(signature);
78             String tmp = new String(outStr);
79         } catch (InvalidKeyException | IOException | SignatureException | NoSuchAlgorithmException e) {
80             logger.error(e.getMessage());
81         }
82         return new String(outStr);
83     }
84     
85     public static String[] splitAs60(String inStr) {
86         int count = inStr.length() / 60;
87         String[] out = new String[count + 1];
88         for (int i = 0; i < count; i++) {
89             String tmp = inStr.substring(i * 60, i * 60 + 60);
90             out[i] = tmp;
91         }
92         if (inStr.length() > count * 60) {
93             String tmp = inStr.substring(count * 60, inStr.length());
94             out[count] = tmp;
95         }
96         return out;
97     }
98     
99 }