source: trunk/CrypPlugins/Whirlpool/WhirlpoolPlugin.cs @ 2061

Last change on this file since 2061 was 445, checked in by Thomas Schmid, 12 years ago
  • added new (group)interfaces "CheckSumHash" ans "CryptographicHash" changed plugins to be placed in correct group
  • plugins with more than one interface will be placed in several groups
  • Property svn:keywords set to URL Author Date Rev Id
File size: 10.8 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////////////////////
2// CrypTool V2
3// © 2008 - Gerhard Junker
4// Apache License see http://www.apache.org/licenses/
5//
6// $HeadURL: trunk/CrypPlugins/Whirlpool/WhirlpoolPlugin.cs $
7//////////////////////////////////////////////////////////////////////////////////////////////////
8// $Revision:: 445                                                                            $://
9// $Author:: schmid                                                                           $://
10// $Date:: 2009-08-30 19:35:51 +0000 (Sun, 30 Aug 2009)                                       $://
11//////////////////////////////////////////////////////////////////////////////////////////////////
12
13// The Whirlpool algorithm was developed by
14// Paulo S. L. M. Barreto and Vincent Rijmen</a>.
15//
16// This implementation is based on the reference implementation found at
17// http://www.larc.usp.br/~pbarreto/whirlpool.zip
18// .. and moved from C to C#
19
20// Read more at
21// http://en.wikipedia.org/wiki/Whirlpool_(cryptography)
22// http://de.wikipedia.org/wiki/Whirlpool_(Algorithmus)
23// http://www.larc.usp.br/~pbarreto/WhirlpoolPage.html
24
25
26 
27using System;
28using System.Collections.Generic;
29using System.ComponentModel;
30using System.IO;
31using System.Security.Cryptography;
32using System.Text;
33
34using Cryptool.PluginBase;
35using Cryptool.PluginBase.Cryptography;
36using Cryptool.PluginBase.IO;
37using Cryptool.PluginBase.Miscellaneous;
38
39namespace Whirlpool
40{
41  [Author("Gerhard Junker", null, "private project member", null)]
42  [PluginInfo(false, "Whirlpool", "Whirlpool hash function", "", "Whirlpool/Whirlpool1.png")]
43  public class WPHash : ICryptographicHash
44  {
45
46    /// <summary>
47    /// can only handle one input canal
48    /// </summary>
49    private enum dataCanal
50    {
51      /// <summary>
52      /// nothing assigned
53      /// </summary>
54      none,
55      /// <summary>
56      /// using stream interface
57      /// </summary>
58      streamCanal,
59      /// <summary>
60      /// using byte array interface
61      /// </summary>
62      byteCanal
63    };
64
65    WhirlpoolSettings whirlpoolSetting = new WhirlpoolSettings();
66
67    /// <summary>
68    /// Initializes a new instance of the <see cref="WPHash"/> class.
69    /// </summary>
70    public WPHash()
71    {
72    }
73
74    /// <summary>
75    /// Gets or sets the settings.
76    /// </summary>
77    /// <value>The settings.</value>
78    public ISettings Settings
79    {
80      get
81      {
82        return whirlpoolSetting;
83      }
84      set
85      {
86        whirlpoolSetting = (WhirlpoolSettings)value;
87        OnPropertyChanged("Settings");
88        GuiLogMessage("Settings changed.", NotificationLevel.Debug);
89      }
90
91    }
92
93
94    #region Input data
95
96    // Input input
97    private static byte[] empty = { };
98    private byte[] input = empty;
99    //private dataCanal inputCanal = dataCanal.none;
100
101    /// <summary>
102    /// Notifies the update input.
103    /// </summary>
104    private void NotifyUpdateInput()
105    {
106      OnPropertyChanged("InputStream");
107      OnPropertyChanged("InputData");
108    }
109
110    /// <summary>
111    /// Gets or sets the input data.
112    /// </summary>
113    /// <value>The input input.</value>
114    [PropertyInfo(Direction.InputData, "Input Stream", "Input stream to be hashed", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
115    public CryptoolStream InputStream
116    {
117      get
118      {
119        CryptoolStream inputDataStream = new CryptoolStream();
120        inputDataStream.OpenRead(this.GetPluginInfoAttribute().Caption, input);
121        return inputDataStream;
122      }
123      set
124      {
125        //if (inputCanal != dataCanal.none && inputCanal != dataCanal.streamCanal)
126        //  GuiLogMessage("Duplicate input key not allowed!", NotificationLevel.Error);
127        //inputCanal = dataCanal.streamCanal;
128
129        if (null == value)
130          input = empty;
131        else
132        {
133          long len = value.Length;
134          input = new byte[len];
135
136          for (long i = 0; i < len; i++)
137            input[i] = (byte)value.ReadByte();
138        }
139        NotifyUpdateInput();
140        GuiLogMessage("InputStream changed.", NotificationLevel.Debug);
141      }
142    }
143
144    /// <summary>
145    /// Gets the input data.
146    /// </summary>
147    /// <value>The input data.</value>
148    [PropertyInfo(Direction.InputData, "Input Data", "Input stream to be hashed", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
149    public byte[] InputData
150    {
151      get
152      {
153        return input;
154      }
155      set
156      {
157        //if (inputCanal != dataCanal.none && inputCanal != dataCanal.byteCanal)
158        //  GuiLogMessage("Duplicate input data not allowed!", NotificationLevel.Error);
159        //inputCanal = dataCanal.byteCanal;
160
161        if (null == value)
162          input = empty;
163        else
164        {
165          long len = value.Length;
166          input = new byte[len];
167
168          for (long i = 0; i < len; i++)
169            input[i] = value[i];
170        }
171        NotifyUpdateInput();
172        GuiLogMessage("InputData changed.", NotificationLevel.Debug);
173      }
174    }
175    #endregion
176
177    #region Output
178
179    // Output
180    private List<CryptoolStream> listCryptoolStreamsOut = new List<CryptoolStream>();
181    private byte[] outputData = { };
182
183    /// <summary>
184    /// Notifies the update output.
185    /// </summary>
186    private void NotifyUpdateOutput()
187    {
188      OnPropertyChanged("HashOutputStream");
189      OnPropertyChanged("HashOutputData");
190    }
191
192
193    /// <summary>
194    /// Gets or sets the output data stream.
195    /// </summary>
196    /// <value>The output data stream.</value>
197    [PropertyInfo(Direction.OutputData, "Hashed Stream", "Output stream of the hashed value", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
198    public CryptoolStream HashOutputStream
199    {
200      get
201      {
202        CryptoolStream outputDataStream = null;
203        if (outputData != null)
204        {
205          outputDataStream = new CryptoolStream();
206          listCryptoolStreamsOut.Add(outputDataStream);
207          outputDataStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputData);
208        }
209        GuiLogMessage("Got HashOutputStream.", NotificationLevel.Debug);
210        return outputDataStream;
211      }
212      //set
213      //{
214      //} //readonly
215    }
216
217    /// <summary>
218    /// Gets the output data.
219    /// </summary>
220    /// <value>The output data.</value>
221    [PropertyInfo(Direction.OutputData, "Hashed Data", "Output data of the hashed value", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
222    public byte[] HashOutputData
223    {
224      get
225      {
226        GuiLogMessage("Got HashOutputData.", NotificationLevel.Debug);
227        return this.outputData;
228      }
229      //set
230      //{
231      //    if (outputData != value)
232      //    {
233      //        this.outputData = value;
234      //    }
235      //    NotifyUpdateOutput();
236      //}
237    }
238
239    /// <summary>
240    /// Hashes this instance.
241    /// </summary>
242    public void Hash()
243    {
244      HMACWHIRLPOOL wh = new HMACWHIRLPOOL();
245      wh.Initialize();
246      outputData = wh.ComputeHash(input);
247      wh = null;
248      NotifyUpdateOutput();
249    }
250    #endregion
251
252    #region IPlugin Member
253
254#pragma warning disable 67
255    public event StatusChangedEventHandler OnPluginStatusChanged;
256    public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
257    public event PluginProgressChangedEventHandler OnPluginProgressChanged;
258#pragma warning restore
259
260    /// <summary>
261    /// Provide all presentation stuff in this user control, it will be opened in an tab.
262    /// Return null if your plugin has no presentation.
263    /// </summary>
264    /// <value>The presentation.</value>
265    public System.Windows.Controls.UserControl Presentation
266    {
267      get
268      {
269        return null;
270      }
271    }
272
273    /// <summary>
274    /// Gets the quick watch presentation - will be displayed inside of the plugin presentation-element.
275    /// You can return the existing Presentation if it makes sense to display it inside a small area.
276    /// But be aware that
277    /// if Presentation is displayed in QuickWatchPresentation you can't open Presentation it in a tab
278    /// before you you close QuickWatchPresentation;
279    /// Return null if your plugin has no QuickWatchPresentation.
280    /// </summary>
281    /// <value>The quick watch presentation.</value>
282    public System.Windows.Controls.UserControl QuickWatchPresentation
283    {
284      get
285      {
286        return null;
287      }
288    }
289
290    /// <summary>
291    /// Will be called from editor after restoring settings and before adding to workspace.
292    /// </summary>
293    public void Initialize()
294    {
295    }
296
297    /// <summary>
298    /// Will be called from editor before right before chain-run starts
299    /// </summary>
300    public void PreExecution()
301    {
302    }
303
304    /// <summary>
305    /// Will be called from editor while chain-run is active and after last necessary input
306    /// for plugin has been set.
307    /// </summary>
308    public void Execute()
309    {
310      Hash();
311    }
312
313    /// <summary>
314    /// Will be called from editor after last plugin in chain has finished its work.
315    /// </summary>
316    public void PostExecution()
317    {
318    }
319
320    /// <summary>
321    /// Not defined yet.
322    /// </summary>
323    public void Pause()
324    {
325    }
326
327    /// <summary>
328    /// Will be called from editor while chain-run is active. Plugin hast to stop work immediately.
329    /// </summary>
330    public void Stop()
331    {
332    }
333
334    /// <summary>
335    /// Will be called from editor when element is deleted from worksapce.
336    /// Releases unmanaged and - optionally - managed resources
337    /// </summary>
338    public void Dispose()
339    {
340      foreach (CryptoolStream stream in listCryptoolStreamsOut)
341      {
342        stream.Close();
343      }
344      listCryptoolStreamsOut.Clear();
345    }
346
347    #endregion
348
349    #region INotifyPropertyChanged Member
350
351    /// <summary>
352    /// Occurs when a property value changes.
353    /// </summary>
354    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
355
356    /// <summary>
357    /// Called when [property changed].
358    /// </summary>
359    /// <param name="name">The name.</param>
360    protected void OnPropertyChanged(string name)
361    {
362      EventsHelper.PropertyChanged(PropertyChanged, this, new PropertyChangedEventArgs(name));
363    }
364
365    /// <summary>
366    /// GUIs the log message.
367    /// </summary>
368    /// <param name="message">The message.</param>
369    /// <param name="logLevel">The log level.</param>
370    private void GuiLogMessage(string message, NotificationLevel logLevel)
371    {
372      EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(message, this, logLevel));
373    }
374
375    #endregion
376  }
377}
Note: See TracBrowser for help on using the repository browser.