source: trunk/CrypPlugins/Tiger/Tiger.cs @ 198

Last change on this file since 198 was 198, checked in by Gerhard Junker, 13 years ago

Implementing Tiger and Whirlpool as HMAC (was HashAlgorithm)
Added Tiger and Whirlpool to PKCS#5

  • Property svn:keywords set to URL Author Date Rev Id
File size: 10.6 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////////////////////
2// CrypTool V2
3// © 2008 - Gerhard Junker
4// Apache License see http://www.apache.org/licenses/
5//
6// $HeadURL: trunk/CrypPlugins/Tiger/Tiger.cs $
7//////////////////////////////////////////////////////////////////////////////////////////////////
8// $Revision:: 198                                                                            $://
9// $Author:: junker                                                                           $://
10// $Date:: 2009-01-16 10:18:00 +0000 (Fri, 16 Jan 2009)                                       $://
11//////////////////////////////////////////////////////////////////////////////////////////////////
12
13
14// read more about Tiger
15//
16// http://en.wikipedia.org/wiki/Tiger_(cryptography)
17// http://de.wikipedia.org/wiki/Tiger_(Hashfunktion)
18// http://www.cs.technion.ac.il/~biham/Reports/Tiger/
19//
20// based first on an VisualBasic implementation of Markus Hahn - Thanks.
21// from http://www.hotpixel.net/software.html
22// and changed to fit more the published algorithm
23
24using System;
25using System.Collections.Generic;
26using System.ComponentModel;
27using System.IO;
28using System.Security.Cryptography;
29using System.Text;
30
31using Cryptool.PluginBase;
32using Cryptool.PluginBase.Cryptography;
33using Cryptool.PluginBase.IO;
34using Cryptool.PluginBase.Miscellaneous;
35
36
37namespace Tiger
38{
39
40  [Author("Gerhard Junker", null, "private project member", null)]
41  [PluginInfo(false, "Tiger", "Tiger2 V1.0.2 Hash",
42    "http://en.wikipedia.org/wiki/Tiger_(cryptography)", "Tiger/Tiger1.png")]
43  public class Tiger : IHash
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
66    /// <summary>
67    /// Initializes A new instance of the <see cref="Tiger"/> class.
68    /// </summary>
69    public Tiger()
70    {
71      this.settings = new TigerSettings();
72    }
73
74    #region Settings
75    TigerSettings settings;
76
77    /// <summary>
78    /// Gets or sets the settings.
79    /// </summary>
80    /// <value>The settings.</value>
81    public ISettings Settings
82    {
83      get
84      {
85        return settings;
86      }
87      set
88      {
89        settings = (TigerSettings)value;
90        OnPropertyChanged("Settings");
91        GuiLogMessage("Settings changed.", NotificationLevel.Debug);
92      }
93    }
94
95    /// <summary>
96    /// Gets or sets A value indicating whether this instance has changes.
97    /// </summary>
98    /// <value>
99    ///         <c>true</c> if this instance has changes; otherwise, <c>false</c>.
100    /// </value>
101    public bool HasChanges
102    {
103      get
104      {
105        return settings.HasChanges;
106      }
107
108      set
109      {
110        settings.HasChanges = value;
111        GuiLogMessage("HasChanges changed.", NotificationLevel.Debug);
112      }
113    }
114
115    #endregion
116
117    #region Input inputdata / password
118
119    // Input inputdata
120    private byte[] inputdata = { };
121    //private dataCanal inputCanal = dataCanal.none;
122
123    /// <summary>
124    /// Notifies the update input.
125    /// </summary>
126    private void NotifyUpdateInput()
127    {
128      OnPropertyChanged("InputStream");
129      OnPropertyChanged("InputData");
130    }
131
132    /// <summary>
133    /// Gets or sets the input inputdata.
134    /// </summary>
135    /// <value>The input inputdata.</value>
136    [PropertyInfo(Direction.Input, "Input Data Stream", "Input data stream to be hashed", "",
137      false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
138    public CryptoolStream InputStream
139    {
140      get
141      {
142        CryptoolStream inputStream = new CryptoolStream();
143        inputStream.OpenRead(this.GetPluginInfoAttribute().Caption, inputdata);
144        return inputStream;
145      }
146      set
147      {
148        if (null == value)
149        {
150          inputdata = new byte[0];
151          return;
152        }
153        //if (inputCanal != dataCanal.none && inputCanal != dataCanal.streamCanal)
154        //  GuiLogMessage("Duplicate input data not allowed!", NotificationLevel.Error);
155        //inputCanal = dataCanal.streamCanal;
156
157        long len = value.Length;
158        inputdata = new byte[len];
159
160        for (long i = 0; i < len; i++)
161          inputdata[i] = (byte)value.ReadByte();
162
163        NotifyUpdateInput();
164        GuiLogMessage("InputStream changed.", NotificationLevel.Debug);
165      }
166    }
167
168    /// <summary>
169    /// Gets the input data.
170    /// </summary>
171    /// <value>The input data.</value>
172    [PropertyInfo(Direction.Input, "Input Data", "Input Data to be hashed", "",
173      false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
174    public byte[] InputData
175    {
176      get
177      {
178        return inputdata;
179      }
180      set
181      {
182        //if (inputCanal != dataCanal.none && inputCanal != dataCanal.byteCanal)
183        //  GuiLogMessage("Duplicate Data data not allowed!", NotificationLevel.Error);
184        //inputCanal = dataCanal.byteCanal;
185        if (null == value)
186        {
187          inputdata = new byte[0];
188          return;
189        }
190        long len = value.Length;
191        inputdata = new byte[len];
192
193        for (long i = 0; i < len; i++)
194          inputdata[i] = value[i];
195
196        NotifyUpdateInput();
197        GuiLogMessage("InputData changed.", NotificationLevel.Debug);
198      }
199    }
200    #endregion
201
202    #region Output
203
204    // Output
205    private List<CryptoolStream> listCryptoolStreamsOut = new List<CryptoolStream>();
206    private byte[] outputData = { };
207
208    /// <summary>
209    /// Notifies the update output.
210    /// </summary>
211    private void NotifyUpdateOutput()
212    {
213      OnPropertyChanged("HashOutputStream");
214      OnPropertyChanged("HashOutputData");
215    }
216
217
218    /// <summary>
219    /// Gets or sets the output inputdata stream.
220    /// </summary>
221    /// <value>The output inputdata stream.</value>
222    [PropertyInfo(Direction.Output, "Hashed Stream", "Output stream of the hashed value", "",
223      true, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
224    public CryptoolStream HashOutputStream
225    {
226      get
227      {
228        CryptoolStream outputDataStream = null;
229        if (outputData != null)
230        {
231          outputDataStream = new CryptoolStream();
232          outputDataStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputData);
233          listCryptoolStreamsOut.Add(outputDataStream);
234        }
235        GuiLogMessage("Got HashOutputStream.", NotificationLevel.Debug);
236        return outputDataStream;
237      }
238      //set
239      //{
240      //} //readonly
241    }
242
243    /// <summary>
244    /// Gets the output inputdata.
245    /// </summary>
246    /// <value>The output inputdata.</value>
247    [PropertyInfo(Direction.Output, "Hashed Data", "Output data of the hashed value", "",
248      true, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
249    public byte[] HashOutputData
250    {
251      get
252      {
253        GuiLogMessage("Got HashOutputData.", NotificationLevel.Debug);
254        return this.outputData;
255      }
256    }
257
258    #endregion
259
260    void Hash()
261    {
262      HMACTIGER2 tg = new HMACTIGER2();
263      outputData = tg.ComputeHash(inputdata);
264      NotifyUpdateOutput();
265    }
266
267    #region IPlugin Member
268
269
270#pragma warning disable 67
271    public event StatusChangedEventHandler OnPluginStatusChanged;
272    public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
273    public event PluginProgressChangedEventHandler OnPluginProgressChanged;
274#pragma warning restore
275
276
277    /// <summary>
278    /// Provide all presentation stuff in this user control, it will be opened in an tab.
279    /// Return null if your plugin has no presentation.
280    /// </summary>
281    /// <value>The presentation.</value>
282    public System.Windows.Controls.UserControl Presentation
283    {
284      get
285      {
286        return null;
287      }
288    }
289
290    /// <summary>
291    /// Gets the quick watch presentation
292    /// </summary>
293    /// <value>The quick watch presentation.</value>
294    public System.Windows.Controls.UserControl QuickWatchPresentation
295    {
296      get
297      {
298        return null;
299      }
300    }
301
302    /// <summary>
303    /// Will be called from editor before right before chain-run starts
304    /// </summary>
305    public void PreExecution()
306    {
307    }
308
309    /// <summary>
310    /// Will be called from editor while chain-run is active and after last necessary input
311    /// for plugin has been set.
312    /// </summary>
313    public void Execute()
314    {
315      Hash();
316    }
317
318    /// <summary>
319    /// Will be called from editor after last plugin in chain has finished its work.
320    /// </summary>
321    public void PostExecution()
322    {
323    }
324
325
326    public void Pause()
327    {
328    }
329
330    /// <summary>
331    /// Will be called from editor while chain-run is active. Plugin hast to stop work immediately.
332    /// </summary>
333    public void Stop()
334    {
335    }
336
337    /// <summary>
338    /// Will be called from editor after restoring settings and before adding to workspace.
339    /// </summary>
340    public void Initialize()
341    {
342    }
343
344    /// <summary>
345    /// Will be called from editor when element is deleted from worksapce.
346    /// Releases unmanaged and - optionally - managed resources
347    /// </summary>
348    public void Dispose()
349    {
350      foreach (CryptoolStream stream in listCryptoolStreamsOut)
351      {
352        stream.Close();
353      }
354      listCryptoolStreamsOut.Clear();
355    }
356
357    #endregion
358
359    #region INotifyPropertyChanged Member
360
361    public event PropertyChangedEventHandler  PropertyChanged;
362
363    /// <summary>
364    /// Called when [property changed].
365    /// </summary>
366    /// <param name="name">The name.</param>
367    protected void OnPropertyChanged(string name)
368    {
369      if (PropertyChanged != null)
370      {
371        if (name == "Settings")
372        {
373          Hash();
374        }
375        else
376          PropertyChanged(this, new PropertyChangedEventArgs(name));
377      }
378    }
379
380
381    /// <summary>
382    /// GUIs the log message.
383    /// </summary>
384    /// <param name="message">The message.</param>
385    /// <param name="logLevel">The log level.</param>
386    private void GuiLogMessage(string message, NotificationLevel logLevel)
387    {
388      EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this,
389        new GuiLogEventArgs(message, this, logLevel));
390    }
391
392    #endregion
393  }
394}
Note: See TracBrowser for help on using the repository browser.