1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.application

File JavaCryptoTools.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
65
8
1
163
139
15
0.23
8.12
8
1.88

Classes

Class Line # Actions
JavaCryptoTools 34 65 0% 15 0
1.0100%
 

Contributing tests

This file is covered by 52 tests. .

Source view

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.domain.RNGAlgorithm;
26    import org.europa.together.utils.Constraints;
27    import org.europa.together.utils.StringUtils;
28    import org.springframework.stereotype.Repository;
29   
30    /**
31    * Implementation of Java cryptography.
32    */
33    @Repository
 
34    public class JavaCryptoTools implements CryptoTools, Serializable {
35   
36    private static final long serialVersionUID = 14L;
37    private static final Logger LOGGER = new LogbackLogger(JavaCryptoTools.class);
38   
39    /**
40    * Constructor.
41    */
 
42  1 toggle public JavaCryptoTools() {
43  1 Security.setProperty("crypto.policy", "unlimited");
44  1 LOGGER.log("instance class", LogLevel.INFO);
45    }
46   
 
47  567 toggle @Override
48    public String calculateHash(final String plainText,
49    final HashAlgorithm algorithm) {
50  567 String hash = null;
51  567 try {
52  567 String hashAlgorithm = algorithm.getValue();
53   
54  566 MessageDigest md = MessageDigest.getInstance(hashAlgorithm);
55  566 md.reset();
56  566 hash = StringUtils.byteToString(md.digest(plainText.getBytes("UTF-8")));
57  565 String msg = "Utils.calculateHash(" + algorithm.toString() + ")"
58    + " plaintext: " + plainText + " hash: " + hash;
59   
60  565 LOGGER.log(msg, LogLevel.DEBUG);
61    } catch (Exception ex) {
62  2 LOGGER.catchException(ex);
63   
64    }
65  567 return hash;
66    }
67   
 
68  1 toggle @Override
69    public int getMaxKeySize(final CipherAlgorithm cipher)
70    throws NoSuchAlgorithmException {
71  1 return javax.crypto.Cipher.getMaxAllowedKeyLength(cipher.toString());
72    }
73   
 
74  4 toggle @Override
75    public KeyPair generateCipherKeyPair(final CipherAlgorithm cipher) {
76  4 KeyPair pair = null;
77  4 try {
78  4 int lenght = Constraints.INT_4096;
79  4 KeyPairGenerator keyring = KeyPairGenerator.getInstance(cipher.toString());
80  3 keyring.initialize(lenght, new SecureRandom());
81  3 pair = keyring.generateKeyPair();
82    } catch (Exception ex) {
83  1 LOGGER.catchException(ex);
84    }
85  4 return pair;
86    }
87   
 
88  3 toggle @Override
89    public void saveKeyPairToFile(final String path, final KeyPair keyRing) {
90    //FALLBACK
91  3 String destination = Constraints.SYSTEM_USER_HOME_DIR;
92  3 if (!StringUtils.isEmpty(path)) {
93  2 destination = path;
94    }
95  3 OutputStream privateFile = null;
96  3 OutputStream publicFile = null;
97  3 try {
98  3 byte[] publicKey = keyRing.getPublic().getEncoded();
99  2 byte[] privateKey = keyRing.getPrivate().getEncoded();
100  2 String privateCipher = keyRing.getPrivate().getAlgorithm();
101  2 privateFile = new FileOutputStream(destination + "/"
102    + privateCipher + ".key");
103  2 privateFile.write(privateKey);
104  2 privateFile.close();
105  2 LOGGER.log("Private Key stored in PKCS#8 format.", LogLevel.DEBUG);
106  2 String publicCipher = keyRing.getPublic().getAlgorithm();
107  2 publicFile = new FileOutputStream(destination + "/"
108    + publicCipher + ".pub");
109  2 publicFile.write(publicKey);
110  2 publicFile.close();
111  2 LOGGER.log("Public Kex stored in X.509 format.", LogLevel.DEBUG);
112    } catch (Exception ex) {
113  1 LOGGER.catchException(ex);
114    }
115    }
116   
 
117  2 toggle @Override
118    public PrivateKey loadPrivateKeyFile(final String keyFile, final CipherAlgorithm algorithm) {
119  2 PrivateKey key = null;
120  2 try {
121  2 Path path = Paths.get(keyFile);
122  1 byte[] bytes = Files.readAllBytes(path);
123  1 PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(bytes);
124  1 KeyFactory kf = KeyFactory.getInstance(algorithm.toString());
125  1 key = kf.generatePrivate(ks);
126    } catch (Exception ex) {
127  1 LOGGER.catchException(ex);
128    }
129  2 return key;
130    }
131   
 
132  2 toggle @Override
133    public PublicKey loadPublicKeyFile(final String keyFile, final CipherAlgorithm algorithm) {
134  2 PublicKey key = null;
135  2 try {
136  2 Path path = Paths.get(keyFile);
137  1 byte[] bytes = Files.readAllBytes(path);
138  1 X509EncodedKeySpec ks = new X509EncodedKeySpec(bytes);
139  1 KeyFactory kf = KeyFactory.getInstance(algorithm.toString());
140  1 key = kf.generatePublic(ks);
141    } catch (Exception ex) {
142  1 LOGGER.catchException(ex);
143    }
144  2 return key;
145    }
146   
 
147  2 toggle @Override
148    public double generateRandomNumber(final RNGAlgorithm algortihm) {
149   
150  2 double random = .0;
151  2 try {
152  2 SecureRandom genrator
153    = SecureRandom.getInstance(algortihm.name());
154   
155  1 random = genrator.nextDouble();
156  1 LOGGER.log("Random Value: " + random, LogLevel.DEBUG);
157   
158    } catch (Exception ex) {
159  1 LOGGER.catchException(ex);
160    }
161  2 return random;
162    }
163    }