Feb 24

I’ve recently written an AIR time tracking application that uses the Harvest & Coopapp APIs. The code and AIR application can be found on the harvestcoop project page on Google Code.

Harvest does provide an iPhone application and also a Harvest Widget for Mac OS X & MS Vista. I decided to create an AIR application so that users on operating systems that don’t have widget support can do their time tracking from the comfort of their own desktops.

The harvestcoop AIR application currently works with Harvest accounts that have widget support.

Feb 19

I’ll be presenting on the PureMVC framework next week (February 26) at the Vancouver Flash Platform February Meetup.

Here are the links to the presentation’s slides, sample application and code.

Feb 10

The Adobe AIR runtime includes a SQLite database so that you can create data driven applications.

The following are the default locations for the SQLite database:

Mac OS X:

/Users/<userName>/Library/Preferences/<applicationName>/Local Store/<databaseFileName>.db

Windows:

C:\Documents and Settings\<userName>\Application Data\<applicationName>\Local Store\<databaseFileName>.db

Adobe AIR 1.5 introduces a feature to encrypt the SQLite database so that you can secure the contents of database. This is could come in handy if you are writing an application that stores information such as customer contact data.

The Open method and the OpenAsync method of the SQLConnection class include an encryptionKey parameter; this parameter tells AIR to encrypt the database file.

The code sample below shows you how to create / open an encrypted SQLite database. Links to an example application & code can be found at the end of this article.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import com.digitaldogbyte.util.Encryption;
 
private var dbConn:SQLConnection;
private var dbFile:File;
private var dbKey:ByteArray;
 
private function init():void
{
	dbKey = Encryption.retrieveKey();
 
	dbFile = File.applicationStorageDirectory.resolvePath("EncryptionExample.db");
	dbFile.createDirectory();
 
	dbConn = new SQLConnection();
	dbConn.addEventListener(SQLEvent.OPEN, handleDbOpen);
	dbConn.addEventListener(SQLErrorEvent.ERROR, handleDbError);
 
	// by passing in the dbKey variable as the encryptionKey parameter we are telling AIR to encrypt the SQLite database
	dbConn.openAsync(dbFile, SQLMode.CREATE, null, false, 1024, dbKey);
}

Let’s take a look at the Encryption class to see how the dbKey ByteArray is created. The dbKey value is assigned the return value of the retrieveKey method from the Encryption class. The retrieveKey method checks to see if a key already exists in the application’s EncryptedLocalStore. If the key already exists the retrieveKey method will return the existing key, however if the key does not exist; the generateKey method is called to create a key.

The generateKey method uses the as3crypto library’s Random type to create a key. This key is required to read & write to the encrypted database.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.digitaldogbyte.util
{
 
	import com.hurlant.crypto.Crypto;
	import com.hurlant.crypto.prng.Random;
	import com.hurlant.crypto.symmetric.AESKey;
	import com.hurlant.crypto.symmetric.ECBMode;
	import com.hurlant.crypto.symmetric.ICipher;
	import com.hurlant.util.Hex;
 
	import flash.data.EncryptedLocalStore;
	import flash.utils.ByteArray;
 
	public class Encryption
	{
 
		private static var _key:ByteArray;
 
		private static function generateKey():void
		{
			// create key - write to encrypted local store
			_key = new ByteArray();
			var random:Random = new Random();
			random.nextBytes(_key, 16);
			flash.data.EncryptedLocalStore.setItem("key",_key);
		}
 
		public static function retrieveKey():ByteArray
		{
			// try to retrieve key from encrypted local store
			_key = flash.data.EncryptedLocalStore.getItem("key");
			// if no key found in encrypted local store - create a key
			if (_key == null)
			{
				generateKey();
			}
			return _key;
		}
 
	}
}

After you’ve created the encrypted database you can navigate to the database file and try to open it. The database file will not open in your database browser application since the actual database file has been encrypted.

SQLite open error

Here are the download links to the example application & code.