View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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        // Immediately seed the generator
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  }