C#中与PHP格式对应的md5函数

分享PHPC# by 达达 at 2011-07-19

如题

public static string MD5(string password)
{
    byte[] textBytes = System.Text.Encoding.Default.GetBytes(password);

    System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
    cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();

    byte[] hash = cryptHandler.ComputeHash(textBytes);

    StringBuilder ret = new StringBuilder(32);

    foreach (byte a in hash)
    {
        if (a < 16)
            ret.Append("0").Append(a.ToString("x"));
        else
            ret.Append(a.ToString("x"));
    }

    return ret.ToString();
}