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
Highlighted
Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified
Accepted Solution

Converting a package String (like com.rim.sample) to long without the JDE

For those of us using linux and Eclipse, how are people doing this conversion?  Is it something simple I can just create a script to do?  Not sure what the algorithm is to do this without the JDE.

 

Patrick

 

Developer
Posts: 53
Registered: ‎12-11-2008
My Device: Not Specified

Re: Converting a package String (like com.rim.sample) to long without the JDE

[ Edited ]

Hope it will help:

 

Class StringUtilities

public static long stringHashToLong(String key)

Returns the first 8 bytes of a SHA1 digest as a long. The same result can be obtained interactively using the BlackBerry JDE by highlighting a string, right-clicking, and choosing "Convert '<string>' to long" from the context menu.

 

Parameters: key - The string to convert to a long. Returns: The long representation of key. Throws: NullPointerException - if the argument is null. Since: JDE 4.0.2
Message Edited by Artem on 12-17-2008 04:06 AM
Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: Converting a package String (like com.rim.sample) to long without the JDE

Check this code. It is an excerpt from knowledge base.

 

I just saved it for my needs. And do not remember the http link Smiley Happy

 

 

//String value to be converted String strValue = "com.samples.stringToHash"; SHA1Digest sha1Digest = new SHA1Digest(); //convert the string value to a byte array and pass it into the hash algorithm sha1Digest .update(strValue.getBytes() ); //retrieve a byte array containing the digest byte[] hashValBytes = sha1Digest.getDigest(); long hashValLong = 0; //create a long value from the byte array for( int i = 0; i < 8; i++ ) { hashValLong |= ((long)(hashValBytes[i]) & 0x0FF)<<(8*i); }

 

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Converting a package String (like com.rim.sample) to long without the JDE

You will find a version of this for NetBeans here:

http://www.jonathanhfisher.co.uk/b2/?p=10

 

I'm not sure if this might be better.

http://www.jonathanhfisher.co.uk/b2/?p=44

 

I would encourage you to wonder round jonathan's examples - some great stuff in there and some good thoughts in his blog too.

Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: Converting a package String (like com.rim.sample) to long without the JDE

Thanks guys, I'll look at all this, and eventually mark one as the solution I used.

 

Patrick

Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: Converting a package String (like com.rim.sample) to long without the JDE

[ Edited ]

I"d really llike something independent of RIM code... ie open source cyrpt libraries.

 

I'd tried this:

 

 

/*
Generate a Message Digest
*/
import java.security.*;
import javax.crypto.*;

public class MessageDigestExample {

public static void main (String[] args) throws Exception {
//
// Check args and get plaintext
if (args.length !=1) {
System.err.println("Usage: java MessageDigestExample text");
System.exit(1);
}

byte[] plainText = args[0].getBytes("UTF8");

// Get a message digest object using the SHA-1 algorithm
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");

// Print out the provider used
//System.out.println( "\n" + messageDigest.getProvider().getInfo() );

// Calculate the digest and print it out
messageDigest.update( plainText );

// Retrieve a byte array containing the digest
byte[] hashValBytes = messageDigest.digest();

long hashValLong = 0;

// Create a long value from the byte array
for( int i = 0; i < 8; i++ ) {
hashValLong |= ((long)(hashValBytes[i]) & 0x0FF)<<(8*i);
}

System.out.println( "\nDigest: " );
System.out.println( "0x" + hashValLong + "L" );

}
}

 

but I get a different value Long.  So, I'm going to look into this more now that I know it's supposed to be the SHA-1 hash.

 

Patrick

 

 

Message Edited by Berrysoft on 12-17-2008 08:10 AM
Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Converting a package String (like com.rim.sample) to long without the JDE

If you want to do this with a script as you suggestion and want the sha1, I guess the gpg tool may work,

 

$ echo "test string " | gpg --print-md sha1
CE06 B532 20B9 9F69 7723  48C0 6EBE 2C39 36DE BA51

 

and you can figure out which are the first 8 bytes.

Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: Converting a package String (like com.rim.sample) to long without the JDE

Ah, you have given me an important clue, and I forgot all about GPG!

 

I was wondering how they got 64 bits from 160!  So you say that they just take the first 8 bytes of the hash???  Most excellent, I shall try this next.  =)

 

Patrick

 

Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: Converting a package String (like com.rim.sample) to long without the JDE

[ Edited ]

Unfortunately, they are apparently using a different algorithm, as their code does not match the first 8 bytes of the SHA-1.

 

patrick@berrysoft:~$ echo "net.berrysoft.dib.Game" | gpg --print-md sha1
F630 7F29 47FD FC1E BDBA  7037 342C BDD9 DFE9 DD17

 

vs. theirs:  0xe4d0e6f8ce19aa3bL

 

for the string "net.berrysoft.dib.Game".

 

Anyone know what hash they are using????  Just would be best to match theirs.

 

Patrick

 

Message Edited by Berrysoft on 12-17-2008 11:41 AM
Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Converting a package String (like com.rim.sample) to long without the JDE

You might want to check the javadocs but I thought someone did that.

Could check for extra cr/lf using od -ax or something and see if the jvm

character encoding is important ( convert it to byte[] and optionally add a lf

and see what you have ).