请问C#中MD5函数ComputeHash是怎么实现的?
//
        // 摘要:
        //     计算指定字节数组的哈希值。
        //
        // 参数:
        //   buffer:
        //     要计算其哈希代码的输入。
        //
        // 返回结果:
        //     计算所得的哈希代码。
        //
        // 异常:
        //   System.ArgumentNullException:
        //     buffer 为 null。
        //
        //   System.ObjectDisposedException:
        //     此对象已释放。
        public byte[] ComputeHash(byte[] buffer);因为我本人在移植一个C#的程序到PHP,但是这个函数我不知道他怎么实现的。
请大家帮忙,给个提示,谢谢!

解决方案 »

  1.   

    非常感谢!
    通过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! 
      

  2.   

    http://blog.csdn.net/aimeast/archive/2008/11/19/3337153.aspx
      

  3.   

    如果想让php的结果等同于.net的结果,那么需要对md5函数的结果进行16进制字符串到标准字符串的转换。
    那么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);
      

  4.   


    难道感谢别人的帮助也有错?
    To:Allen2064
    你那个php实现.net的md5用在php4里还行。php5已经是32位的了。
      

  5.   

    问题已经解决,请参考:
    http://topic.csdn.net/u/20100217/18/a518dce7-dbbd-459e-966c-c398728bef79.html