source: trunk/CrypPlugins/RandomInput/RandomInput.cs @ 2061

Last change on this file since 2061 was 1259, checked in by Matthäus Wander, 12 years ago

shortened license text to boilerplate notice in all source files

File size: 6.3 KB
Line 
1/*
2   Copyright 2008 Timm Korte, University of Siegen
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17// Cryptool RandomInput Plugin
18// Author: Timm Korte, cryptool@easycrypt.de
19
20using System;
21using System.Collections.Generic;
22using System.Linq;
23using System.Text;
24using Cryptool.PluginBase;
25using System.Windows.Controls;
26using System.Windows;
27using System.Windows.Data;
28using System.Security.Cryptography;
29using System.IO;
30using System.ComponentModel;
31using Cryptool.PluginBase.IO;
32using System.Runtime.CompilerServices;
33using System.Runtime.Remoting.Contexts;
34using Cryptool.PluginBase.Miscellaneous;
35using Cryptool.PluginBase.Generator;
36
37namespace RandomInput
38{
39  [Author("Timm Korte", "cryptool@easycrypt.de", "Uni Bochum", "http://www.ruhr-uni-bochum.de")]
40  [PluginInfo(true, "Random Generator", "Simple random input generator.", "", "RandomInput/icon.png")]
41  public class RandomInput : ContextBoundObject, IRandomNumberGenerator
42  {
43    private int maxByteArraySize = 10496000;
44    private byte[] rndArray;
45    private RandomInputSettings settings;
46    private RNGCryptoServiceProvider sRandom = new RNGCryptoServiceProvider();
47    private List<CryptoolStream> listCryptoolStreamsOut = new List<CryptoolStream>();
48
49    public ISettings Settings
50    {
51      get { return settings; }
52      set { settings = (RandomInputSettings)value; }
53    }
54
55    public RandomInput()
56    {
57      this.settings = new RandomInputSettings();
58    }
59
60    private void getRand()
61    {
62      try
63      {
64        if (settings.KeepRND == 0 || rndArray == null || (UInt64)rndArray.Length != settings.NumBytes)
65        {
66          Progress(50, 100);
67          rndArray = new byte[Math.Min((UInt64)settings.NumBytes, (UInt64)maxByteArraySize)];
68          sRandom.GetBytes(rndArray);
69
70          GuiMessage("Generated " + rndArray.Length + " bytes of random data.", NotificationLevel.Info);
71          Progress(100, 100);
72        }
73
74        // The PropertyChanged event is send always, because new chain run needs to
75        // get the output events from input plugins every time the execute method is
76        // executed.
77        OnPropertyChanged("ByteArrayOutput");
78        OnPropertyChanged("StreamOutput");
79      }
80      catch
81      {
82        GuiMessage("Can't create random data. Incorrect length value set in the Counter field.", NotificationLevel.Error);
83      }
84    }
85
86    [PropertyInfo(Direction.OutputData, "Stream Output", "Memory stream of given length.", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
87    public CryptoolStream StreamOutput
88    {
89      get
90      {
91        Progress(0.5, 1.0);
92        // getRand(); creates a loop if OnPropertyChanged is used inside the property. is now called in execute method
93        if (rndArray == null) getRand();
94        if (rndArray != null)
95        {
96          GuiMessage("Send " + rndArray.Length + " bytes of random data as stream.", NotificationLevel.Debug);
97          CryptoolStream cryptoolStream = new CryptoolStream();
98          cryptoolStream.OpenRead(this.GetPluginInfoAttribute().Caption, rndArray);
99          listCryptoolStreamsOut.Add(cryptoolStream);         
100          Progress(1.0, 1.0);
101         
102          return cryptoolStream;
103        }
104        GuiMessage("No stream generated. Returning null.", NotificationLevel.Error);
105        Progress(1.0, 1.0);
106        return null;
107      }
108      set { } // readonly
109    }
110
111    [PropertyInfo(Direction.OutputData, "ByteArray Output", "Byte array of given length.", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
112    public byte[] ByteArrayOutput
113    {
114      get
115      {
116        Progress(0.5, 1.0);
117        // getRand();
118        if (rndArray != null)
119          GuiMessage("Send " + rndArray.Length + " bytes of random data as byte array.", NotificationLevel.Debug);
120        else
121          GuiMessage("No bytes generated. Returning null.", NotificationLevel.Error);
122
123        Progress(1.0, 1.0);       
124        return rndArray;
125      }
126      set { } // readonly
127    }
128
129    #region IInput Member
130
131    #endregion
132
133    #region IPlugin Member
134
135    public UserControl PresentationControl { get; private set; }
136
137    public void Initialize()
138    {
139    }
140
141    public void Dispose()
142    {
143      foreach (CryptoolStream cryptoolStream in listCryptoolStreamsOut)
144      {
145        cryptoolStream.Close();
146      }
147      listCryptoolStreamsOut.Clear();
148    }
149
150    public void Stop()
151    {
152    }
153
154    public void PreExecution()
155    {
156      if (!(settings.KeepRND == 1)) rndArray = null;
157      Dispose();
158    }
159
160    public void PostExecution()
161    {
162      Dispose();
163    }
164
165    public event StatusChangedEventHandler OnPluginStatusChanged;
166
167    public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
168
169    public event PluginProgressChangedEventHandler OnPluginProgressChanged;
170
171    public UserControl Presentation
172    {
173      get { return null; }
174    }
175
176    public UserControl QuickWatchPresentation
177    {
178      get { return null; }
179    }
180
181    public void Execute()
182    {
183      getRand();
184    }
185
186    public void Pause()
187    {
188
189    }
190
191    #endregion
192
193
194    #region INotifyPropertyChanged Members
195
196    public event PropertyChangedEventHandler PropertyChanged;
197
198    public void OnPropertyChanged(string name)
199    {
200      EventsHelper.PropertyChanged(PropertyChanged, this, new PropertyChangedEventArgs(name));
201    }
202
203    #endregion
204
205    private void GuiMessage(string message, NotificationLevel level)
206    {
207      EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(message, this, level));
208    }
209
210    private void Progress(double value, double max)
211    {
212      EventsHelper.ProgressChanged(OnPluginProgressChanged, this, new PluginProgressEventArgs(value, max));
213    }
214  }
215}
Note: See TracBrowser for help on using the repository browser.