Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.exoplatform.web.security.security;
 
 
Subclass of java.security.SecureRandom which is able to reseed itself every reseedingPeriod milliseconds. All methods inherited from java.security.SecureRandom are passed to delegate. When reseeding the delegate is recreated. The reseeding happens in a new java.lang.Thread not to block the java.lang.Thread in which any of the next* methods is called.

Author(s):
Peter Palaga
 
 public class AutoReseedRandom extends SecureRandom implements Runnable {
 
     private static final long serialVersionUID = 8754191896768407628L;

    
Default expiration time in milliseconds, an equivalent of 24 hours.
 
     public static final long DEFAULT_RESEEDING_PERIOD = 1000 * 60 * 60 * 24;

    
Default random algorithm DEFAULT_RANDOM_ALGORITHM.
 
     public static final String DEFAULT_RANDOM_ALGORITHM = "SHA1PRNG";

    
Default random algorithm provider DEFAULT_RANDOM_ALGORITHM_PROVIDER.
 
     public static final String DEFAULT_RANDOM_ALGORITHM_PROVIDER = null;

    
Default seed length DEFAULT_SEED_LENGTH.
 
     public static final int DEFAULT_SEED_LENGTH = 32;

    
Name of the reseeding thread RESEEDING_THREAD_NAME.
 
     private static final String RESEEDING_THREAD_NAME = AutoReseedRandom.class.getSimpleName() + " reseeding";

    
Marked as volatile to avoid instruction reordering on initialization.
 
     private volatile SecureRandom delegate;
 
     private volatile long nextReseed = 0;

    
Time in milliseconds after which the delegate gets reseeded by resetRandom().
 
     private final long reseedingPeriod;
 
     private final Logger log = LoggerFactory.getLogger(AutoReseedRandom.class);

    
 
     private final String algorithm;

    
See java.security.SecureRandom.getInstance(java.lang.String,java.lang.String). DEFAULT_RANDOM_ALGORITHM_PROVIDER ( DEFAULT_RANDOM_ALGORITHM_PROVIDER) is used in the default constructor. You may want to consider using "NativePRNG" on some platforms if you require stronger cryptography.
 
     private final String algorithmProvider;

    
 
     private final int seedLength;
    public AutoReseedRandom() {
    }

    

Parameters:
algorithm See java.security.SecureRandom.getInstance(java.lang.String,java.lang.String).
algorithmProvider See java.security.SecureRandom.getInstance(java.lang.String,java.lang.String). In most cases you will want to use DEFAULT_RANDOM_ALGORITHM_PROVIDER ( DEFAULT_RANDOM_ALGORITHM_PROVIDER) which will lead to using java.security.SecureRandom.getInstance(java.lang.String) instead of java.security.SecureRandom.getInstance(java.lang.String,java.lang.String). You may want to consider using "NativePRNG", "SUN" or other providers if you have special requirements.
seedLength number of Bytes, see java.security.SecureRandom.generateSeed(int).
reseedingPeriod Time in milliseconds after which the delegate gets reseeded by resetRandom()
    public AutoReseedRandom(String algorithmString algorithmProviderint seedLengthlong reseedingPeriod) {
        super();
        this. = algorithm;
        this. = algorithmProvider;
        this. = seedLength;
        this. = reseedingPeriod;
         = System.currentTimeMillis() + reseedingPeriod;
        resetRandom();
    }

    
Forks the reseeding java.lang.Thread if necessary.
    private void checkReseed() {
        boolean reseed = false;
        synchronized (this) {
            /* only one thread can read or write nextReseed */
            if (System.currentTimeMillis() > ) {
                /*
                 * we move the nextReseed further to the future already here if we did it in the forked thread we could start
                 * several concurrent forks.
                 */
                 = System.currentTimeMillis() + ;
                reseed = true;
            }
        }
        if (reseed) {
            new Thread(this).start();
        }
    }

    
Called from the reseeding java.lang.Thread and on initialization.
    private void resetRandom() {
        SecureRandom newRandom = null;
        try {
            if ( == null) {
                newRandom = SecureRandom.getInstance();
            } else {
                newRandom = SecureRandom.getInstance();
            }
            /* ensure the SecureRandom gets seeded */
            long before = .isDebugEnabled() ? 0 : System.currentTimeMillis();
            newRandom.setSeed(newRandom.generateSeed());
            if (.isDebugEnabled()) {
                long now = System.currentTimeMillis();
                .debug("secureRandom.setSeed took " + ((now - before) / 1000.0) +" seconds.");
            }
             = newRandom;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            .info("Falling back to default random algorithm provider because '" +  + "' is not available."e);
            /* use the default provider */
            try {
                newRandom = SecureRandom.getInstance();
                /* ensure the SecureRandom gets seeded */
                newRandom.setSeed(newRandom.generateSeed());
                 = newRandom;
            } catch (NoSuchAlgorithmException e1) {
                throw new RuntimeException(e);
            }
        }
    }

    
Called from the reseeding java.lang.Thread.

    @Override
    public void run() {
        try {
            resetRandom();
        } catch (Exception e) {
            .error(e.getMessage(), e);
        }
    }
    /*
     * (non-Javadoc)
     *
     * @see java.security.SecureRandom#getAlgorithm()
     */
    @Override
    public String getAlgorithm() {
        return .getAlgorithm();
    }
    /*
     * (non-Javadoc)
     *
     * @see java.security.SecureRandom#setSeed(byte[])
     */
    @Override
    public synchronized void setSeed(byte[] seed) {
        .setSeed(seed);
    }
    /*
     * (non-Javadoc)
     *
     * @see java.security.SecureRandom#setSeed(long)
     */
    @Override
    public void setSeed(long seed) {
        if ( != null) {
            .setSeed(seed);
        }
    }
    /*
     * (non-Javadoc)
     *
     * @see java.security.SecureRandom#nextBytes(byte[])
     */
    @Override
    public synchronized void nextBytes(byte[] bytes) {
        .nextBytes(bytes);
        checkReseed();
    }
    /*
     * (non-Javadoc)
     *
     * @see java.security.SecureRandom#generateSeed(int)
     */
    @Override
    public byte[] generateSeed(int numBytes) {
        return .generateSeed(numBytes);
    }
    /*
     * (non-Javadoc)
     *
     * @see java.util.Random#nextInt()
     */
    @Override
    public int nextInt() {
        int result = .nextInt();
        checkReseed();
        return result;
    }
    /*
     * (non-Javadoc)
     *
     * @see java.util.Random#nextInt(int)
     */
    @Override
    public int nextInt(int n) {
        int result = .nextInt(n);
        checkReseed();
        return result;
    }
    /*
     * (non-Javadoc)
     *
     * @see java.util.Random#nextLong()
     */
    @Override
    public long nextLong() {
        long result = .nextLong();
        checkReseed();
        return result;
    }
    /*
     * (non-Javadoc)
     *
     * @see java.util.Random#nextBoolean()
     */
    @Override
    public boolean nextBoolean() {
        boolean result = .nextBoolean();
        checkReseed();
        return result;
    }
    /*
     * (non-Javadoc)
     *
     * @see java.util.Random#nextFloat()
     */
    @Override
    public float nextFloat() {
        float result = .nextFloat();
        checkReseed();
        return result;
    }
    /*
     * (non-Javadoc)
     *
     * @see java.util.Random#nextDouble()
     */
    @Override
    public double nextDouble() {
        double result = .nextDouble();
        checkReseed();
        return result;
    }
    /*
     * (non-Javadoc)
     *
     * @see java.util.Random#nextGaussian()
     */
    @Override
    public synchronized double nextGaussian() {
        double result = .nextGaussian();
        checkReseed();
        return result;
    }
New to GrepCode? Check out our FAQ X