View Javadoc
1   /*
2   * Licensed to the Apache Software Foundation (ASF) under one
3   * or more contributor license agreements.  See the NOTICE file
4   * distributed with this work for additional information
5   * regarding copyright ownership.  The ASF licenses this file
6   * to you under the Apache License, Version 2.0 (the
7   * "License"); you may not use this file except in compliance
8   * with the License.  You may obtain a copy of the License at
9   *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18  package org.apache.commons.crypto.random;
19  
20  import java.security.NoSuchAlgorithmException;
21  import java.security.SecureRandom;
22  import java.util.Properties;
23  
24  import org.apache.commons.crypto.utils.Utils;
25  
26  /**
27   * A CryptoRandom of Java implementation.
28   * <p>
29   * This class is not public/protected so does not appear in the main Javadoc Please ensure that property use is documented in the enum
30   * CryptoRandomFactory.RandomProvider
31   * </p>
32   */
33  final class JavaCryptoRandom implements CryptoRandom {
34  
35      private final SecureRandom instance;
36  
37      /**
38       * Constructs a {@link JavaCryptoRandom}.
39       *
40       * @param properties the configuration properties. Uses the key {@link CryptoRandomFactory#JAVA_ALGORITHM_KEY} to get the name of the algorithm, with a
41       *        default of {@link CryptoRandomFactory#JAVA_ALGORITHM_DEFAULT}
42       */
43      public JavaCryptoRandom(final Properties properties) {
44          SecureRandom tmp;
45          try {
46              tmp = SecureRandom.getInstance(properties.getProperty(CryptoRandomFactory.JAVA_ALGORITHM_KEY, CryptoRandomFactory.JAVA_ALGORITHM_DEFAULT));
47          } catch (final NoSuchAlgorithmException e) {
48              tmp = new SecureRandom();
49          }
50          instance = tmp;
51      }
52  
53      /**
54       * Overrides {@link java.lang.AutoCloseable#close()}. For {@link JavaCryptoRandom}, we don't need to recycle resource.
55       */
56      @Override
57      public void close() {
58          // do nothing
59      }
60  
61      /**
62       * Overrides Random#next(). Generates an integer containing the user-specified number of random bits(right justified, with leading zeros).
63       *
64       * @param numBits number of random bits to be generated, where 0 {@literal <=} {@code numBits} {@literal <=} 32.
65       * @return int an {@code int} containing the user-specified number of random bits (right justified, with leading zeros).
66       */
67      protected int next(final int numBits) {
68          Utils.checkArgument(numBits >= 0 && numBits <= 32);
69          // Can't simply invoke instance.next(bits) here, because that is package protected.
70          // But, this should do.
71          return instance.nextInt() >>> (Integer.SIZE - numBits);
72      }
73  
74      /**
75       * Overrides {@link CryptoRandom#nextBytes(byte[])}. Generates random bytes and places them into a user-supplied byte array. The number of random bytes
76       * produced is equal to the length of the byte array.
77       *
78       * @param bytes the array to be filled in with random bytes.
79       */
80      @Override
81      public void nextBytes(final byte[] bytes) {
82          instance.nextBytes(bytes);
83      }
84  }