topics
mobile
data mining
web
data visualization
distributed computing
blackberry, iphone, android
sentiment analysis, string matching
social networking, google app engine
processing
hadoop, aster data
code blackberry persistent store
Simple code snippet showing a static class that wraps around an encrypted persistent store. Acts as a key/val retrieval system, but persists to disk underneath.

This persistent store uses a code signing key to encrypt the data that is stored on the BlackBerry. Without this, any application (or user with access to the BlackBerry) could read the contents of your store. This is a problem if you wish to store a password on the device.

STORE PASSWORDS ON DISK
I wrote this when I wanted to store a password for a mobile app on the BlackBerry. If you are interested in storing things in plain-text, rather than encrypted, you don't need to use the CodeSigningKey class in this snippet.

ENCRYPTION
Also of note, if you use this with encryption, you will need to get a license from BlackBerry (because encryption is used). I've read that they cost $100, and can take up to 10 days to process. On top of this, you are subject to the license agreement that BB offers, and must sign your app using the cert that BlackBerry gives you.

I believe that you do not need the Certicom license that applies to BB apps that use certain encryption libraries.
import net.rim.device.api.crypto.SHA1Digest;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.CodeSigningKey;
import net.rim.device.api.system.ControlledAccess;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiEngine;
import net.rim.device.api.ui.component.Dialog;

public class PersistentStore {
	private CodeSigningKey codeSigningKey;

	public PersistentStore() {
		 codeSigningKey = CodeSigningKey.get("ACME");
	}

	public void setObject(String keyString, Object val) {
		 long key = getLongKey(keyString);

		 PersistentObject pObject = PersistentStore.getPersistentObject(key);

		 pObject.setContents(new ControlledAccess(val, codeSigningKey));
		 pObject.commit();
	}

	public Object getObject(String keyString) {
		 Object object;
		 long key = getLongKey(keyString);

		 PersistentObject pObject = PersistentStore.getPersistentObject(key);
		 object = pObject.getContents(codeSigningKey);

		 return object;
	}

	private long getLongKey(String key) {
		 SHA1Digest sha1Digest = new SHA1Digest();

		 sha1Digest.update(key.getBytes());

		 byte[] hashValBytes = sha1Digest.getDigest();

		 long hashValLong = 0;

		 for(int i = 0; i < 8; ++i) {
			  hashValLong |= ((long)(hashValBytes[i]) & 0x0FF) << (8*i);
		 }

		 return hashValLong;
	}
}
LINKS
BlackBerry Code Signing Tips
BlackBerry - Java Code Signing Keys
blog comments powered by Disqus