public override byte[] GetBytes (int cb)
{
#if NET_2_0
#pragma warning restore 809
#else
// 1.0/1.1 was a little late at throwing the argument exception ;-)
if (password == null)
throw new ArgumentNullException ("Password");
#endif
if (cb < 1)
throw new IndexOutOfRangeException ("cb");
if (state == 0) {
// it's now impossible to change the HashName, Salt
// and IterationCount
Reset ();
state = 1;
}
byte[] result = new byte [cb];
int cpos = 0;
// the initial hash (in reset) + at least one iteration
int iter = Math.Max (1, IterationsValue - 1);
// start with the PKCS5 key
if (output == null) {
// calculate the PKCS5 key
output = initial;
// generate new key material
for (int i = 0; i < iter - 1; i++)
output = hash.ComputeHash (output);
}
while (cpos < cb) {
byte[] output2 = null;
if (hashnumber == 0) {
// last iteration on output
output2 = hash.ComputeHash (output);
}
else if (hashnumber < 1000) {
string n = Convert.ToString (hashnumber);
output2 = new byte [output.Length + n.Length];
for (int j=0; j < n.Length; j++)
output2 [j] = (byte)(n [j]);
Buffer.BlockCopy (output, 0, output2, n.Length, output.Length);
// don't update output
output2 = hash.ComputeHash (output2);
}
else {
throw new CryptographicException (
Locale.GetText ("too long"));
}
int rem = output2.Length - position;
int l = Math.Min (cb - cpos, rem);
Buffer.BlockCopy (output2, position, result, cpos, l);
cpos += l;
position += l;
while (position >= output2.Length) {
position -= output2.Length;
hashnumber++;
}
}
return result;
}