Saturday, October 25, 2014

Generating HMAC MD5/SHA1/SHA256/SHA512 etc in Java, Android

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class HMAC {
  public static void main(String[] args) throws Exception {
    System.out.println(hmacDigest("The quick brown fox jumps over the lazy dog", "key", "HmacSHA1"));
  public static String hmacDigest(String msg, String keyString, String algo) {
    String digest = null;
    try {
      SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), algo);
      Mac mac = Mac.getInstance(algo);
      byte[] bytes = mac.doFinal(msg.getBytes("ASCII"));
      StringBuffer hash = new StringBuffer();
      for (int i = 0; i < bytes.length; i++) {
        String hex = Integer.toHexString(0xFF & bytes[i]);
        if (hex.length() == 1) {
      digest = hash.toString();
    } catch (UnsupportedEncodingException e) {
    } catch (InvalidKeyException e) {
    } catch (NoSuchAlgorithmException e) {
    return digest;

Sunday, October 12, 2014

Use Retrofit With Jackson

Retrofit uses Google gson by default. but in some cases we may want to change the convertor to something else. In this case I wanted to use Jackson convertor in Retrofit.

First of all we need to create JacksonCovnerter, which is available under Retrofit examples.
I just used the same java class JacksonConverter

Then simply need to provide convertor while building rest adapter

JacksonConverter converter = new JacksonConverter(new ObjectMapper());

RestAdapter restAdapter = new RestAdapter.Builder()

Thats all, once you set the converter, Retrofit will use JacksonConverter instead of default google gson convertor. Make-sure you have added required Jackson dependencies to your project (Jackson core, Annotations etc. )


One small issue I faced, sometimes my REST API Json response doesn't contain double quotes for field names. Sometimes I get following issue

   Unexpected character ('n' (code 110)): was expecting double-quote to start field name
   at [Source:; line: 1, column: 3]
Solution is to add following line on Jackson Convertor.

mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

Sunday, August 10, 2014

Useful Linux Commands

Install ZIP:

sudo apt-get install zip

create ZIP file

zip -r foldername


sudo apt-get install unzip

unzip -d destination_folder

Enable remote desktop

sudo apt-get install xrdp

Install MYSQL

sudo apt-get install mysql-server

Login to mysql server

mysql -h localhost -u myname -pmypass mydb

Allow MYSQL to connect from outside: stackoverflow-post

mysql> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
mysql> CREATE USER 'monty'@'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'%'

stackoverflow post:

To expose MySQL to anything other than localhost you will have to have the following line uncommented in /etc/mysql/my.cnf and assigned to your computers IP address and not loopback
#Replace xxx with your IP Address 
bind-address        =


sudo service mysql restart

Install Java :

Friday, August 8, 2014

Android Navigation Drawer from Left and Right

On to the point now.. The implementation of Navigation Drawer is fairly straight-forward and documented very nicely here. Here I’d mention an approach using which you could use navigation drawer on both left and right side in the same view. This is useful for following reasons:
1. You want to use the left side drawer for Navigation purpose and right side drawer for things like ‘Filter’, ‘Notifications’ etc.
2. You’d like a similar feel for overlays that appear on the app. Using it on both sides, gives a very smooth flow and retains the same behaviour (peeking, smooth sliding and edge touch).
On to the implementation:
This would be an extension to the application provided as example in the documentation section. So, first get the example app running and then try this method.
Add another ListView for the right side drawer, and instead of “start” use the layout_gravity as “end”. So your final layout file would look like following
 android:layout_height="match_parent" />

In the activity, get a handle to both the list views. (By the way, they could be any other views as well..). Now whenever you need to open the right side menu, just call the openDrawer method on the drawer layout, providing gravity as END.
That’s it, now you could open the navigation drawer on both sides.
Now, you’d like to close one drawer on opening the other drawer.. Use the closeDrawer method at appropriate location to close the drawer.
That’s pretty much it. The drawers close and open in a very smooth manner and provide a pleasant experience to the user.
Original post referenced :