JavaCryptoRandom.java

  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. import java.security.NoSuchAlgorithmException;
  20. import java.security.SecureRandom;
  21. import java.util.Properties;

  22. import org.apache.commons.crypto.utils.Utils;

  23. /**
  24.  * A CryptoRandom of Java implementation.
  25.  * <p>
  26.  * This class is not public/protected so does not appear in the main Javadoc Please ensure that property use is documented in the enum
  27.  * CryptoRandomFactory.RandomProvider
  28.  * </p>
  29.  */
  30. final class JavaCryptoRandom implements CryptoRandom {

  31.     private final SecureRandom instance;

  32.     /**
  33.      * Constructs a {@link JavaCryptoRandom}.
  34.      *
  35.      * @param properties the configuration properties. Uses the key {@link CryptoRandomFactory#JAVA_ALGORITHM_KEY} to get the name of the algorithm, with a
  36.      *        default of {@link CryptoRandomFactory#JAVA_ALGORITHM_DEFAULT}
  37.      */
  38.     public JavaCryptoRandom(final Properties properties) {
  39.         SecureRandom tmp;
  40.         try {
  41.             tmp = SecureRandom.getInstance(properties.getProperty(CryptoRandomFactory.JAVA_ALGORITHM_KEY, CryptoRandomFactory.JAVA_ALGORITHM_DEFAULT));
  42.         } catch (final NoSuchAlgorithmException e) {
  43.             tmp = new SecureRandom();
  44.         }
  45.         instance = tmp;
  46.     }

  47.     /**
  48.      * Overrides {@link java.lang.AutoCloseable#close()}. For {@link JavaCryptoRandom}, we don't need to recycle resource.
  49.      */
  50.     @Override
  51.     public void close() {
  52.         // do nothing
  53.     }

  54.     /**
  55.      * Overrides Random#next(). Generates an integer containing the user-specified number of random bits(right justified, with leading zeros).
  56.      *
  57.      * @param numBits number of random bits to be generated, where 0 {@literal <=} {@code numBits} {@literal <=} 32.
  58.      * @return int an {@code int} containing the user-specified number of random bits (right justified, with leading zeros).
  59.      */
  60.     protected int next(final int numBits) {
  61.         Utils.checkArgument(numBits >= 0 && numBits <= 32);
  62.         // Can't simply invoke instance.next(bits) here, because that is package protected.
  63.         // But, this should do.
  64.         return instance.nextInt() >>> (Integer.SIZE - numBits);
  65.     }

  66.     /**
  67.      * Overrides {@link CryptoRandom#nextBytes(byte[])}. Generates random bytes and places them into a user-supplied byte array. The number of random bytes
  68.      * produced is equal to the length of the byte array.
  69.      *
  70.      * @param bytes the array to be filled in with random bytes.
  71.      */
  72.     @Override
  73.     public void nextBytes(final byte[] bytes) {
  74.         instance.nextBytes(bytes);
  75.     }
  76. }