/[classpath]/classpath/gnu/java/security/provider/SHA1PRNG.java
ViewVC logotype

Diff of /classpath/gnu/java/security/provider/SHA1PRNG.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6.2.1 by gnu_andrew, Sun Mar 13 14:38:32 2005 UTC revision 1.6.2.2 by gnu_andrew, Fri May 27 00:01:44 2005 UTC
# Line 1  Line 1 
1  /* SHA1PRNG.java --- Secure Random SPI SHA1PRNG  /* SHA1PRNG.java --- Secure Random SPI SHA1PRNG
2     Copyright (C) 1999, 2001, 2003 Free Software Foundation, Inc.     Copyright (C) 1999, 2001, 2003, 2005 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 52  public class SHA1PRNG extends SecureRand Line 52  public class SHA1PRNG extends SecureRand
52    int seedpos;    int seedpos;
53    int datapos;    int datapos;
54    private boolean seeded = false; // set to true when we seed this    private boolean seeded = false; // set to true when we seed this
55      /**
56       * The size of seed.
57       */
58      private static final int SEED_SIZE = 20;
59      /**
60       * The size of data.
61       */
62      private static final int DATA_SIZE = 40;
63    
64      /**
65       * Create a new SHA-1 pseudo-random number generator.
66       */
67    public SHA1PRNG()    public SHA1PRNG()
68    {    {
69      try {      try {
# Line 63  public class SHA1PRNG extends SecureRand Line 74  public class SHA1PRNG extends SecureRand
74        throw new InternalError ("no SHA implementation found");        throw new InternalError ("no SHA implementation found");
75      }      }
76    
77      seed = new byte[20];      seed = new byte[SEED_SIZE];
78      seedpos = 0;      seedpos = 0;
79      data = new byte[40];      data = new byte[DATA_SIZE];
80      datapos = 20;  // try to force hashing a first block      datapos = SEED_SIZE;  // try to force hashing a first block
81    }    }
82    
83    public void engineSetSeed(byte[] seed)    public void engineSetSeed(byte[] seed)
84    {    {
85      for(int i = 0; i < seed.length; i++)      for(int i = 0; i < seed.length; i++)
86        this.seed[seedpos++ % 20] ^= seed[i];        this.seed[seedpos++ % SEED_SIZE] ^= seed[i];
87      seedpos %= 20;      seedpos %= SEED_SIZE;
88    
89    }    }
90    
# Line 83  public class SHA1PRNG extends SecureRand Line 94  public class SHA1PRNG extends SecureRand
94      int loc = 0;      int loc = 0;
95      while (loc < bytes.length)      while (loc < bytes.length)
96        {        {
97          int copy = Math.min (bytes.length - loc, 20 - datapos);          int copy = Math.min (bytes.length - loc, SEED_SIZE - datapos);
98    
99          if (copy > 0)          if (copy > 0)
100            {            {
# Line 94  public class SHA1PRNG extends SecureRand Line 105  public class SHA1PRNG extends SecureRand
105          else          else
106            {            {
107              // No data ready for copying, so refill our buffer.              // No data ready for copying, so refill our buffer.
108              System.arraycopy( seed, 0, data, 20, 20);              System.arraycopy( seed, 0, data, SEED_SIZE, SEED_SIZE);
109              byte[] digestdata = digest.digest( data );              byte[] digestdata = digest.digest( data );
110              System.arraycopy( digestdata, 0, data, 0, 20);              System.arraycopy( digestdata, 0, data, 0, SEED_SIZE);
111              datapos = 0;              datapos = 0;
112            }            }
113        }        }
# Line 117  public class SHA1PRNG extends SecureRand Line 128  public class SHA1PRNG extends SecureRand
128          new Random(0L).nextBytes(seed);          new Random(0L).nextBytes(seed);
129    
130          byte[] digestdata = digest.digest(data);          byte[] digestdata = digest.digest(data);
131          System.arraycopy(digestdata, 0, data, 0, 20);          System.arraycopy(digestdata, 0, data, 0, SEED_SIZE);
132    
133          seeded = true;          seeded = true;
134        }        }

Legend:
Removed from v.1.6.2.1  
changed lines
  Added in v.1.6.2.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26