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