source: trunk/CrypPlugins/MD5/Algorithm/PresentableMD5State.cs @ 970

Last change on this file since 970 was 970, checked in by pretzsch, 12 years ago

+ MD5 plugin: Improved presentation

File size: 8.1 KB
Line 
1/*
2   Copyright 2009 Holger Pretzsch, University of Duisburg-Essen
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
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.Reflection;
22using System.Windows;
23
24namespace Cryptool.MD5.Algorithm
25{
26    /// <summary>
27    /// The state of a <see cref="PresentableMD5"/> algorithm
28    /// </summary>
29    /// <remarks>
30    /// This class contains a simple enumeration describing the current state. It also contains any computed results which are needed to perform the next step of the algorithm.
31    /// </remarks>
32    /// <seealso cref="PresentableMD5"/>
33    public class PresentableMD5State
34    {
35        /// <summary>
36        /// Description of the current state
37        /// </summary>
38        /// <seealso cref="MD5StateDescription"/>
39        public MD5StateDescription State { get; set; }
40
41        /// <summary>
42        /// The size of one MD5 data block
43        /// </summary>
44        public const int DATA_BLOCK_SIZE = 64;
45
46        /// <summary>
47        /// Temporary variable A, used inside the compression function
48        /// </summary>
49        public uint A { get; set; }
50
51        /// <summary>
52        /// Temporary variable B, used inside the compression function
53        /// </summary>
54        public uint B { get; set; }
55
56        /// <summary>
57        /// Temporary variable C, used inside the compression function
58        /// </summary>
59        public uint C { get; set; }
60
61        /// <summary>
62        /// Temporary variable D, used inside the compression function
63        /// </summary>
64        public uint D { get; set; }
65
66        /// <summary>
67        /// Determines in which round of the compression function the algorithm currently is, value range 0-3
68        /// </summary>
69        public uint RoundIndex;
70
71        /// <summary>
72        /// Determines in which round of the compression function the algorithm currently is, value range 1-4
73        /// </summary>
74        public uint Round
75        {
76            get
77            {
78                return RoundIndex + 1;
79            }
80        }
81               
82        /// <summary>
83        /// Returns the function name of the inner round function currently used
84        /// </summary>
85        public string RoundFunctionName
86        {
87            get
88            {
89                                switch (Round)
90                                {
91                                        case 1:
92                                                return "F";
93                                        case 2:
94                                                return "G";
95                                        case 3:
96                                                return "H";
97                                        case 4:
98                                                return "I";
99                                        default:
100                                                return "";
101                                }
102            }
103        }
104
105        /// <summary>
106        /// Determines in which step of the current round of the compression function the algorithm currently is, value range 0-15
107        /// </summary>
108        public uint RoundStepIndex;
109
110        /// <summary>
111        /// Determines in which step of the current round of the compression function the algorithm currently is, value range 1-16
112        /// </summary>
113        public uint RoundStep
114        {
115            get
116            {
117                return RoundStepIndex + 1;
118            }
119        }
120
121        public uint AbsoluteStepIndex
122        {
123            get
124            {
125                return RoundIndex * 16 + RoundStepIndex;
126            }
127        }
128
129        public uint ShiftConstant
130        {
131            get
132            {
133                return PresentableMD5.ShiftConstantTable[AbsoluteStepIndex];
134            }
135        }
136
137        public uint AdditionConstant
138        {
139            get
140            {
141                return PresentableMD5.AdditionConstantTable[AbsoluteStepIndex];
142            }
143        }
144
145        /// <summary>
146        /// The total number of bytes hashed so far
147        /// </summary>
148        /// <remarks>
149        /// Updates after the compression function has run.
150        /// </remarks>
151        public uint BytesHashed { get; set; }
152
153        /// <summary>
154        /// Have all padding steps been completed?
155        /// </summary>
156        public bool IsPaddingDone { get; set; }
157
158        /// <summary>
159        /// Is this step the last step in the round?
160        /// </summary>
161        public bool IsLastStepInRound
162        {
163            get
164            {
165                return RoundStep == 16;
166            }
167        }
168
169        /// <summary>
170        /// Is this the last round for this call of the compression function?
171        /// </summary>
172        public bool IsLastRound
173        {
174            get
175            {
176                return Round == 4;
177            }
178        }
179
180        /// <summary>
181        /// Accumulated hash value H1
182        /// </summary>
183        public uint H1 { get; set; }
184
185        /// <summary>
186        /// Accumulated hash value H2
187        /// </summary>
188        public uint H2 { get; set; }
189
190        /// <summary>
191        /// Accumulated hash value H3
192        /// </summary>
193        public uint H3 { get; set; }
194
195        /// <summary>
196        /// Accumulated hash value H4
197        /// </summary>
198        public uint H4 { get; set; }
199
200        /// <summary>
201        /// The data used in the current run of the compression function, read into unsigned integer variables
202        /// </summary>
203        public uint[] DataAsIntegers { get; set; }
204
205        /// <summary>
206        /// The data in the buffer
207        /// </summary>
208        /// <remarks>
209        /// Normally, this holds exactly 64 bytes of data. It may contain less immediately after reading the last data block, which is usually less than 64 bytes long. It may contain more (exactly 128 bytes in that case) when padding requires a new block to be appended to the buffer.
210        /// </remarks>
211        public byte[] Data { get; set; }
212
213        /// <summary>
214        /// Returns the 64 byte block for the current iteration (downsizes the 128 byte Data array to 64 bytes)
215        /// </summary>
216        public byte[] CurrentDataBlock
217        {
218            get
219            {
220                uint currentBlockLength = Math.Min(DataLength, 64);
221                byte[] result = new byte[currentBlockLength];
222                Array.Copy(Data, DataOffset, result, 0, currentBlockLength);
223                return result;
224            }
225        }
226
227        /// <summary>
228        /// Determines the length of data in the buffer
229        /// </summary>
230        public uint DataLength { get; set; }
231
232        /// <summary>
233        /// Determines the offset to be used when running the compression function
234        /// </summary>
235        /// <remarks>
236        /// This is always 0 except for the last step, where it may be 64 to process an extra block appended through padding.
237        /// </remarks>
238        public uint DataOffset { get; set; }
239
240        /// <summary>
241        /// Holds the length of data in bit as determined at the start of the padding process
242        /// </summary>
243        public ulong LengthInBit { get; set; }
244
245        /// <summary>
246        /// Default constructor
247        /// </summary>
248        public PresentableMD5State()
249        {
250        }
251
252        /// <summary>
253        /// Copy constructor
254        /// </summary>
255        /// <param name="other">State to copy</param>
256        public PresentableMD5State(PresentableMD5State other)
257        {
258            foreach (FieldInfo fi in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
259                fi.SetValue(this, fi.GetValue(other));
260
261
262            foreach (PropertyInfo pi in GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
263                if (pi.CanWrite && pi.CanRead)
264                    pi.SetValue(this, pi.GetValue(other, null), null);
265        }
266    }
267}
Note: See TracBrowser for help on using the repository browser.