Friday, April 11, 2014

Dispatch queue, Operation queue difference

Both dispatch queue and operation queue in Grand Central Dispatch (GCD) are available for ios development for handling asynchronous operation, and each has its own features.

Operation queue is a higher level API than dispatch queue, it is based on objective-c, so it is ARC ready. It only supports concurrent operation, although with addDependency method, it can define the dependency between operations and make them happen sequentially. It also supports operation priority, so a new operation with high priority may run before other operation added earlier in the queue. An completion block can be used to do some work which is not part of operation task.

Dispatch queue is based on c API, it supports block for task. It supports both concurrent queue and serial queue (private dispatch queue), and Main dispatch queue. Dispatch queue does not provide method to get queued item count or cancel queued item. If it is required to cancel all queued item in serial queue, one option is adding a sync item with empty completion block, if is called, then we know all previous items have been processed.

Thursday, April 10, 2014

Client certificate authentication fails only on mac Safari browser

Safari browser on mac has a bug which causes client certificate authentication failure, although the same certificate works properly on other browsers (firefox, or chrome).  It seems the safari browser does not pick the user selected certificate to answer the server challenge if more than one client certificate is available, as it always work properly if there is one client certificate available.

To workaround the issue, in the keychain utility, move all certificates from "logon/my certificate" folder to a different folder, and only leave the one you use for client certificate authentication. Then the authentiation should work, but as long as a second client certificate is added, it will stop working. 

Monday, April 7, 2014

Encryption mode (ECB, CBC), padding, IV, salt, iteration count

For encryption mode selection, ECB mode should be avoided, the major issue with ECB is the encryption key will generate the identical ciphertext each time on the identical plaintext, so it does not hide data pattern. CBC or other encryption mode should be used if possible.

When using CBC, it uses IV (initialization vector) to alter the plaintext before encrypting with the encryption key, so it will generates a different ciphertext from the same key and plaintext. The IV is not secret, and it is required during encryption. Be sure to use a random IV each time when encrypting a blob.

As CBC works on a fixed block size, so the plaintext needs to be changed to be multiple of the block size. To do this, padding is used to append extra bytes at the end of plain text. Decryption should know what padding is used when encrypting, so the padding can be removed properly

Salt and iteration count are used to generate an encryption key from a user provided password. Salt is added to the password before it is used as encryption key. In this sense, it works in similar way as IV to break the pattern. Iteration count is how many times a encryption function should be called when convert a salted password to encryption key, its only purpose is increasing the delay for a brute fore attack. Salt and iteration count should be stored with the generated encryption key.



Thursday, March 20, 2014

Steps to create and run jasmin testing for cordova project

1. go to page http://jasmine.github.io/2.0/introduction.html and check the latest version of jasmin. Currently it is 2.0. (Download jasmin 2.0 from https://github.com/pivotal/jasmine/tree/master/dist)

2. create a regular cordova application for ios

3. copy the content of folder jasmine-standalone-2.0.0 into cordova platform's www folder

4. update /lib/jasmine-2.0.0/boot.js with below change:
/*comment out the below code as the test should be triggered by cordova on device ready 
  window.onload = function() {
    if (currentWindowOnload) {
      currentWindowOnload();
    }
    htmlReporter.initialize();
    env.execute();
  };
*/

//add  the below method to start jasmin cordova test
  window.jasminetest = function(){
    htmlReporter.initialize();
    env.execute();

  }

5. edit index.html created by cordova by adding the below code in head section. yourjscode is your javascript implementation. yourjasminecode is your jasmine testing code used to test yourjscode
        
   <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.0.0/jasmine_favicon.png">
  <link rel="stylesheet" type="text/css" href="lib/jasmine-2.0.0/jasmine.css">

  <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine.js"></script>
  <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine-html.js"></script>
  <script type="text/javascript" src="lib/jasmine-2.0.0/boot.js"></script>

  <!-- include source files here... -->
  <script type="text/javascript" src="src/yourjscode.js"></script>

  <!-- include spec files here... -->
  <script type="text/javascript" src="spec/yourjasmincode.js"></script>

  and also delete the below element to remove cordova ui element
<div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>

6. edit index.js by updating receivedEvent method as below

   // Update DOM on a Received Event
    receivedEvent: function(id) {
     /*   var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');
*/
        window.jasminetest();
        console.log('Received Event: ' + id);
    }


7. run your cordova project, the test should start automatically. If later, you want to test a different jasmin test suite, just replace yourjscode.js and yourjasminecode.js.


Tuesday, February 18, 2014

Convert certificate type on mac

It is easy to convert certificate type on MAC without having to use command line tool.
1. open keychain application on mac.
2. optional create a new keychain for separating this certificate from others by clicking File-New KeyChain
3. drag and drop the original certificate into the new keychain, or use File-Import menu and select the new keychain
4. export the imported certificate to other format by clicking File-Export menu

In addition, there is a good document about managing certificate on MAC.
https://developer.apple.com/library/ios/technotes/tn2326/_index.html#//apple_ref/doc/uid/DTS40014136

When testing the self signed certificate, the root CA must be installed on device via configuration profile. Just install the server certificate without root CA will not make the device to trust the server certificate.

To deploy the configuration profile from IIS to ios browser, in iis mime type configuration, add a new mime type, with extension of ".mobileconfig", and MIME type as "application/iphone-configuration"

Tuesday, February 11, 2014

Trust the self signed certificate on Chrome and Safaria on MAC

Safaria and Chrome on MAC use the keychain to handle self signed certificate, following the below steps to trust the self signed server certificate.

In the address bar, click the little lock with the X. This will bring up a small information screen. Click the button that says "Certificate Information."

Click and drag the image to your desktop. It looks like a little certificate.

Double-click it. This will bring up the Keychain Access utility. Enter your password to unlock it.

Be sure you add the certificate to the System keychain, not the login keychain. Click "Always Trust," even though this doesn't seem to do anything.

After it has been added, double-click it. You may have to authenticate again.

Expand the "Trust" section.

"When using this certificate," set to "Always Trust"

Tuesday, January 28, 2014

How to delete a remote git repository from github web page

In order to delete a git repository from github web page, first log on to github site and select the git repository, once inside the git repository you want to delete, select the settings button on top left of the page, then scroll to the bottom of the page called "Danger Zone", it has button to allow you to make it public or private, transfer the repository to others, or delete the repository.

By the way, if it fails to clone a git remote repository to local or push it to remote, you will need to set the SSH keys from settings page in github web site. You need first generate the private and public key and then load the public key from ~/ssh/id_rsa.pub to github web site's SSH key settings.