Click here to Skip to main content
15,897,273 members
Home / Discussions / C#
   

C#

 
Questioncode to convert doc file to txt file Pin
Anil Kumar.Arvapalli26-Feb-10 20:31
Anil Kumar.Arvapalli26-Feb-10 20:31 
AnswerRe: code to convert doc file to txt file Pin
Richard MacCutchan26-Feb-10 21:36
mveRichard MacCutchan26-Feb-10 21:36 
AnswerRe: code to convert doc file to txt file Pin
Luc Pattyn26-Feb-10 22:27
sitebuilderLuc Pattyn26-Feb-10 22:27 
QuestionStoring password Pin
Reza Shojaee26-Feb-10 18:18
Reza Shojaee26-Feb-10 18:18 
AnswerMessage Closed Pin
26-Feb-10 18:45
stancrm26-Feb-10 18:45 
GeneralRe: Storing password Pin
Reza Shojaee26-Feb-10 18:49
Reza Shojaee26-Feb-10 18:49 
GeneralRe: Storing password Pin
AspDotNetDev26-Feb-10 18:51
protectorAspDotNetDev26-Feb-10 18:51 
GeneralRe: Storing password [edited] [modified] Pin
OriginalGriff26-Feb-10 22:42
mveOriginalGriff26-Feb-10 22:42 
Don't use MD5 - it is officially "broken". Use SHA instead. Attached the class I use for SHA handling:
using System;
using System.Text;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Security;
using System.IO;

namespace UtilityControls
    {
    /// <summary>
    /// Supports SHA2 hash processing
    /// NOTE: SHA3 is under developemnt and the specification is due in 2012
    /// </summary>
    public class SHA2Hash
        {
        #region Fields
        private byte[] _SHA2Data;
        /// <summary>
        /// Bits in an SHA2 hash
        /// </summary>
        public const int SHA2Bits = 512;
        /// <summary>
        /// Bytes in an SHA2 hash
        /// </summary>
        public const int SHA2Bytes = SHA2Bits / 8;
        /// <summary>
        /// Size of SHA2Hash string
        /// </summary>
        public const int Length = SHA2Bytes * 2;     // As in SHA2 as a string...
        #endregion

        #region Properties
        /// <summary>
        /// Returns the SHA2 hash as a string
        /// </summary>
        public string SHA2data
            {
            get
                {
                StringBuilder sb = new StringBuilder(Length);
                foreach (byte b in _SHA2Data)
                    {
                    sb.Append(b.ToString("x2"));
                    }
                return sb.ToString();
                }
            }
        #endregion

        #region Constructors
        /// <summary>
        /// Constructs an SHA2 hash from a stream
        /// </summary>
        /// <param name="s">stream, data to construct SHA2 from</param>
        public SHA2Hash(Stream s)
            {
            SHA512 shaM = new SHA512Managed();
            _SHA2Data = shaM.ComputeHash(s);
            }

        /// <summary>
        /// Constructs an SHA2 hash from a SecureString
        /// </summary>
        /// <param name="ss">SecureString, data to construct SHA2 from</param>
        public SHA2Hash(SecureString ss)
            {
            SHA512 shaM = new SHA512Managed();
            if (ss != null)
                {
                IntPtr ptr = Marshal.SecureStringToBSTR(ss);
                byte[] bs = Encoding.UTF8.GetBytes(Marshal.PtrToStringAuto(ptr));
                _SHA2Data = shaM.ComputeHash(bs);
                Marshal.ZeroFreeBSTR(ptr);
                }
            }

        /// <summary>
        /// Constructs an SHA2 hash from a string
        /// </summary>
        /// <param name="s">string, data to construct SHA2 from</param>
        public SHA2Hash(string s)
            {
            SHA512 shaM = new SHA512Managed();
            byte[] bs = Encoding.UTF8.GetBytes(s);
            _SHA2Data = shaM.ComputeHash(bs);
            }

        /// <summary>
        /// Constructs an SHA2 hash from a string containing an SHA2 hash
        /// </summary>
        /// <param name="s">string containing hash</param>
        /// <param name="bRestore">Always true</param>
        public SHA2Hash(string s, bool bRestore)
            {
            if (!bRestore)
                {
                throw new ApplicationException("Invalid Restore Constructor parameter");
                }
            if (s.Length == SHA2Hash.Length)
                {
                byte[] bytes = new byte[SHA2Hash.SHA2Bytes];
                char[] chars = s.ToCharArray();
                int i = 0;
                while ((i >= 0) && (i < SHA2Hash.SHA2Bytes))
                    {
                    if (StaticMethods.IsHex(chars[(i * 2) + 0]) && StaticMethods.IsHex(chars[(i * 2) + 1]))
                        {
                        byte b1 = StaticMethods.ByteHex(chars[(i * 2) + 0]);
                        byte b2 = StaticMethods.ByteHex(chars[(i * 2) + 1]);
                        bytes[i] = (byte) ((b1 << 4) | b2);
                        i++;
                        }
                    else
                        {
                        i = -1;
                        }
                    }
                if (i > 0)
                    {
                    _SHA2Data = bytes;
                    return;
                    }
                }
            throw new ApplicationException("Bad restore hash data");
            }

        /// <summary>
        /// Constructs an SHA2 hash from an array of bytes
        /// </summary>
        /// <param name="bytes">byte[], data to construct SHA2 from</param>
        public SHA2Hash(byte[] bytes)
            {
            SHA512 shaM = new SHA512Managed();
            _SHA2Data = shaM.ComputeHash(bytes);
            }

        /// <summary>
        /// Constructs an SHA2 hash from an array of chars
        /// </summary>
        /// <param name="chars">char[], data to construct SHA2 from</param>
        public SHA2Hash(char[] chars)
            {
            SHA512 shaM = new SHA512Managed();
            byte[] bs = Encoding.UTF8.GetBytes(chars);
            _SHA2Data = shaM.ComputeHash(bs);
            }
        #endregion

        #region Overrides
        /// <summary>
        /// Overridden to let HashTable and ArryList work
        /// </summary>
        /// <returns>Hash code for instance</returns>
        public override int GetHashCode()
            {
            int hash = 0;
            for (int i = 0; i < SHA2Bytes; i += 4)
                {
                hash ^= StaticMethods.LittleEndian(_SHA2Data[i + 0],
                                                   _SHA2Data[i + 1], 
                                                   _SHA2Data[i + 2], 
                                                   _SHA2Data[i + 3]);
                }
            return base.GetHashCode() ^ hash;
            }

        /// <summary>
        /// Performs an equality test between two SHA2Hashes
        /// </summary>
        /// <param name="obj">SHA2Hash to compare</param>
        /// <returns>true if the instances are the same</returns>
        public override bool Equals(object obj)
            {
            return (this == (SHA2Hash)obj);
            }
        #endregion

        #region Public methods
        /// <summary>
        /// Performs an equality test between two SHA2Hashes
        /// </summary>
        /// <param name="m1">LHS of == test</param>
        /// <param name="m2">RHS of == test</param>
        /// <returns>true if the SHA2Hashes are the same</returns>
        public static bool operator ==(SHA2Hash m1, SHA2Hash m2)
            {
            if ((object)m1 == null)
                {
                if ((object)m2 == null)
                    {
                    // Both null - i.e. if (instance == null)...
                    return true;
                    }
                }
            if (((object)m1 == null) || ((object)m2 == null))
                {
                return false;
                }
            for (int i = 0; i < SHA2Bytes; i++)
                {
                if (m1._SHA2Data[i] != m2._SHA2Data[i])
                    {
                    return false;
                    }
                }
            return true;
            }

        /// <summary>
        /// Performs an inequality test between two SHA2Hashes
        /// </summary>
        /// <param name="m1">LHS of == test</param>
        /// <param name="m2">RHS of == test</param>
        /// <returns>true if the SHA2Hashes are the same</returns>
        public static bool operator !=(SHA2Hash m1, SHA2Hash m2)
            {
            if ((object)m1 == null)
                {
                if ((object)m2 == null)
                    {
                    // Both null - i.e. if (instance == null)...
                    return false;
                    }
                }
            if (((object)m1 == null) || ((object)m2 == null))
                {
                return true;
                }
            for (int i = 0; i < SHA2Bytes; i++)
                {
                if (m1._SHA2Data[i] != m2._SHA2Data[i])
                    {
                    return true;
                    }
                }
            return false;
            }
        #endregion

        #region Private Methods
        #endregion
        }
    }

