title: When encrypting ID in Java and C# using SHA-1, C# returns value with an extra 0 in front sometimes
body: <p>I have a website that connects to another external website and passes ID numbers, the page I am working on and that generates the links is written in c#, whereas the target website is written in Java and I am experiences some mismatches</p>
  
  <p>I am trying to use the same approach in C# as the code in Java to encrypt the ID's, the code looks like this:</p>
  
  <pre><code>public static String shaHash(String input) 
  throws NoSuchAlgorithmException {
    if (input == null) {
      return null;
    }
    MessageDigest sha = MessageDigest.getInstance("SHA");
    try {
      sha.update(input.getBytes("UTF8"));
      BigInteger hash = new BigInteger(1, sha.digest());
      return hash.toString(16);
    } catch (UnsupportedEncodingException e) {}
    return null;
  }
  </code></pre>
  
  <p>And the C# code looks like </p>
  
  <pre><code>StringBuilder convertedId = new StringBuilder();
              var idToByte = Encoding.ASCII.GetBytes(id);
              using (SHA1 sha1 = SHA1.Create())
              {
                  var bytes = sha1.ComputeHash(idToByte);
                  for (int i = 0; i &lt; bytes.Length; i++)
                  {
                      convertedId.Append(bytes[i].ToString("x2"));
                  }
              }
              return convertedId.ToString();
  </code></pre>
  
  <p>But the return values are different as the C# code seems like it is adding an extra 0 at the beginning:</p>
  
  <p>Java: b83caf2f739209c42a8d02df0f6d4794f288703, and 
  C#: 0b83caf2f739209c42a8d02df0f6d4794f288703</p>
  
  <p>What  would it be the problem? I have tried to change the encoding  in the C# code for UTF8 but still doesn't work.</p>
  
  <p>Any ideas guys?</p>
  
  <p>Many thanks in advance</p>