Sunday, April 10, 2016

Finding difference between two dates in JS

"Finding difference between two dates" is not a new topic in JavaScript world, however with this post, i wish to go one step ahead by providing this ability as an extension of Date object itself and in a configurable manner.
In this code we extend Date object prototype with a new method named "difference". This method take another date (Created using "new Date()") and "unit" for comparison result i.e in what units do you want to know the difference e.g "seconds","minutes","hours","days" or "months"


Wednesday, December 3, 2014

How to fix: Smart watch 2 won't install apps on MIUI


Many people trying to connect Sony smartwatch to phones with MIUI or Xiaomi Phones have faced issue where even after downloading and installing apps for smartphone from Google play store, the apps are not seen on the watch for use.

In this post i will provide simple steps for solving this issue. Please note that this fix requires rooting your phone.

Step 1: Root your phone.

Step 2: Install Lucky patcher (http://lucky-patcher.netbew.com/)

Step 3: Install all the smartphone apps you need from Google Play Store.

Step 4: Select the specific app from lucky patcher's list. Click on tools and select "Move the app to "system/app""

Step 5: Reboot the phone

After phone reboot you should see the installed app on phone. :)

Monday, August 4, 2014

Can't download App's from Google play via 3g network

This is a short post describing a solution for a very common problem on android phones. One of the common problem with android is that, Apps cannot be downloaded over 3G connection. This problem is not only there with the play store, but with other applications dealing with media. The solution for this is very simple!
Go into Settings/...More settings/Mobile networks/Access Point Names. Choose your Access Point name and empty Proxy and Port. And that should fix your problem!

Wednesday, April 9, 2014

ENHANCING CAESAR CIPHER TECHNIQUE

A Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and widely known encryption techniques. In this type of substitution cipher each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.


As with all single alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communication security.

Example

The transformation can be represented by aligning two alphabets; the cipher alphabet is the plain alphabet rotated left or right by some number of positions. For instance, here is a Caesar cipher using a left rotation of three places, equivalent to a right shift of 23 (the shift parameter is used as the key):
Plain:    ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher:   XYZABCDEFGHIJKLMNOPQRSTUVW

When encrypting, a person looks up each letter of the message in the "plain" line and writes down the corresponding letter in the "cipher" line. Deciphering is done in reverse, with a right shift of 3.
Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
Plaintext:  the quick brown fox jumps over the lazy dog

Breaking the Cipher

The Caesar cipher can be easily broken even in a cipher text-only scenario. Two situations can be considered:

  1. An attacker knows (or guesses) that some sort of simple substitution cipher has been used, but not specifically that it is a Caesar scheme;
  2. An attacker knows that a Caesar cipher is in use, but does not know the shift value.
In the first case, the cipher can be broken using the same techniques as for a general simple substitution cipher, such as frequency analysis or pattern words. While solving, it is likely that an attacker will quickly notice the regularity in the solution and deduce that a Caesar cipher is the specific algorithm employed.

In the second instance, breaking the scheme is even more straightforward. Since there are only a limited number of possible shifts (26 in English), they can each be tested in turn in a brute force attack. One way to do this is to write out a snippet of the cipher text in a table of all possible shifts — a technique sometimes known as "completing the plain component".

Decryption
shift
Candidate plaintext
0
exxegoexsrgi
1
dwwdfndwrqfh
2
cvvcemcvqpeg
3
buubdlbupodf
4
attackatonce
5
zsszbjzsnmbd
6
yrryaiyrmlac
...
23
haahjrhavujl
24
gzzgiqgzutik
25
fyyfhpfytshj



















The example given is for the cipher text "EXXEGOEXSRGI"; the plaintext is instantly recognizable by eye at a shift of four. Another way of viewing this method is that, under each letter of the cipher text, the entire alphabet is written out in reverse starting at that letter. This attack can be accelerated using a set of strips prepared with the alphabet written down them in reverse order. The strips are then aligned to form the cipher text along one row, and the plaintext should appear in one of the other rows.
Another brute force approach is to match up the frequency distribution of the letters. By graphing the frequencies of letters in the cipher text, and by knowing the expected distribution of those letters in the original language of the plaintext, a human can easily spot the value of the shift by looking at the displacement of particular features of the graph. This is known as frequency analysis.

