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
Developer
azdragon2
Posts: 38
Registered: ‎02-04-2009
My Device: 9530
Accepted Solution

sha1

Hello,

 

I'm trying to get the typical (default php type) sha1 of a String. Here's what I've got thus far:

 

        SHA1Digest sha1Digest = new SHA1Digest();
        String sha = "teststring"; //This string has been changed for secuirty reasons

        sha1Digest.update(sha.getBytes(), 0, sha.length());
        byte[] digest = sha1Digest.getDigest();
        sha = "";
        for (int a = 0; a < digest.length; a++)
        {
            sha += digest[a];
        }
        System.out.println("sha: " + sha);

 

 

but this is reporting an incorrect value of something like 69785171-7241-731860-91-81-11884-401-11041-56-40-88 for example,

 

can you guys help me out? Anyone know what I'm doing wrong? Or perhaps anyone know a better way of doing this?

Note: I've already got the security stuff in place using the IDE Edit>>Preferences>>Code Signing>>RIM Crypto API (marked true)

and all the keys have been acquired and setup

 

 

thanks for your help guys

Please use plain text.
Developer
azdragon2
Posts: 38
Registered: ‎02-04-2009
My Device: 9530

Re: sha1

bump

 

So does anyone know how to properly use the Sha1Digest class?

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: sha1

[ Edited ]

You can loop through the array and convert each byte to hex if you are looking for a hex string. Otherwise google converting byte[] to a long. I'm not sure what the standard is.

 

Integer.toHexString( 0xFF & bytes[i] )

Message Edited by mreed on 05-31-2009 05:46 PM
Please use plain text.
Developer
SergGr
Posts: 166
Registered: ‎05-07-2009
My Device: Not Specified

Re: sha1

[ Edited ]

I agree with mreed that you have to use Integer.toHexString( 0xFF & digest[a] ). Also I suggest you using getBytes in other way because your usage relies on default encoding in several aspects. For example in some encodings str.getBytes().length is not the same as str.length(). And there might be no proper code from some non-standard symbols. I suggest using UTF-8 (or the same encoding your PHP code uses).

 

private void doTest() throws UnsupportedEncodingException { SHA1Digest sha1Digest = new SHA1Digest(); String sha = "teststring"; byte[] inpData = sha.getBytes("UTF-8"); sha1Digest.update(inpData, 0, inpData.length); byte[] digest = sha1Digest.getDigest(); StringBuffer shaRes = new StringBuffer(40); //40 hex char is size of 160-bit SHA-1 result for (int a = 0; a < digest.length; a++) { String tmp = Integer.toHexString(0xff & digest[a]); if(tmp.length() == 1) //If hex value is "0X" then tmp is just one digit "X"

{ shaRes.append('0'); } shaRes.append(tmp); } System.out.println("shaRes: " + shaRes.toString()); }

 

This code gives me the same result as implementation at http://jssha.sourceforge.net/

You might wrap byte[] inpData = sha.getBytes("UTF-8"); into try/catch and ignore UnsupportedEncodingException because AFAIK UTF-8 is supported by all BB devices.

 

P.S. the code is updated according to mreed's fix for "0" in the result.

Message Edited by SergGr on 06-02-2009 03:51 PM

--------------------------------------------------------------------------------------------------------
If your issue is solved, set "Solution" mark at the relevant post.
Don't hesitate to Kudos people whose posts helped you.
Please use plain text.
Developer
azdragon2
Posts: 38
Registered: ‎02-04-2009
My Device: 9530

Re: sha1

hey mreed and sergGr,

 

thanks so much for your responses. This seems to be working correctly. I'm just running into one problem, the encrpytion seems to be ignoring the 0's. For example, this is what I am getting:

35d81f737e87d13a81ecf8cdb3668f8ff5bc7d5

 

and this is what it should be:

35d81f737e087d13a81ecf8cdb3668f8ff5bc7d5

 

any ideas on how to fix this?

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: sha1

If the length of Integer.toHexString() is only 1, prepend a 0.
Please use plain text.