Source for gnu.java.security.util.PRNG

   1: /* PRNG.java -- A Utility methods for default source of randomness
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.security.util;
  40: 
  41: import java.util.HashMap;
  42: 
  43: import gnu.java.security.prng.IRandom;
  44: import gnu.java.security.prng.LimitReachedException;
  45: import gnu.java.security.prng.MDGenerator;
  46: 
  47: /**
  48:  * A useful hash-based (SHA) pseudo-random number generator used throughout this
  49:  * library.
  50:  *
  51:  * @see MDGenerator
  52:  */
  53: public class PRNG
  54: {
  55:   /** The underlying {@link IRandom}. */
  56:   private IRandom delegate;
  57: 
  58:   /**
  59:    * Private constructor to enforce using the Factory method.
  60:    *
  61:    * @param delegate the undelying {@link IRandom} object used.
  62:    */
  63:   private PRNG(IRandom delegate)
  64:   {
  65:     super();
  66: 
  67:     this.delegate = delegate;
  68:   }
  69: 
  70:   public static final PRNG getInstance()
  71:   {
  72:     IRandom delegate = new MDGenerator();
  73:     try
  74:       {
  75:         HashMap map = new HashMap();
  76:         // initialise it with a seed
  77:         long t = System.currentTimeMillis();
  78:         byte[] seed = new byte[] {
  79:             (byte)(t >>> 56), (byte)(t >>> 48),
  80:             (byte)(t >>> 40), (byte)(t >>> 32),
  81:             (byte)(t >>> 24), (byte)(t >>> 16),
  82:             (byte)(t >>>  8), (byte) t };
  83:         map.put(MDGenerator.SEEED, seed);
  84:         delegate.init(map); // default is to use SHA-1 hash
  85:       }
  86:     catch (Exception x)
  87:       {
  88:         throw new ExceptionInInitializerError(x);
  89:       }
  90:     return new PRNG(delegate);
  91:   }
  92: 
  93:   /**
  94:    * Completely fills the designated <code>buffer</code> with random data
  95:    * generated by the underlying delegate.
  96:    *
  97:    * @param buffer the place holder of random bytes generated by the underlying
  98:    *          delegate. On output, the contents of <code>buffer</code> are
  99:    *          replaced with pseudo-random data, iff the <code>buffer</code>
 100:    *          size is not zero.
 101:    */
 102:   public void nextBytes(byte[] buffer)
 103:   {
 104:     nextBytes(buffer, 0, buffer.length);
 105:   }
 106: 
 107:   /**
 108:    * Fills the designated <code>buffer</code>, starting from byte at position
 109:    * <code>offset</code> with, at most, <code>length</code> bytes of random
 110:    * data generated by the underlying delegate.
 111:    *
 112:    * @see IRandom#nextBytes
 113:    */
 114:   public void nextBytes(byte[] buffer, int offset, int length)
 115:   {
 116:     try
 117:       {
 118:         delegate.nextBytes(buffer, offset, length);
 119:       }
 120:     catch (LimitReachedException x) // re-initialise with a seed
 121:       {
 122:         try
 123:           {
 124:             HashMap map = new HashMap();
 125:             long t = System.currentTimeMillis();
 126:             byte[] seed = new byte[] {
 127:                 (byte)(t >>> 56), (byte)(t >>> 48),
 128:                 (byte)(t >>> 40), (byte)(t >>> 32),
 129:                 (byte)(t >>> 24), (byte)(t >>> 16),
 130:                 (byte)(t >>>  8), (byte) t };
 131:             map.put(MDGenerator.SEEED, seed);
 132:             delegate.init(map); // default is to use SHA-1 hash
 133:             delegate.nextBytes(buffer, offset, length);
 134:           }
 135:         catch (Exception y)
 136:           {
 137:             throw new ExceptionInInitializerError(y);
 138:           }
 139:       }
 140:   }
 141: }