Enhancing the Cipher

Based on the explanation provided we see that main problem with Caesar cipher is predictability. Two main issues that cause this predictability are:

  1.  Fixed index for shifting characters
  2.  Knowledge about character at a given index.
We can enhance the strength of the cipher by overcoming these 2 issues. Let us look into these 2 issues and how we can overcome them:

Fixed index for shifting characters

As you read earlier in Caesar cipher each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. Thus if attacker manages to figure out shift index he can easily break the cipher text. We can overcome this making the shift index variable. Consider the example shown in the table above. Instead of using a fixed index of 4, the index could be length of the string I.e 12. Using such variable index will make the cipher text difficult to predict. In addition to this advantage, two similar word/phrase with different lengths would have different cipher texts. For example attackatonce and attackonce will have no pattern in common even though the word “attack” and “once” are being repeated. This happens because the string length is different thus the shift index will be different.

Knowledge about character at a given index

In our previous example we also observed while shifting the alphabets, the alphabet at new position occurs as per alphabetical order of A-Z. Thus if attacker manages to figure out the shift index he can easily find out all the characters in the given cipher text. Besides making the shift index dynamic we can also shuffle the alphabetical order by which the alphabets won’t be substituted as per the default alphabetic order. For example on a shift index of 4 if we were suppose to substitute character “a”, by alphabetical order the new character would be “d”. However if we do not follow alphabetical order and replace the characters basis of shuffled set of characters it will make it difficult for attacker to derive the plain text. For this we need to have a shuffled set of alphabets.

Implementation


Enhanced Caesar cipher can be implemented in following manner:

  1. Determine the shift index on the basis of string length.
  2. Determine the position of character to be replaced in the shuffled string.
  3. Add the shift index to the position to determine the position of new character.
  4. Determine the character at new position. If the new position is more than the length of the shuffled set then loop again after the loop index reaches the length of shuffled set.
For example
The plain text to be ciphered is “america”. The shuffled subset is “adwxyzefijklmnoghpqrstbcuv”. The cipher for this will be “frnagyf”. In this example “a” is substituted with “e”. For this substitution we 1st determine the position of “a” in shuffled subset. This is 1. Now we need to determine the shift index, which is length of string i.e. 7. Thus with a right shift of 7,”a” gives “f” as per the shuffled set. Similar substitution process will be applied to each character in the given word.

The Outcome

An enhanced cipher technique derived from a simple substitution cipher is much effective compared to its original version.

Wednesday, November 27, 2013

Using jshybugger to debug HTML, CSS and javascript on native Android browser


