title: Two different hash values even after entering same string in Android and .NET
body: <p>I have a .NET SOAP web serviec(.asmx) for SHA-1 hashing to get Base64 hash string.</p>
  <p>Here is my web service code:</p>
  <pre><code> public class Service1 : System.Web.Services.WebService
    {
      [WebMethod]
      public string HashCode(string str)
      {
          string rethash = &quot;&quot;;
          try
          {
  
              System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
              System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
              byte[] combined = encoder.GetBytes(str);
              hash.ComputeHash(combined);
              rethash = Convert.ToBase64String(hash.Hash);
          }
          catch (Exception ex)
          {
              string strerr = &quot;Error in HashCode : &quot; + ex.Message;
          }
          return rethash;
      }
  }
  </code></pre>
  <p>Here hash returned for input string &quot;abc&quot; is</p>
  <blockquote>
  <p>qZk+NkcGgWq6PiVxeFDCbJzQ2J0=</p>
  </blockquote>
  <p>Now, Android code again using SHA-1 and Base64 is:</p>
  <pre><code>public class PaswordencodingActivity extends Activity 
     {
   private static final String soap_action = &quot;http://tempuri.org/HashCode&quot;;
   private static final String method_name = &quot;HashCode&quot;;
   private static final String namespace2 = &quot;http://tempuri.org/&quot;;
   private static final String url2 = &quot;http://10.0.2.2/checkhash/Service1.asmx&quot;; 
      
  String password=&quot;abc&quot;;
  public final static int NO_OPTIONS = 0;
  String hash;
      String result2;
  
     @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
      final EditText pass=(EditText)findViewById(R.id.editText1);
      Button encode=(Button)findViewById(R.id.button1);
      
      encode.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v)  {
              // Perform action on click
              password=pass.getText().toString();
              if(password!=null){
              try {
      SHA1(password) ;
             } catch (NoSuchAlgorithmException e) {   
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block                                          
                 e.printStackTrace();
      } catch (IOException e)
                {
      // TODO Auto-generated catch block
          e.printStackTrace();
          }
              }
              else{
      Toast.makeText(PaswordencodingActivity.this, &quot;this is a negative onClick&quot;, Toast.LENGTH_LONG).show();
              }
                 
           }
      });
      
  }
  private static String convertToHex(byte[] bytes) throws java.io.IOException 
   {       
        StringBuffer sb = new StringBuffer();
        String hex=null;
          for (int i = 0; i &lt; bytes.length; i++) 
          {           
            hex=Base64.encodeToString(bytes, 0, bytes.length, NO_OPTIONS);
              if (hex.length() == 1) 
              {
                  sb.append('0');
              }
              sb.append(hex);
          }
          return sb.toString();
      }
  
  
   public  void SHA1(String text) 
        throws NoSuchAlgorithmException, IOException  
     { 
         MessageDigest md;
         md = MessageDigest.getInstance(&quot;SHA-1&quot;);
         byte[] sha1hash = new byte[40];
         md.update(text.getBytes(&quot;iso-8859-1&quot;), 0, text.length());
         sha1hash = md.digest();
         hash=convertToHex(sha1hash);
         System.out.println(&quot;hash value is&quot;+hash);
    try 
    {
          result2 = call3(hash);
  } catch (XmlPullParserException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
    
  if(result2.equalsIgnoreCase(hash))
  {
      System.out.println(&quot;success&quot;);
      
  }
  
  } 
   
  public String call3(String hash) throws XmlPullParserException
      {
          String b=&quot;&quot;;     
          SoapObject request = new SoapObject(namespace2, method_name);                       
          request.addProperty(&quot;str&quot;,hash);
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
       envelope.dotNet = true; 
       envelope.setOutputSoapObject(request);
          
       HttpTransportSE  android = new HttpTransportSE(url2);
           android.debug = true; 
    try 
   {
      
           android.call(soap_action, envelope);
           SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
           Log.i(&quot;myapp&quot;,result.toString());
           System.out.println(&quot; --- response ---- &quot; + result); 
           b=result.toString();
  
   } catch (SocketException ex) { 
      ex.printStackTrace(); 
      } catch (Exception e) { 
      e.printStackTrace(); 
      } 
          
      return b;   
      
  }
  </code></pre>
  <p>Android returned hash value is (from logcat for input String &quot;abc&quot;)</p>
  <blockquote>
  <p>11-24 13:56:35.179: INFO/myapp(578): 48a4yT8WPFCmkTxMSC9WaEtSxJI=</p>
  <p>11-24 13:56:35.179: INFO/System.out(578):  --- response ---- 48a4yT8WPFCmkTxMSC9WaEtSxJI=</p>
  </blockquote>
  <p>Can someone tell what's going wrong in my code? Am I double hashing somewhere</p>
  <p>Please help</p>
  <p>Thanks</p>