Tuesday, 1 October 2013

Generating PHP SHA1 Hash in Windows Phone and Windows 8 App

If you want to generate the PHP SHA1 (sometimes called hex digest format) in windows phone or windows 8 app us the following method.

private string HashString(string str)
{
 string dataHash = string.Empty;
 string hashType = "SHA1";

 try
 {
  HashAlgorithmProvider Algorithm = HashAlgorithmProvider.OpenAlgorithm(hashType);
  IBuffer vector = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
  IBuffer digest = Algorithm.HashData(vector);

  if (digest.Length != Algorithm.HashLength)
  {
   throw new System.InvalidOperationException("Failed");
  }
  dataHash = CryptographicBuffer.EncodeToHexString(digest); 
  return dataHash;
 }
 catch (Exception){}

 return null;
}