Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
joschika77
Posts: 24
Registered: ‎09-12-2008
My Device: Not Specified

String to MD5

Hello, i would convert with pleasure a string into MD5. I have already tried something.

 

 

byte[] digestData = new byte[16]; MD5Digest digest = new MD5Digest(); //DigestOutputStream digestStream = new DigestOutputStream( digest, null ); ByteArrayOutputStream digestStream = new ByteArrayOutputStream(); digestStream.write(myPassword.getBytes(),0,myPassword.getBytes().length); digest.getDigest( digestData, 0 ); String test = new String(digestData);

 

I need the Hash value in a string. Who can help me?

 

 

 

Please use plain text.
New Developer
joschika77
Posts: 24
Registered: ‎09-12-2008
My Device: Not Specified

Re: String to MD5

Please use plain text.
Developer
spring_jitu
Posts: 126
Registered: ‎06-08-2009
My Device: Not Specified

Re: String to MD5

Use this If any one needed futher::

 

suppose username = xyz;

 

 

 private static String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9))
                    buf.append((char) ('0' + halfbyte));
                else
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        }
        return buf.toString();
    }
   
    private String generateSessionVal(String username) throws Exception
    {
        byte[] bytes = username.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 convertToHex(md5);
   }

Please use plain text.
New Developer
science
Posts: 1
Registered: ‎12-31-2009
My Device: Not Specified

Re: String to MD5

Thank you. It worked for me.

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: String to MD5

Can I suggest you mark this as Solved, and being bold, I think your responders deserve some kudos too.  But that is just me. 

Please use plain text.