csharp 计算MD5-Hash

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 计算MD5-Hash相关的知识,希望对你有一定的参考价值。

<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Calculates a MD5 hash from the given string and uses the given</span>
<span style="color: #008080; font-style: italic;">/// encoding.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;Input&quot;&gt;Input string&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;UseEncoding&quot;&gt;Encoding method&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;MD5 computed string&lt;/returns&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> CalculateMD5<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> Input, Encoding UseEncoding<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Security</span>.<span style="color: #0000FF;">Cryptography</span>.<span style="color: #0000FF;">MD5CryptoServiceProvider</span> CryptoService;
    CryptoService = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Security</span>.<span style="color: #0000FF;">Cryptography</span>.<span style="color: #0000FF;">MD5CryptoServiceProvider</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> InputBytes = UseEncoding.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span>Input<span style="color: #000000;">&#41;</span>;
    InputBytes = CryptoService.<span style="color: #0000FF;">ComputeHash</span><span style="color: #000000;">&#40;</span>InputBytes<span style="color: #000000;">&#41;</span>;
    <span style="color: #0600FF;">return</span> BitConverter.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span>InputBytes<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;-&quot;</span>, <span style="color: #808080;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Calculates a MD5 hash from the given string. </span>
<span style="color: #008080; font-style: italic;">/// (By using the default encoding)</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;Input&quot;&gt;Input string&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;MD5 computed string&lt;/returns&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> CalculateMD5<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> Input<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// That's just a shortcut to the base method</span>
    <span style="color: #0600FF;">return</span> CalculateMD5<span style="color: #000000;">&#40;</span>Input, <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Text</span>.<span style="color: #0000FF;">Encoding</span>.<span style="color: #0600FF;">Default</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>

以上是关于csharp 计算MD5-Hash的主要内容,如果未能解决你的问题,请参考以下文章

csharp [初学者]测验成绩计算器

csharp 计算含税,税额,不含税

csharp Linq Parallel并行计算的用法

csharp 如何计算功率指数值。

csharp 如何计算功率指数值。

csharp 运行时间计算