Get MD5 for byte array - CSharp Security

CSharp examples for Security:MD5

Description

Get MD5 for byte array

Demo Code


using System.Text;
using System.Security.Cryptography;
using System;//from ww  w  .j a  v a  2 s. c o  m

public class Main{
        public static string GetMD5(byte[] array, int offset, int count)
        {
            string str;
            MD5 md = null;
            try
            {
                md = new MD5CryptoServiceProvider();
                md.Initialize();
                str = ByteArrayToHexadecimalString(md.ComputeHash(array, offset, count));
            }
            finally
            {
                if (md != null)
                {
                    md.Clear();
                }
            }
            return str;
        }
        public static string GetMD5(byte[] array)
        {
            if (array.Length < 1)
            {
                throw new ArgumentException("array should not be empty.");
            }
            return GetMD5(array, 0, array.Length);
        }
        public static string GetMD5(string str)
        {
            return GetMD5(Encoding.UTF8.GetBytes(str));
        }
}

Related Tutorials