summary refs log tree commit diff
path: root/crypto/test/src/util/test/FixedSecureRandom.cs
blob: 15a2e9bb3234d93630f8eb85e0568d051116257c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.IO;

using Org.BouncyCastle.Security;

namespace Org.BouncyCastle.Utilities.Test
{
	public class FixedSecureRandom
		: SecureRandom
	{
		private byte[]       _data;
		private int          _index;

		protected FixedSecureRandom(
			byte[] data)
		{
			_data = data;
		}

		public static FixedSecureRandom From(
			params byte[][] values)
		{
			MemoryStream bOut = new MemoryStream();

			for (int i = 0; i != values.Length; i++)
			{
				try
				{
					byte[] v = values[i];
					bOut.Write(v, 0, v.Length);
				}
				catch (IOException)
				{
					throw new ArgumentException("can't save value array.");
				}
			}

			return new FixedSecureRandom(bOut.ToArray());
		}

		public override void NextBytes(
			byte[] buf)
		{
			Array.Copy(_data, _index, buf, 0, buf.Length);

			_index += buf.Length;
		}

		public override void NextBytes(
			byte[]	buf,
			int		off,
			int		len)
		{
			Array.Copy(_data, _index, buf, off, len);

			_index += len;
		}

		public bool IsExhausted
		{
			get { return _index == _data.Length; }
		}
	}
}