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

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

MD5 visualization: Fixed and improved presentation of parsed data block integers

File size: 8.7 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        /// <summary>
122        /// Absolute index of the current step, value range 0 - 63
123        /// </summary>
124        public uint AbsoluteStepIndex
125        {
126            get
127            {
128                return RoundIndex * 16 + RoundStepIndex;
129            }
130        }
131
132        /// <summary>
133        /// Shift constant used for current step
134        /// </summary>
135        public uint ShiftConstant
136        {
137            get
138            {
139                return PresentableMD5.ShiftConstantTable[AbsoluteStepIndex];
140            }
141        }
142
143        /// <summary>
144        /// Addition constant used for current step
145        /// </summary>
146        public uint AdditionConstant
147        {
148            get
149            {
150                return PresentableMD5.AdditionConstantTable[AbsoluteStepIndex];
151            }
152        }
153
154        /// <summary>
155        /// The index of the message part used for current step
156        /// </summary>
157        public int MessagePartIndex
158        {
159            get
160            {
161                return PresentableMD5.GetWordIndex(RoundIndex, AbsoluteStepIndex);
162            }
163        }
164
165        /// <summary>
166        /// The total number of bytes hashed so far
167        /// </summary>
168        /// <remarks>
169        /// Updates after the compression function has run.
170        /// </remarks>
171        public uint BytesHashed { get; set; }
172
173        /// <summary>
174        /// Have all padding steps been completed?
175        /// </summary>
176        public bool IsPaddingDone { get; set; }
177
178        /// <summary>
179        /// Is this step the last step in the round?
180        /// </summary>
181        public bool IsLastStepInRound
182        {
183            get
184            {
185                return RoundStep == 16;
186            }
187        }
188
189        /// <summary>
190        /// Is this the last round for this call of the compression function?
191        /// </summary>
192        public bool IsLastRound
193        {
194            get
195            {
196                return Round == 4;
197            }
198        }
199
200        /// <summary>
201        /// Accumulated hash value H1
202        /// </summary>
203        public uint H1 { get; set; }
204
205        /// <summary>
206        /// Accumulated hash value H2
207        /// </summary>
208        public uint H2 { get; set; }
209
210        /// <summary>
211        /// Accumulated hash value H3
212        /// </summary>
213        public uint H3 { get; set; }
214
215        /// <summary>
216        /// Accumulated hash value H4
217        /// </summary>
218        public uint H4 { get; set; }
219
220        /// <summary>
221        /// The data used in the current run of the compression function, read into unsigned integer variables
222        /// </summary>
223        public uint[] DataAsIntegers { get; set; }
224
225        /// <summary>
226        /// The data in the buffer
227        /// </summary>
228        /// <remarks>
229        /// 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.
230        /// </remarks>
231        public byte[] Data { get; set; }
232
233        /// <summary>
234        /// Returns the 64 byte block for the current iteration (downsizes the 128 byte Data array to 64 bytes)
235        /// </summary>
236        public byte[] CurrentDataBlock
237        {
238            get
239            {
240                uint currentBlockLength = Math.Min(DataLength, 64);
241                byte[] result = new byte[currentBlockLength];
242                Array.Copy(Data, DataOffset, result, 0, currentBlockLength);
243                return result;
244            }
245        }
246
247        /// <summary>
248        /// Determines the length of data in the buffer
249        /// </summary>
250        public uint DataLength { get; set; }
251
252        /// <summary>
253        /// Determines the offset to be used when running the compression function
254        /// </summary>
255        /// <remarks>
256        /// This is always 0 except for the last step, where it may be 64 to process an extra block appended through padding.
257        /// </remarks>
258        public uint DataOffset { get; set; }
259
260        /// <summary>
261        /// Holds the length of data in bit as determined at the start of the padding process
262        /// </summary>
263        public ulong LengthInBit { get; set; }
264
265        /// <summary>
266        /// Default constructor
267        /// </summary>
268        public PresentableMD5State()
269        {
270        }
271
272        /// <summary>
273        /// Copy constructor
274        /// </summary>
275        /// <param name="other">State to copy</param>
276        public PresentableMD5State(PresentableMD5State other)
277        {
278            foreach (FieldInfo fi in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
279                fi.SetValue(this, fi.GetValue(other));
280
281
282            foreach (PropertyInfo pi in GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
283                if (pi.CanWrite && pi.CanRead)
284                    pi.SetValue(this, pi.GetValue(other, null), null);
285        }
286    }
287}
Note: See TracBrowser for help on using the repository browser.