aboutsummaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/chefclient/Utils.java
blob: 393069dfd77d36e5cc84cf02623198f526b18e5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.appc.adapter.chef.chefclient;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Security;
import java.security.Signature;
import java.security.SignatureException;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.openssl.PEMKeyPair;

public class Utils {
    private Utils(){}
    
    public static String sha1AndBase64(String inStr) {
        MessageDigest md = null;
        String outStr = null;
        byte[] outbty = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            byte[] digest = md.digest(inStr.getBytes());
            outbty = Base64.encode(digest);
        } catch (NoSuchAlgorithmException nsae) {
            nsae.printStackTrace();
        }
        return new String(outbty);
    }
    
    public static String signWithRSA(String inStr, String pemPath) {
        byte[] outStr = null;
        try ( BufferedReader br  = new BufferedReader(new FileReader(pemPath))) {
            Security.addProvider(new BouncyCastleProvider());
            PEMParser pemParser = new PEMParser(br);
            JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
            Object object = pemParser.readObject();
            KeyPair kp =  converter.getKeyPair((PEMKeyPair) object);;
            PrivateKey privateKey = kp.getPrivate();
            Signature instance = Signature.getInstance("RSA");
            instance.initSign(privateKey);
            instance.update(inStr.getBytes());
            byte[] signature = instance.sign();
            outStr = Base64.encode(signature);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SignatureException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return new String(outStr);
    }
    
    public static String[] splitAs60(String inStr) {
        int count = inStr.length() / 60;
        String[] out = new String[count + 1];

        for (int i = 0; i < count; i++) {
            String tmp = inStr.substring(i * 60, i * 60 + 60);
            out[i] = tmp;
        }
        if (inStr.length() > count * 60) {
            String tmp = inStr.substring(count * 60, inStr.length());
            out[count] = tmp;
        }
        return out;
    }
}