douzi1991 2011-10-18 15:57
浏览 37

将C#行转换为PHP

I have the following function in C#:

    static MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider();

    public string Guid GetMD5(string str)
    {
        lock (md5Provider)
        {
            return (new Guid(md5Provider.ComputeHash(Encoding.Unicode.GetBytes(str)))).ToString();
        }
    }

I need the same code but with PHP. Note that the md5() function has a different behavior.

thx

  • 写回答

1条回答 默认 最新

  • dongtu1357 2011-10-18 16:07
    关注

    I suspect the issue you are having is that the C# code returns the hash separated with dashes, because you are converting the hash to a GUID, and GUID.ToString() returns the string in what M$ call "registry format", which is the standard 8-4-4-4-12 string notation of a GUID.

    If this is the case, you could achieve the same result with this function:

    function md5_guid ($data, isFile = FALSE) {
      if ($isFile) {
        if (is_file($data) && is_readable($data)) {
          $hash = str_split(md5_file($data), 4);
        } else {
          return FALSE;
        }
      } else {
        $hash = str_split(md5($data), 4);
      }
      return "$hash[0]$hash[1]-$hash[2]-$hash[3]-$hash[4]-$hash[5]$hash[6]$hash[7]";
    }
    
    
    // Returns the MD5 hash of the string 'this is some data' in the format of a GUID
    echo md5_guid('this is some data');
    
    // Returns the MD5 hash of the file at 'somefile.txt' in the format of a GUID
    echo md5_guid('somefile.txt', TRUE);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100