Debugging web applications on android device as been a challenging issue. This becomes further challenging when the debugging is to be done on native browser of Android.
The debugging tools provided by native android browser are insufficient to perform detailed analysis of the problem. (In case you're not aware about these tools and wish to check em out type "about:debug" in your native browser's address bar and then check your settings menu for debug options).
While looking for solution of one of my problems on native android browser i came across this tool which does the job brilliantly! Its name is jsHybugger (https://www.jshybugger.com/#!/).

When you use jsHybugger App, jsHybugger runs on your device and provides two things:
  • a proxy between the native Android browser and the web server where your mobile web pages are hosted
  • a communication endpoint for the debugging frontend on your development machine
Using this very simple and straight forward. Steps for it are as follow:

  1. Download and install the APK file for jsHybugger App, either through a direct download on your device or from your development machine using ADB.
  2. Start jsHybugger App on your device and adjust the configuration (see parameters below).
  3. Open the native Android browser via Open browser. The browser automatically navigates to the specified URL.
  4. Connect to jsHybugger.
  5. Set port forwarding to enabled in ADB interface. For more information on how to use ADB extension please refer to my earlier blog post (http://anirudhprabhu.blogspot.in/2013/08/debugging-website-or-web-application.html)

Parameters
  • Remote host: IP address or hostname of the remote web server
  • Remote port: HTTP port of the remote web server (usually 80)
  • URI: URI of the start page, e.g. /index.html
Control
  • Stop service: Stop service on device
  • Start service: Start service on device
  • Open browser: Start the Android browser with the configured URL
  • Clear cache: Clear jsHybugger cache
  • Log viewer: Display HTTP access log

Happy Debugging!!!

Sunday, October 6, 2013

Basics of Jasmine Framework

Hey folks today we will look into some basics of jasmine. Jasmine is a behavior-driven testing framework for JavaScript programming language. It’s a bunch of tools that you can use to test JavaScript code. you can test your code against specifications that you write. If your code should work in a certain way, Jasmine helps you express
that intention in code.




Getting Started
1. Start by downloading the latest standalone release of Jasmine (https://github.com/pivotal/jasmine/downloads). Unzip it.

2. In your html include scripts in following order:

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

3. Now include you spec file. This is a normal js file in which your test cases are written. So in our case it will be

<!-- Spec files-->
<script type="text/javascript" src="spec/calculatorSpec.js"></script>

Please not that always include spec file before actual code file.

4. Now we include the code file

<!-- Src files-->
<script type="text/javascript" src="src/calculator.js"></script>

Which will contain our logic

Now here are few things you need to know before we look into the code of spec file

* A describe("some name"...) is called a suite. This name of suite would usually be a component of your application.
* Inside of suite is the it() block. This is called specification. Its is a Js function that says what a small  peice of your component must do.

* Matchers: This basically takes the arguement to the expect function and checks to see if it satisfies some criterion in the matcher. for example: toEqual()

Now lets take a look at the spec

You can view the example here: http://designmantra.x10.mx/jasmine/

We are carrying out five tests here:
1. Storing current value
2. Addition yields correct results. 
3. Substraction yields correct results.
4. Multiplication yields correct results.
5. Division yields correct results.

Here is the code for spec file:

describe("Calculator",function(){
it("Should have current value stored",function(){
expect(calculator.currentValue).toBeDefined();
});
beforeEach(function(){
calculator.currentValue = 0
});
describe("Addition module",function(){
it("Should add two numbers",function(){
expect(calculator.add(5)).toEqual(5);
expect(calculator.add(5)).toEqual(10);
})
});
describe("Substraction module",function(){
it("Should substract two numbers",function(){
expect(calculator.substract(5)).toEqual(-5);
})
})
describe("Multiplication module",function(){
it("Should multiply two numbers",function(){
calculator.currentValue = 5;
expect(calculator.multiply(5)).toEqual(25);
})
})
describe("Division module",function(){
it("Should divide two numbers",function(){
expect(calculator.divide(5)).toEqual(5);
})
})
})

Here is the corresponding code for calculator.js code

var calculator = {
currentValue : 0,
add: function(num){
return this.currentValue += num;
},
substract:function(num){
return this.currentValue -= num;
},
multiply:function(num){
return this.currentValue *= num;
},
divide:function(num){
return 25/num;
}
}

Hope this article was useful. For any queries write me at prabhu.anirudh@live.in.

Saturday, August 24, 2013

Debugging a website or web application using Chrome and Android device made easy

In this post we will look into simple steps of how to debug your website or you web application on chrome desktop while you are viewing it on chrome on android. Here is what you need:

1. An android device with chrome installed on it.
2. Device drivers for that android device (must have as this will enable USB Debugging)
3. ADB extension (https://chrome.google.com/webstore/detail/adb/dpngiggdglpdnjdoaefidgiigpemgage)

Here is how you perform debugging:

Step 1. Install ADB extension in your chrome on desktop
Step 2. Install device drivers for your android device
Step 3. Enable USB debugging:
Step 4: Enable USB debugging within chrome for android as shown below:

Step 5. In chrome on desktop click on Start ADB
Step 6: Click on view inspection target to start debugging