The problem is Integer.toHexString(0xFF & messageDigest...
It doeasn't print leading 0s
try this one


try {
        MessageDigest digest = java.security.MessageDigest.getInstance
("MD5");
        digest.update(password.getBytes());
        byte passDigest[] = digest.digest();
        // Create Hex String
        StringBuffer hexPass = new StringBuffer();
        for (int i=0; i<passDigest.length; i++) {
                String h = Integer.toHexString(0xFF & passDigest[i]);
                while (h.length()<2) h = "0" + h;
                        hexPass.append(h);
        }
        md5Pass = hexPass.toString();
} catch (NoSuchAlgorithmException e) {
}


Found on google anyway



On Dec 7, 9:33 pm, niko20 <nikolatesl...@yahoo.com> wrote:
> Try this:
>
> public String md5(String s) {
>     try {
>         // Create MD5 Hash
>         MessageDigest digest = java.security.MessageDigest.getInstance
> ("MD5");
>         digest.update(s.getBytes());
>         byte messageDigest[] = digest.digest();
>
>         // Create Hex String
>         StringBuffer hexString = new StringBuffer();
>         for (int i=0; i<messageDigest.length; i++)
>             hexString.append(Integer.toHexString(0xFF & messageDigest
> [i]));
>         return hexString.toString();
>
>     } catch (NoSuchAlgorithmException e) {
>         e.printStackTrace();
>     }
>     return "";
>
> }
>
> Found it on google search
>
> On Dec 7, 2:32 pm, niko20 <nikolatesl...@yahoo.com> wrote:
>
> > Just a a side note, why do you care if it doesn't match? Unless you
> > are comparing MD5's to ones generated elsewhere, you are still getting
> > a unique hash. And as long as you compare to only hashes generated by
> > android it will always match....
>
> > -niko
>
> > On Dec 7, 1:14 pm, Justin Anderson <janderson....@gmail.com> wrote:
>
> > > Have you verified the website is correct by checking with a couple 
> > > different
> > > hash generation sites?
>
> > > On Dec 6, 2009 11:00 PM, "Mike M" <mike.mos...@gmail.com> wrote:
>
> > > Hey guys,
>
> > > I'm trying to get an MD5 Hash in my Android app, but it's not giving
> > > the correct hash.
>
> > > For example, in my app I have this:
>
> > > MD5_Hash hash = new MD5_Hash();
> > > myHash = hash.Hash("What I want to Hash");
>
> > > I get this from the above code:  306a801b82d299c8e965c2bf1d7fc18
>
> > > I go to a website that will calculate MD5 hashes, and enter "What I
> > > want to Hash", and get this:  306a8001b82d299c8e965c2bf1d7fc18.
>
> > > It does this every time; for some reason it strips one of the 0's.
> > > Has anyone noticed this?  Is there another / bettter way to get a hash
> > > ?
>
> > > Thanks
>
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "Android Beginners" group.
> > > To post to this group, send email to android-beginners@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > android-beginners+unsubscr...@googlegroups.com<android-beginners%2Bunsubscr
> > >  i...@googlegroups.com>
> > > For more options, visit this group 
> > > athttp://groups.google.com/group/android-beginners?hl=en
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to