13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with local SQL databases in AIR<br />

import com.adobe.air.crypto.EncryptionKeyG<strong>en</strong>erator;<br />

4 Before the point where the code creates the database or op<strong>en</strong>s a connection to it, add code to create an<br />

EncryptionKeyG<strong>en</strong>erator instance by calling the EncryptionKeyG<strong>en</strong>erator() constructor.<br />

var keyG<strong>en</strong>erator:EncryptionKeyG<strong>en</strong>erator = new EncryptionKeyG<strong>en</strong>erator();<br />

5 Obtain a password from the user:<br />

var password:String = passwordInput.text;<br />

if (!keyG<strong>en</strong>erator.validateStrongPassword(password))<br />

{<br />

// display an error message<br />

return;<br />

}<br />

The EncryptionKeyG<strong>en</strong>erator instance uses this password as the basis for the <strong>en</strong>cryption key (shown in the next<br />

step). The EncryptionKeyG<strong>en</strong>erator instance tests the password against certain strong password validation<br />

requirem<strong>en</strong>ts. If the validation fails, an error occurs. As the example code shows, you can check the password ahead<br />

of time by calling the EncryptionKeyG<strong>en</strong>erator object’s validateStrongPassword() method. That way you can<br />

determine whether the password meets the minimum requirem<strong>en</strong>ts for a strong password and avoid an error.<br />

6 G<strong>en</strong>erate the <strong>en</strong>cryption key from the password:<br />

var <strong>en</strong>cryptionKey:ByteArray = keyG<strong>en</strong>erator.getEncryptionKey(password);<br />

The getEncryptionKey() method g<strong>en</strong>erates and returns the <strong>en</strong>cryption key (a 16-byte ByteArray). You can th<strong>en</strong><br />

use the <strong>en</strong>cryption key to create your new <strong>en</strong>crypted database or op<strong>en</strong> your existing one.<br />

The getEncryptionKey() method has one required parameter, which is the password obtained in step 5.<br />

Note: To maintain the highest level of security and privacy for data, an application must require the user to <strong>en</strong>ter a<br />

password each time the application connects to the database. Do not store the user’s password or the database<br />

<strong>en</strong>cryption key directly. Doing so exposes security risks. Instead, as demonstrated in this example, an application<br />

should use the same technique to derive the <strong>en</strong>cryption key from the password both wh<strong>en</strong> creating the <strong>en</strong>crypted<br />

database and wh<strong>en</strong> connecting to it later.<br />

The getEncryptionKey() method also accepts a second (optional) parameter, the overrideSaltELSKey<br />

parameter. The EncryptionKeyG<strong>en</strong>erator creates a random value (known as a salt) that is used as part of the<br />

<strong>en</strong>cryption key. In order to be able to re-create the <strong>en</strong>cryption key, the salt value is stored in the Encrypted Local<br />

Store (ELS) of your AIR application. By default, the EncryptionKeyG<strong>en</strong>erator class uses a particular String as the<br />

ELS key. Although unlikely, it’s possible that the key can conflict with another key your application uses. Instead of<br />

using the default key, you might want to specify your own ELS key. In that case, specify a custom key by passing it<br />

as the second getEncryptionKey() parameter, as shown here:<br />

var customKey:String = "My custom ELS salt key";<br />

var <strong>en</strong>cryptionKey:ByteArray = keyG<strong>en</strong>erator.getEncryptionKey(password, customKey);<br />

7 Create or op<strong>en</strong> the database<br />

With an <strong>en</strong>cryption key returned by the getEncryptionKey() method, your code can create a new <strong>en</strong>crypted<br />

database or attempt to op<strong>en</strong> the existing <strong>en</strong>crypted database. In either case you use the SQLConnection class’s<br />

op<strong>en</strong>() or op<strong>en</strong>Async() method, as described in “Creating an <strong>en</strong>crypted database” on page 758 and “Connecting<br />

to an <strong>en</strong>crypted database” on page 758.<br />

In this example, the application is designed to op<strong>en</strong> the database in asynchronous execution mode. The code sets<br />

up the appropriate ev<strong>en</strong>t list<strong>en</strong>ers and calls the op<strong>en</strong>Async() method, passing the <strong>en</strong>cryption key as the final<br />

argum<strong>en</strong>t:<br />

Last updated 6/6/2012<br />

763

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!