title: How to Create Deterministic Guids
body: <p>In our application we are creating Xml files with an attribute that has a Guid value. This value needed to be consistent between file upgrades. So even if everything else in the file changes, the guid value for the attribute should remain the same.  </p>
  
  <p>One obvious solution was to create a static dictionary with the filename and the Guids to be used for them. Then whenever we generate the file, we look up the dictionary for the filename and use the corresponding guid. But this is not feasible because we might scale to 100's of files and didnt want to maintain big list of guids.</p>
  
  <p>So another approach was to make the Guid the same based on the path of the file. Since our file paths and application directory structure are unique, the Guid should be unique for that path. So each time we run an upgrade, the file gets the same guid based on its path. I found one cool way to generate such '<a href="http://geekswithblogs.net/EltonStoneman/archive/2008/06/26/generating-deterministic-guids.aspx" rel="noreferrer">Deterministic Guids</a>' (Thanks Elton Stoneman). It basically does this:</p>
  
  <pre><code>private Guid GetDeterministicGuid(string input) 
  
  { 
  
  //use MD5 hash to get a 16-byte hash of the string: 
  
  MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider(); 
  
  byte[] inputBytes = Encoding.Default.GetBytes(input); 
  
  byte[] hashBytes = provider.ComputeHash(inputBytes); 
  
  //generate a guid from the hash: 
  
  Guid hashGuid = new Guid(hashBytes); 
  
  return hashGuid; 
  
  } 
  </code></pre>
  
  <p>So given a string, the Guid will always be the same. </p>
  
  <p>Are there any other approaches or recommended ways to doing this? What are the pros or cons of that method?</p>