将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

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);