Showing posts with label networking. Show all posts
Showing posts with label networking. Show all posts

Tuesday, December 18, 2012

Integrating SQLCipher with Cordova/PhoneGap sqlite plugin for iOS

NOTICE (June 2015): These instructions are completely out-of-date, the following Cordova plugin supports sqlcipher out-of-the-box: https://github.com/litehelpers/Cordova-sqlcipher-adapter

These directions are based on the sqlcipher iOS tutorial, with a few adaptations to integrate with a Cordova/PhoneGap project.

Start with a Cordova/PhoneGap iOS project (documented here for Cordova/PhoneGap 2.2.0).

Download & extract the OpenSSL source from www.openssl.org/source in a location that will be referenced later (I used OpenSSL 1.0.1c).

Inside the PhoneGap iOS project folder, clone the github sqlcipher & openssl-xcode projects using commands like the following:

$ git clone git@github.com:sqlcipher/sqlcipher.git
$ git clone git@github.com:sqlcipher/openssl-xcode.git

Download the OpenSSL source from www.openssl.org/source (I used OpenSSL 1.0.1c) & put the source tar.gz file in the openssl-xcode sub-folder.


Add the subproject references for sqlcipher & openssl-xcode. This can be achieved by:

  • selecting the top-level target at the top of the tree control;
  • press alt-command-a;
  • for openssl-xcode: select the openssl-xcode folder and then select openssl-xcode.xcodeproj &
  • repeat these steps for sqlcipher (select the sqlcipher folder & select sqlcipher.xcodeproj).
Configure build dependencies:

  • select the top-level target again;
  • click the Build Phases tab;
  • expand the Target Dependencies;
  • add crypto (click "+", select crypto, and press Add);
  • add sqlcipher

and add the libraries to link:

  • expand the Link Binary With Libraries;
  • click "+" to add
  • add both libcrypto.a & libsqlcipher.a

IMPORTANT: please make sure that no sqlite3 library is being added here.

CHECK POINT: at this point, it should be possible to build the project with the openssl & sqlcipher dependencies.

NOTE: a couple steps from the sqlcipher iOS tutorial were omitted since they should not be necessary. If the project does not build, here are some things to check & try:

  • Please double-check that all dependencies, including Target Dependencies & Link Binary With Libraries have been fulfilled.
  • If the sqlcipher/libcrypto does not build, please read through README.md under openssl-xcode. You may have to configure the location of the OpenSSL sources if nothing else works.
  • The SQLITE_HAS_CODEC C flag, which is necessary to build sqlcipher with its cipher capabilities, should be defined within the sqlcipher subproject. It should not be necessary to define this flag within the top-level application project, however it is noted here just in case.

It should now be possible to add SQLitePlugin.[hm] to the project Plugins folder & build again. The following patch to SQLitePlugin.m should enable the plugin to use database encryption:


diff --git a/iOS/Plugins/SQLitePlugin.m b/iOS/Plugins/SQLitePlugin.m
index 871bd89..3d62208 100644
--- a/iOS/Plugins/SQLitePlugin.m
+++ b/iOS/Plugins/SQLitePlugin.m
@@ -79,6 +79,9 @@
         return;
     }

+    const char *key = [[options objectForKey:@"key"] UTF8String];
+    sqlite3_key(db, key, strlen(key));
+
     dbPointer = [NSValue valueWithPointer:db];
     [openDBs setObject:dbPointer forKey: dbname];
     [self respond:callback withString: @"{ message: 'Database opened' }" withType:@"success"];



Add SQLitePlugin to Cordova.plist resources, add SQLitePlugin.js to the www folder, and include SQLitePlugin.js in index.html. Try a small test program, like the one from the brodyspark/ PhoneGap-SQLitePlugin-iOS homepage, but open the database with a statement like this:


var db = window.sqlitePlugin.openDatabase({name: "DB",
    key: "secret1"});


If you try the program again but with a different key, you should see a db error in the console log.

In the future, I would like to provide a script or boilerplate to make it easy to create Cordova/PhoneGap projects with encrypted storage working from the beginning.

Thursday, November 29, 2012

Creating my first LinkedIn group

I was thinking about what to blog next until I ran into some discussion on LinkedIn about why it is so hard to find good Javascript developers. I suggested CoffeeScript, which had really proven itself over the past few years, and got some nasty reactions which led me to some ideas. While altJS.org already has an active mailing list, I did not see such a group and decided to try starting one.

My experience starting these kinds of things so far is relatively limited to things like starting blogs on Blogger, starting a Google mailing list (mostly to capture some important correspondence that I can link to), and registering some dot-com names so this was still a new adventure for me.

It turned out to be quite simple to get started. I just went on the LinkedIn page under Groups and pressed "Create a Group". When I saw the form I was happy that it would be so easy to create the group. For the name, I thought of "Alt2JS" that could very quickly describe the purpose of the group. There was an option to add a logo and I decided to consider a brave attempt to add a decent one.

I have already found the Google Drive to provide some very good online tools and wondered if there would be a good online logo application. I tried searching "make logo online", got a few nice results, and decided to try Logo Ease. I found it was very easy to understand and use to create a simple but very decent logo and was able to upload it very quickly. (I will describe my experiences in a future posting.)

When looking at the form, I next thought of registering a domain for alt2js just in case it may be handy in the future. I was very happy that alt2js.com was still not yet registered so I went ahead and registered alt2js.com.

So now I filled in the Group Name, selected the Group Type (Professional Group), Summary, and Description. The one critique I had was that the Access option was set to "Request to Join" by default. I have seen a number of groups that had transitioned from closed to public and now I have come to understand why. So I selected Auto-Join and was happy that all the access sub-options (display in groups directory, allow members to display logos, and allow members to invite others) were now enabled by default.

To finish I pressed the button to "Create an Open Group", got a nice form to invite some of my connections to the group, and now have a nice new LinkedIn Alt2JS group.