1 |
|
package org.europa.together.application; |
2 |
|
|
3 |
|
import java.io.FileOutputStream; |
4 |
|
import java.io.OutputStream; |
5 |
|
import java.io.Serializable; |
6 |
|
import java.nio.file.Files; |
7 |
|
import java.nio.file.Path; |
8 |
|
import java.nio.file.Paths; |
9 |
|
import java.security.KeyFactory; |
10 |
|
import java.security.KeyPair; |
11 |
|
import java.security.KeyPairGenerator; |
12 |
|
import java.security.MessageDigest; |
13 |
|
import java.security.NoSuchAlgorithmException; |
14 |
|
import java.security.PrivateKey; |
15 |
|
import java.security.PublicKey; |
16 |
|
import java.security.SecureRandom; |
17 |
|
import java.security.Security; |
18 |
|
import java.security.spec.PKCS8EncodedKeySpec; |
19 |
|
import java.security.spec.X509EncodedKeySpec; |
20 |
|
import org.europa.together.business.CryptoTools; |
21 |
|
import org.europa.together.business.Logger; |
22 |
|
import org.europa.together.domain.CipherAlgorithm; |
23 |
|
import org.europa.together.domain.HashAlgorithm; |
24 |
|
import org.europa.together.domain.LogLevel; |
25 |
|
import org.europa.together.utils.Constraints; |
26 |
|
import org.europa.together.utils.StringUtils; |
27 |
|
import org.springframework.stereotype.Repository; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
@Repository |
|
|
| 100% |
Uncovered Elements: 0 (66) |
Complexity: 13 |
Complexity Density: 0.23 |
|
33 |
|
public class JavaCryptoTools implements CryptoTools, Serializable { |
34 |
|
|
35 |
|
private static final long serialVersionUID = 14L; |
36 |
|
private static final Logger LOGGER = new LogbackLogger(JavaCryptoTools.class); |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
41 |
1 |
public JavaCryptoTools() {... |
42 |
1 |
Security.setProperty("crypto.policy", "unlimited"); |
43 |
1 |
LOGGER.log("instance class", LogLevel.INFO); |
44 |
|
} |
45 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
46 |
314 |
@Override... |
47 |
|
public String calculateHash(final String plainText, |
48 |
|
final HashAlgorithm algorithm) { |
49 |
314 |
String hash = null; |
50 |
314 |
try { |
51 |
314 |
MessageDigest md = MessageDigest.getInstance(algorithm.toString()); |
52 |
313 |
md.reset(); |
53 |
313 |
hash = StringUtils.byteToString(md.digest(plainText.getBytes("UTF-8"))); |
54 |
312 |
String msg = "Utils.calculateHash(" + algorithm.toString() + ")" |
55 |
|
+ " plaintext: " + plainText + " hash: " + hash; |
56 |
312 |
LOGGER.log(msg, LogLevel.DEBUG); |
57 |
|
} catch (Exception ex) { |
58 |
2 |
LOGGER.catchException(ex); |
59 |
|
|
60 |
|
} |
61 |
314 |
return hash; |
62 |
|
} |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
1 |
@Override... |
65 |
|
public int getMaxKeySize(final CipherAlgorithm cipher) |
66 |
|
throws NoSuchAlgorithmException { |
67 |
1 |
return javax.crypto.Cipher.getMaxAllowedKeyLength(cipher.toString()); |
68 |
|
} |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
70 |
4 |
@Override... |
71 |
|
public KeyPair generateCipherKeyPair(final CipherAlgorithm cipher) { |
72 |
4 |
KeyPair pair = null; |
73 |
4 |
try { |
74 |
4 |
int lenght = Constraints.INT_4096; |
75 |
4 |
KeyPairGenerator keyring = KeyPairGenerator.getInstance(cipher.toString()); |
76 |
3 |
keyring.initialize(lenght, new SecureRandom()); |
77 |
3 |
pair = keyring.generateKeyPair(); |
78 |
|
} catch (Exception ex) { |
79 |
1 |
LOGGER.catchException(ex); |
80 |
|
} |
81 |
4 |
return pair; |
82 |
|
} |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 3 |
Complexity Density: 0.16 |
|
84 |
3 |
@Override... |
85 |
|
public void saveKeyPairToFile(final String path, final KeyPair keyRing) { |
86 |
|
|
87 |
3 |
String destination = Constraints.SYSTEM_USER_HOME_DIR; |
88 |
3 |
if (!StringUtils.isEmpty(path)) { |
89 |
2 |
destination = path; |
90 |
|
} |
91 |
3 |
OutputStream privateFile = null; |
92 |
3 |
OutputStream publicFile = null; |
93 |
3 |
try { |
94 |
3 |
byte[] publicKey = keyRing.getPublic().getEncoded(); |
95 |
2 |
byte[] privateKey = keyRing.getPrivate().getEncoded(); |
96 |
2 |
String privateCipher = keyRing.getPrivate().getAlgorithm(); |
97 |
2 |
privateFile = new FileOutputStream(destination + "/" |
98 |
|
+ privateCipher + ".key"); |
99 |
2 |
privateFile.write(privateKey); |
100 |
2 |
privateFile.close(); |
101 |
2 |
LOGGER.log("Private Key stored in PKCS#8 format.", LogLevel.DEBUG); |
102 |
2 |
String publicCipher = keyRing.getPublic().getAlgorithm(); |
103 |
2 |
publicFile = new FileOutputStream(destination + "/" |
104 |
|
+ publicCipher + ".pub"); |
105 |
2 |
publicFile.write(publicKey); |
106 |
2 |
publicFile.close(); |
107 |
2 |
LOGGER.log("Public Kex stored in X.509 format.", LogLevel.DEBUG); |
108 |
|
} catch (Exception ex) { |
109 |
1 |
LOGGER.catchException(ex); |
110 |
|
} |
111 |
|
} |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
113 |
2 |
@Override... |
114 |
|
public PrivateKey loadPrivateKeyFile(final String keyFile, final CipherAlgorithm algorithm) { |
115 |
2 |
PrivateKey key = null; |
116 |
2 |
try { |
117 |
2 |
Path path = Paths.get(keyFile); |
118 |
1 |
byte[] bytes = Files.readAllBytes(path); |
119 |
1 |
PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(bytes); |
120 |
1 |
KeyFactory kf = KeyFactory.getInstance(algorithm.toString()); |
121 |
1 |
key = kf.generatePrivate(ks); |
122 |
|
} catch (Exception ex) { |
123 |
1 |
LOGGER.catchException(ex); |
124 |
|
} |
125 |
2 |
return key; |
126 |
|
} |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
128 |
2 |
@Override... |
129 |
|
public PublicKey loadPublicKeyFile(final String keyFile, final CipherAlgorithm algorithm) { |
130 |
2 |
PublicKey key = null; |
131 |
2 |
try { |
132 |
2 |
Path path = Paths.get(keyFile); |
133 |
1 |
byte[] bytes = Files.readAllBytes(path); |
134 |
1 |
X509EncodedKeySpec ks = new X509EncodedKeySpec(bytes); |
135 |
1 |
KeyFactory kf = KeyFactory.getInstance(algorithm.toString()); |
136 |
1 |
key = kf.generatePublic(ks); |
137 |
|
} catch (Exception ex) { |
138 |
1 |
LOGGER.catchException(ex); |
139 |
|
} |
140 |
2 |
return key; |
141 |
|
} |
142 |
|
} |