1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.accumulo.core.security.crypto;
18
19 import java.security.NoSuchAlgorithmException;
20 import java.security.NoSuchProviderException;
21 import java.security.SecureRandom;
22
23 import javax.crypto.Cipher;
24 import javax.crypto.NoSuchPaddingException;
25 import javax.crypto.NullCipher;
26
27 import org.apache.log4j.Logger;
28
29 public class DefaultCryptoModuleUtils {
30
31 private static final Logger log = Logger.getLogger(DefaultCryptoModuleUtils.class);
32
33 public static SecureRandom getSecureRandom(String secureRNG, String secureRNGProvider) {
34 SecureRandom secureRandom = null;
35 try {
36 secureRandom = SecureRandom.getInstance(secureRNG, secureRNGProvider);
37
38
39 byte[] throwAway = new byte[16];
40 secureRandom.nextBytes(throwAway);
41
42 } catch (NoSuchAlgorithmException e) {
43 log.error(String.format("Accumulo configuration file specified a secure random generator \"%s\" that was not found by any provider.", secureRNG));
44 throw new RuntimeException(e);
45 } catch (NoSuchProviderException e) {
46 log.error(String.format("Accumulo configuration file specified a secure random provider \"%s\" that does not exist", secureRNGProvider));
47 throw new RuntimeException(e);
48 }
49 return secureRandom;
50 }
51
52 public static Cipher getCipher(String cipherSuite) {
53 Cipher cipher = null;
54
55 if (cipherSuite.equals("NullCipher")) {
56 cipher = new NullCipher();
57 } else {
58 try {
59 cipher = Cipher.getInstance(cipherSuite);
60 } catch (NoSuchAlgorithmException e) {
61 log.error(String.format("Accumulo configuration file contained a cipher suite \"%s\" that was not recognized by any providers", cipherSuite));
62 throw new RuntimeException(e);
63 } catch (NoSuchPaddingException e) {
64 log.error(String.format("Accumulo configuration file contained a cipher, \"%s\" with a padding that was not recognized by any providers"));
65 throw new RuntimeException(e);
66 }
67 }
68 return cipher;
69 }
70
71
72 }