MD5 String - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:MD5

Description

MD5 String

Demo Code


using System.Text;
using System.Security.Cryptography;
using System.IO;/*  w w  w  .  ja  v a  2  s  .  c o  m*/
using System;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        public static string MD5(string sourceStr)
        {
            string str = string.Empty;
            System.Security.Cryptography.MD5 md = System.Security.Cryptography.MD5.Create();
            MemoryStream stream = new MemoryStream();
            StreamWriter writer = new StreamWriter(stream);
            StreamReader reader = new StreamReader(stream);
            if (sourceStr == null)
            {
                sourceStr = string.Empty;
            }
            writer.Write(sourceStr);
            writer.Flush();
            stream.Seek(0L, SeekOrigin.Begin);
            byte[] buffer = md.ComputeHash(stream);
            stream.SetLength(0L);
            writer.Flush();
            for (int i = 0; i < buffer.Length; i++)
            {
                writer.Write("{0:X2}", buffer[i]);
            }
            writer.Flush();
            stream.Seek(0L, SeekOrigin.Begin);
            str = reader.ReadToEnd();
            writer.Close();
            writer.Dispose();
            reader.Close();
            reader.Dispose();
            stream.Close();
            stream.Dispose();
            return str;
        }
}

Related Tutorials