blackberry string to long
if you want to create long value for PersistentStore or PersistentObject,
you need a tools to convert a string to (hex) long value.
the tool should be available at your JDE or Eclipse plugin.
but the question is, how to generate the long value?
as we know it is SHA1 digest value of a string.
Huge Thanks to jonathanhfisher [1]
SHA1Digest sha1Digest = new SHA1Digest(); sha1Digest.update(String2Convert.getBytes(), 0, String2Convert.length()); byte[] hashValBytes = new byte[sha1Digest.getDigestSize()]; sha1Digest.doFinal(hashValBytes, 0); long hashValLong = 0; for( int i = 0; i < 8; i++ ) { hashValLong |= ((long)(hashValBytes[i]) & 0x0FF)<<(8*i); } String hexString = "0x" + Long.toHexString(hashValLong) + "L";
the program above using (encryption) library from bouncycastle.
because I need to run that algorithm on my blackberry application,
which is already have SHA1 digest on its build library,
so I changed several parts of the code:
String sha = "my.blackberry.store.key"; SHA1Digest sha1Digest = new SHA1Digest(); sha1Digest.update(sha.getBytes(), 0, sha.length()); byte hashValBytes[] = new byte[sha1Digest.getDigestLength()]; hashValBytes = Arrays.copy(sha1Digest.getDigest()); long hashValLong = 0L; for(int i = 0; i < 8; i++) hashValLong |= ((long)hashValBytes[i] & 255L) << 8 * i; System.out.println("hexStr= 0x" + new String(longToHex(hashValLong))+"L");
you can user the longToHex converter from [2].
[1] http://www.blackberryforums.com/developer-forum/98447-convert-string-long.html
[2] http://snippets.dzone.com/posts/show/4593
Leave a comment