注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Computer Science

I Pressed My Words Here. Will You Read ?

 
 
 

日志

 
 

C# Customizable MD5 encryption and Validation implementation  

2014-11-07 14:45:57|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

refer to :

MD5 Class

http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5%28v=vs.110%29.aspx

                /// <summary>
                /// Encrypt the string by using MD5
                /// </summary>
                /// <param name="input">string to be encrypted</param>
                /// <param name="md5Hash">the md5hash created by MD5 encryption function, e.g. System.Security.Cryptography.MD5CryptoServiceProvider(); Or MD5.Create();</param>
                /// <param name="stringifyMD5Bytes">the customizable delegate function to stringify the byte[], created by md5Hash.ComputeHash(string);</param>
                /// <returns>the encrypted string</returns>
                public static string GetMd5Hash(string input, System.Security.Cryptography.MD5 md5Hash = null, StringifyMD5Bytes stringifyMD5Bytes=null)
                {
                    md5Hash = md5Hash ?? new System.Security.Cryptography.MD5CryptoServiceProvider();
                    // Convert the input string to a byte array and compute the hash.
                    byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

                    // initiate stringifyMD5Bytes
                    stringifyMD5Bytes = stringifyMD5Bytes ?? (delegate(byte[] MD5Bytes)
                    {
                        // Create a new String builder to collect the bytes
                        // and create a string.
                        StringBuilder sBuilder = new StringBuilder();
                        // Loop through each byte of the hashed data 
                        // and format each one as a hexadecimal string.
                        for (int i = 0; i < MD5Bytes.Length; i++)
                        {
                            sBuilder.Append(MD5Bytes[i].ToString("x2"));
                        }
                        return sBuilder.ToString();
                    });

                    // Return the hexadecimal string.
                    return stringifyMD5Bytes(data);
                }

                /// <summary>
                /// Verify a hash against a string.
                /// </summary>
                /// <param name="hash">the encrypted string to be verified</param>
                /// <param name="input">the unencrypted string to encrypt</param>
                /// <param name="md5Hash">the md5hash created by MD5 encryption function, e.g. System.Security.Cryptography.MD5CryptoServiceProvider(); Or MD5.Create();</param>
                /// <param name="stringifyMD5Bytes">the customizable delegate function to stringify the byte[], created by md5Hash.ComputeHash(string);</param>
                /// <returns>the boolean result. true, passed the verification; false, not.</returns>
                public static bool VerifyMd5Hash(string hash, string input, System.Security.Cryptography.MD5 md5Hash = null, StringifyMD5Bytes stringifyMD5Bytes = null)
                {
                    md5Hash = md5Hash ?? new System.Security.Cryptography.MD5CryptoServiceProvider();
                    // Hash the input.
                    string hashOfInput = GetMd5Hash(input, md5Hash, stringifyMD5Bytes);

                    // Create a StringComparer an compare the hashes.
                    StringComparer comparer = StringComparer.OrdinalIgnoreCase;

                    if (0 == comparer.Compare(hashOfInput, hash))
                    {
                        return System.Convert.ToBoolean(GlobalClass.Const.ValidationResult.Passed);
                    }
                    else
                    {
                        return System.Convert.ToBoolean(GlobalClass.Const.ValidationResult.Failed);
                    }
                }



                /// <summary>
                /// Stringify the bytes from MD5 encryption.
                /// </summary>
                /// <param name="MD5Bytes">the byte[], created by md5Hash.ComputeHash(string);.</param>
                /// <returns>result string</returns>
                public delegate string StringifyMD5Bytes(byte[] MD5Bytes);
  评论这张
 
阅读(11)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017