title: Blackberry MD5 authentication with HTTP Post
body: <p>I need to send to a server in POST an email (String) and a password (MD5 hash in byte[]).</p>
  
  <p>Below how I get my MD5 hash where "password" is a String (what the user enter):</p>
  
  <pre><code>byte[] passMD5 = Crypto.encodeStringMD5(password);
  </code></pre>
  
  <p>And the function:</p>
  
  <pre><code>public static byte[] encodeStringMD5(String s) throws Exception {
      byte[] bytes = s.getBytes();
      MD5Digest digest = new MD5Digest();
      digest.update(bytes, 0, bytes.length);
      int length = digest.getDigestLength();
      byte[] md5 = new byte[length];
      digest.getDigest(md5, 0, true);
      return md5;
  }
  </code></pre>
  
  <p>So "passMD5" should be an MD5 hash in bytes of my string value "password", right?</p>
  
  <p>Then I need to send the information through HTTP POST to an URL and read the result (XML). See below the rest of the code:</p>
  
  <pre><code>readURL(urlTemplate, email, passMD5);
  </code></pre>
  
  <p>Where urlTemplate is a String like "<a href="http://www.domain.com/myfile.aspx?action=login&amp;enc=1" rel="nofollow noreferrer">http://www.domain.com/myfile.aspx?action=login&amp;enc=1</a>", email a String and password the MD5 hash in bytes.</p>
  
  <p>The readURL below:</p>
  
  <pre><code>private void readURL(String url, String email, byte[] pass) throws IOException {
      HttpConnection conn = null;
      InputStream in = null;
      OutputStream os = null;
      byte dataBytes[];
  
      try {
          URLEncodedPostData data = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
          data.append("email", email);
          data.append("pass", pass.toString());
  
          dataBytes = data.getBytes();
  
          conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
          conn.setRequestMethod(HttpConnection.POST);
          conn.setRequestProperty("Content-Type", data.getContentType());
          conn.setRequestProperty("Content-Length", Integer.toString(dataBytes.length));
  
          os = conn.openOutputStream();
          os.write(dataBytes);
          os.flush();
          os.close();
  
          in = conn.openInputStream();
          verifyLogin(getLoginContent(in));
      } catch (IOException e) {
  
      } catch (IllegalArgumentException e) {
  
      } finally {
          ConnectionUtil.close(conn, in);
          ConnectionUtil.close(conn, os);
      }
  }
  </code></pre>
  
  <p>So right now the MD hash of the password in transformed to a String in order to be added to the data.append() function that only takes String parameters...
  I think because of this, I don't send the good MD5 hash and it makes a problem.</p>
  
  <p>On the server side in ASP.NET C#, I have this:</p>
  
  <pre><code>byte[] PasswordHash;
  
  if (enc == 0) {
      MD5 MD5Hasher = MD5.Create();
      PasswordHash = MD5Hasher.ComputeHash(Encoding.Unicode.GetBytes(Password));
  } else {
      PasswordHash = Encoding.Unicode.GetBytes(Password);
  }
  </code></pre>
  
  <p>So when I ask this URL "<a href="http://www.domain.com/myfile.aspx?action=login&amp;enc=0" rel="nofollow noreferrer">http://www.domain.com/myfile.aspx?action=login&amp;enc=0</a>" and give the password AS IS (so a String, not a byte[] and not MD5 hash) and do</p>
  
  <pre><code>data.append("pass", password);
  </code></pre>
  
  <p>then it works.</p>
  
  <p>I just have either a problem with creating my MD5 hash or with the HTTP POST or both...
  Please help me!</p>