[edit]Forgot to use "Encode...when pasting" option.[/edit]
[edit again]Missed the < of </pre>[/edit again]
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace

C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

modified on Saturday, February 27, 2010 7:51 AM

GeneralRe: Storing password [edited] Pin
AspDotNetDev27-Feb-10 0:37
protectorAspDotNetDev27-Feb-10 0:37 
GeneralRe: Storing password [edited] Pin
Richard MacCutchan27-Feb-10 1:24
mveRichard MacCutchan27-Feb-10 1:24 
GeneralRe: Storing password [edited] Pin
Dan Mos28-Feb-10 7:16
Dan Mos28-Feb-10 7:16 
GeneralRe: Storing password Pin
OriginalGriff26-Feb-10 21:54
mveOriginalGriff26-Feb-10 21:54 
AnswerRe: Storing password Pin
Un Known Legend27-Feb-10 2:54
Un Known Legend27-Feb-10 2:54 
AnswerRe: Storing password Pin
AspDotNetDev26-Feb-10 18:50
protectorAspDotNetDev26-Feb-10 18:50 
AnswerRe: Storing password Pin
Abhinav S26-Feb-10 22:19
Abhinav S26-Feb-10 22:19 
AnswerRe: Storing password Pin
PIEBALDconsult27-Feb-10 3:57
mvePIEBALDconsult27-Feb-10 3:57 
QuestionPatch file (Create new file from previous file + changes) Pin
sodevrom26-Feb-10 14:23
sodevrom26-Feb-10 14:23 
AnswerRe: Patch file (Create new file from previous file + changes) Pin
Dave Kreskowiak26-Feb-10 18:00
mveDave Kreskowiak26-Feb-10 18:00 
GeneralRe: Patch file (Create new file from previous file + changes) Pin
sodevrom27-Feb-10 14:41
sodevrom27-Feb-10 14:41 
GeneralRe: Patch file (Create new file from previous file + changes) Pin
Dave Kreskowiak27-Feb-10 18:53
mveDave Kreskowiak27-Feb-10 18:53 
Questionquestion about webviewer control Pin
tonyonlinux26-Feb-10 12:46
tonyonlinux26-Feb-10 12:46 
AnswerRe: question about webviewer control Pin
AspDotNetDev26-Feb-10 13:18
protectorAspDotNetDev26-Feb-10 13:18 
GeneralRe: question about webviewer control Pin
tonyonlinux26-Feb-10 18:21
tonyonlinux26-Feb-10 18:21 
QuestionOpenSSL.NET Tutorial? [modified] Pin
Kiotaya26-Feb-10 11:13
Kiotaya26-Feb-10 11:13 
AnswerRe: OpenSSL.NET Tutorial? Pin
carlecomm27-Feb-10 0:10
carlecomm27-Feb-10 0:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.