请问C#中MD5函数ComputeHash是怎么实现的?
//
// 摘要:
// 计算指定字节数组的哈希值。
//
// 参数:
// buffer:
// 要计算其哈希代码的输入。
//
// 返回结果:
// 计算所得的哈希代码。
//
// 异常:
// System.ArgumentNullException:
// buffer 为 null。
//
// System.ObjectDisposedException:
// 此对象已释放。
public byte[] ComputeHash(byte[] buffer);因为我本人在移植一个C#的程序到PHP,但是这个函数我不知道他怎么实现的。
请大家帮忙,给个提示,谢谢!
//
// 摘要:
// 计算指定字节数组的哈希值。
//
// 参数:
// buffer:
// 要计算其哈希代码的输入。
//
// 返回结果:
// 计算所得的哈希代码。
//
// 异常:
// System.ArgumentNullException:
// buffer 为 null。
//
// System.ObjectDisposedException:
// 此对象已释放。
public byte[] ComputeHash(byte[] buffer);因为我本人在移植一个C#的程序到PHP,但是这个函数我不知道他怎么实现的。
请大家帮忙,给个提示,谢谢!
解决方案 »
- 【自定义事件监听变量】求大神分析下代码。。不知道哪里问题
- 新手学习:如何在类里访问Form1里的datagridview1
- textbox输入数字和小数点的方法,网上找了一堆都没有理想的!!!
- c#线程级别问题,很有意思小程序,也很有问题
- 提问还需要智慧啊,怎么提呢?
- 用C# 写一条SQL的日期查询语句
- 一个nhibernate的问求解
- 我拿C#写了个windows server,要访问sqlserver数据库,用 Integrated Security=SSPI 方式,出问题。
- 如何检查某一个值是否包含在 DropDownList 中﹖
- 100分请求解决线程堵塞、不能中断的问题
- C# 怎么办???
- 高手帮忙解释下c#的一段代码,急!!!
通过Reflector我找到了这个函数的实现方法!
public byte[] ComputeHash(byte[] buffer)
{
if (this.m_bDisposed)
{
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic"));
}
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
this.HashCore(buffer, 0, buffer.Length);
this.HashValue = this.HashFinal();
byte[] buffer2 = (byte[]) this.HashValue.Clone();
this.Initialize();
return buffer2;
}
居然有这么强的工具,哎 我孤陋寡闻啊!谢谢wuyq11!
那么php程序应改为:$md5hex=md5("xutf");
$len=strlen($md5hex)/2;
$md5raw="";
for($i=0;$i<$len;$i++) { $md5raw=$md5raw . chr(hexdec(substr($md5hex,$i*2,2))); } $keyMd5=base64_encode($md5raw);如果想让.net的结果等同于php的结果,那么需要把md5.ComputeHash方法输出的结果转换成16进制字符串,那么.net程序应该改为:System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
byte[] bytesSrc = encoding.GetBytes("xutf");
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(bytesSrc);StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
sb.AppendFormat("{0:x2}", result[i]);
string s1=sb.ToString();
byte[] bytesmd5 = encoding.GetBytes(s1);
string keymd5=Convert.ToBase64String(bytesmd5);
难道感谢别人的帮助也有错?
To:Allen2064
你那个php实现.net的md5用在php4里还行。php5已经是32位的了。
http://topic.csdn.net/u/20100217/18/a518dce7-dbbd-459e-966c-c398728bef79.html