blob: 0e30d07cb68df577a68f143d8e571a171ae34873 (
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
|
/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2017-2018 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.
*
* ============LICENSE_END=========================================================
*/
package org.onap.appc.adapter.chef.chefclient.impl;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Utils {
private final static Logger logger = LoggerFactory.getLogger(Utils.class);
private Utils(){}
public static String sha1AndBase64(String inStr) {
MessageDigest md = null;
byte[] outbty = null;
try {
md = MessageDigest.getInstance("SHA-1");
byte[] digest = md.digest(inStr.getBytes());
outbty = Base64.encode(digest);
} catch (NoSuchAlgorithmException nsae) {
logger.error(nsae.getMessage());
}
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);
String tmp = new String(outStr);
} catch (InvalidKeyException | IOException | SignatureException | NoSuchAlgorithmException e) {
logger.error(e.getMessage());
}
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;
}
}
|