clsEasyMD5.cs

00001 // C# ClassLib
00002 // http://csharp-classlib.sourceforge.net
00003 //
00004 //
00005 // Copyright (C) 2005 Marcel Joachim Kloubert
00006 // 
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 // 
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 //
00021 
00022 using System;
00023 using System.IO;
00024 using System.Security.Cryptography;
00025 using System.Text;
00026 
00027 namespace CSharp_ClassLib.Cryptography
00028 {
00051         public class EasyMD5 : System.Object
00052         {
00054                 private System.Security.Cryptography.MD5CryptoServiceProvider m_oMD5;
00056         private byte[] m_bChecksum;
00058         private System.Text.ASCIIEncoding m_oASCII;
00059                 
00072                 public EasyMD5()
00073                 {
00074                         this.IntRest();
00075                         
00076                         return;
00077                 }
00078                 
00094                 public EasyMD5(byte[] Expression)
00095                 {
00096                         this.IntRest();
00097                         this.ExpressionBytes = Expression;
00098                         
00099                         return;
00100                 }
00101                 
00117                 public EasyMD5(System.IO.Stream stream)
00118                 {
00119                         this.IntRest();
00120                         this.Stream = stream;
00121                         
00122                         return;
00123                 }
00124                 
00141                 public EasyMD5(string filename)
00142                 {
00143                         this.IntRest();
00144                         this.Filename = filename;
00145                         
00146                         return;
00147                 }
00148                 
00163                 public byte[] ChecksumBytes { get { return this.m_bChecksum; } }
00164                 
00179                 public string ChecksumString { get { return this.ToHexString(this.ChecksumBytes); } }
00180                 
00196                 public byte[] ExpressionBytes
00197                 {
00198                         set
00199                         {
00200                                 this.m_oMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();                                                      
00201                                 this.m_bChecksum = this.m_oMD5.ComputeHash(value);
00202                         }
00203                 }
00204                 
00220                 public string ExpressionString { set { this.ExpressionBytes = m_oASCII.GetBytes(value); } }
00221                 
00239                 public string Filename
00240                 {
00241                         set
00242                         {
00243                                 if (!System.IO.File.Exists(value.Trim()))
00244                                         // file does not exist!
00245                                         throw ( new System.IO.FileNotFoundException("File was not found!", value.Trim()) );
00246                                 
00247                                 this.Stream = ( new System.IO.FileStream(value.Trim(), System.IO.FileMode.Open) );
00248                         }
00249                 }
00250                 
00266                 public System.IO.Stream Stream
00267                 {
00268                         set
00269                         {
00270                                 this.m_oMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
00271                                 this.m_bChecksum = this.m_oMD5.ComputeHash(value);
00272                         }
00273                 }
00274                 
00279                 private void IntRest()
00280                 {
00281                         this.m_oMD5 = null;
00282                         this.m_bChecksum = new byte[0];
00283                         
00284                         this.m_oASCII = new System.Text.ASCIIEncoding();
00285                         
00286                         return;
00287                 }
00288                 
00297                 private string ToHexString (byte[] Expression)
00298                 {
00299                         if (null == Expression)
00300                                 return "";
00301                                 
00302                         int i = 0;
00303                         byte bHigh = 0;
00304                         byte bLow = 0;
00305                         string strBuffer = "";
00306                         string strHexString = "0123456789ABCDEF";
00307 
00308                         for (i = 0; i < Expression.GetLength(0); i++)
00309                         {
00310                                 // calc the high & the low byte
00311                                 bHigh = (byte)System.Math.Floor((double)Expression[i] / 16.0);
00312                                 bLow = (byte)(Expression[i] % 16);
00313         
00314                                 // add the character as 2-character-hex-expression
00315                                 strBuffer += strHexString.Substring(bHigh, 1) + strHexString.Substring(bLow, 1);
00316                         }
00317                 
00318                         return strBuffer;
00319                 }
00320 
00325                 public void Clear ()
00326                 {
00327                         this.m_bChecksum = new byte[0];
00328                         
00329                         return;
00330                 }
00331                 
00332         }
00333 }

Generated on Thu Jun 2 08:41:49 2005 for C# ClassLib by  doxygen 1.4.1