Thread: [Csharp-classlib-cvs] demos/EasyMD5/EasyMD5/Classes clsEasyMD5.cs,NONE,1.1
Status: Inactive
Brought to you by:
generalpd
From: Marcel K. <gen...@us...> - 2005-05-03 20:12:54
|
Update of /cvsroot/csharp-classlib/demos/EasyMD5/EasyMD5/Classes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6387/EasyMD5/EasyMD5/Classes Added Files: clsEasyMD5.cs Log Message: - import of EasyMD5 demo --- NEW FILE: clsEasyMD5.cs --- // C# ClassLib // http://csharp-classlib.sourceforge.net // // // Copyright (C) 2005 Marcel Joachim Kloubert // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace CSharp_ClassLib.Cryptography { /*! \brief MD5 class * * Class that contains routines to handle MD5 much easier * * \author generalpd * * \version $Revision: 1.1 $ * * \date 2005-04-18 * * \bug none * * \todo none * * \remarks Last changed by: $Author: generalpd $ in revision $Revision: 1.1 $ on $Date: 2005/05/03 20:12:45 $ * * \note * $Log: clsEasyMD5.cs,v $ * Revision 1.1 2005/05/03 20:12:45 generalpd * - import of EasyMD5 demo * * */ public class EasyMD5 : System.Object { /// MD5 instance private System.Security.Cryptography.MD5CryptoServiceProvider m_oMD5; /// current checksum private byte[] m_bChecksum; /// ASCII encoder private System.Text.ASCIIEncoding m_oASCII; /*! \fn EasyMD5 () * \brief Class constructor that inits all nesessary stuff * * \code * * // Code example * * CSharp_ClassLib.Cryptography.EasyMD5 m_oMD5 = new CSharp_ClassLib.Cryptography.EasyMD5(); * * \endcode * */ public EasyMD5() { this.IntRest(); return; } /*! \fn EasyMD5(byte[] Expression) * \brief Class constructor that inits all nesessary stuff and calculates the MD5 hash from a byte array * * \param Expression The expression * * \code * * // Code example * * byte[] m_bExpression = { 0, 1, 2, 3, 4 }; * CSharp_ClassLib.Cryptography.EasyMD5 m_oMD5 = new CSharp_ClassLib.Cryptography.EasyMD5(m_bExpression); * * \endcode * */ public EasyMD5(byte[] Expression) { this.IntRest(); this.ExpressionBytes = Expression; return; } /*! \fn EasyMD5(System.IO.Stream stream) * \brief Class constructor that inits all nesessary stuff and calculates the MD5 hash from a stream * * \param stream The stream * * \code * * // Code example * * FileStream m_oFileStream = new FileStream("C:\\boot.ini", FileMode.Open); * CSharp_ClassLib.Cryptography.EasyMD5 m_oMD5 = new CSharp_ClassLib.Cryptography.EasyMD5(m_oFileStream); * * \endcode * */ public EasyMD5(System.IO.Stream stream) { this.IntRest(); this.Stream = stream; return; } /*! \fn EasyMD5(string filename) * \brief Class constructor that inits all nesessary stuff and calculates the MD5 hash from a file * * \param filename The full path of the file * * \code * * // Code example * * CSharp_ClassLib.Cryptography.EasyMD5 m_oMD5 = new CSharp_ClassLib.Cryptography.EasyMD5("C:\\boot.ini"); * * \endcode * * \exception System.IO.FileNotFoundException File not found! * */ public EasyMD5(string filename) { this.IntRest(); this.Filename = filename; return; } /*! \property byte[] ChecksumBytes * \brief Returns the current checksum * * \return The checksum * * \code * * // Code example * * byte[] m_bChecksum = m_oMD5.ChecksumBytes; * * \endcode * */ public byte[] ChecksumBytes { get { return this.m_bChecksum; } } /*! \property string ChecksumString * \brief Returns the current checksum * * \return The checksum * * \code * * // Code example * * Console.WriteLine("MD5 hash: " + m_oMD5.ChecksumString); * * \endcode * */ public string ChecksumString { get { return this.ToHexString(this.ChecksumBytes); } } /*! \property byte[] ExpressionBytes * \brief Calculates the checksum of a byte array * * \param value The expression * * \code * * // Code example * * m_oMD5.ExpressionBytes = m_oASCII.GetBytes("Support Mono!"); * Console.WriteLine("MD5 hash: " + m_oMD5.ChecksumString); * * \endcode * */ public byte[] ExpressionBytes { set { this.m_oMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); this.m_bChecksum = this.m_oMD5.ComputeHash(value); } } /*! \property string ExpressionString * \brief Calculates the checksum of a string * * \param value The expression * * \code * * // Code example * * m_oMD5.ExpressionString = "Support Mono!"; * Console.WriteLine("MD5 hash: " + m_oMD5.ChecksumString); * * \endcode * */ public string ExpressionString { set { this.ExpressionBytes = m_oASCII.GetBytes(value); } } /*! \property string Filename * \brief Calculates the checksum from a file * * \param value The full path of the file * * \code * * // Code example * * m_oMD5.Filename = "C:\\boot.ini"; * Console.WriteLine("MD5 hash: " + m_oMD5.ChecksumString); * * \endcode * * \exception System.IO.FileNotFoundException File not found! * */ public string Filename { set { if (!System.IO.File.Exists(value.Trim())) // file does not exist! throw ( new System.IO.FileNotFoundException("File was not found!", value.Trim()) ); this.Stream = ( new System.IO.FileStream(value.Trim(), System.IO.FileMode.Open) ); } } /*! \property string Stream * \brief Calculates the checksum from a stream * * \param value The stream instance * * \code * * // Code example * * m_oMD5.Stream = m_oMemoryStream; * Console.WriteLine("MD5 hash: " + m_oMD5.ChecksumString); * * \endcode * */ public System.IO.Stream Stream { set { this.m_oMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); this.m_bChecksum = this.m_oMD5.ComputeHash(value); } } /*! \fn void IntRest() * \brief Internal routine to init the rest stuff * */ private void IntRest() { this.m_oMD5 = null; this.m_bChecksum = new byte[0]; this.m_oASCII = new System.Text.ASCIIEncoding(); return; } /*! \fn string ToHexString (byte[] Expression) * \brief Internal routine that converts an expression to a hex string * * \param Expression The expression to convert * * \return The expression as hex string * */ private string ToHexString (byte[] Expression) { if (null == Expression) return ""; int i = 0; byte bHigh = 0; byte bLow = 0; string strBuffer = ""; string strHexString = "0123456789ABCDEF"; for (i = 0; i < Expression.GetLength(0); i++) { // calc the high & the low byte bHigh = (byte)System.Math.Floor((double)Expression[i] / 16.0); bLow = (byte)(Expression[i] % 16); // add the character as 2-character-hex-expression strBuffer += strHexString.Substring(bHigh, 1) + strHexString.Substring(bLow, 1); } return strBuffer; } /*! \fn void Clear () * \brief Clears the MD5 checksum var * */ public void Clear () { this.m_bChecksum = new byte[0]; return; } } } |