2 * ============LICENSE_START=======================================================
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 * =============================================================================
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
16 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 * ============LICENSE_END=========================================================
27 package org.onap.appc.adapter.chef.chefclient.impl;
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;
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;
50 private static final Logger logger = LoggerFactory.getLogger(Utils.class);
53 public static String sha1AndBase64(String inStr) {
54 MessageDigest md = null;
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());
63 return new String(outbty);
66 public static String signWithRSA(String inStr, String pemPath) {
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());
84 return new String(outStr);
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);
94 if (inStr.length() > count * 60) {
95 String tmp = inStr.substring(count * 60, inStr.length());