Saturday, October 25, 2014

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

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
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);
      mac.init(key);
      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) {
          hash.append('0');
        }
        hash.append(hex);
      }
      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()
.setConverter(converter)
               .setServer("www.abc.com")
               .build();


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

org.codehaus.jackson.JsonParseException: 
   Unexpected character ('n' (code 110)): was expecting double-quote to start field name
   at [Source: java.io.StringReader@7c87c24a; 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 compressed_filename.zip foldername

Unzip

sudo apt-get install unzip

unzip file.zip -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'
    ->     WITH GRANT OPTION;
mysql> CREATE USER 'monty'@'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'%'
    ->     WITH GRANT OPTION;

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        = xxx.xxx.xxx.xxx

RESTART MySQL

sudo service mysql restart

Install Java : https://www.digitalocean.com/community/tutorials/how-to-install-java-on-ubuntu-with-apt-get
----


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.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
 
<FrameLayout
 android:id="@+id/content_frame"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />
 
<ListView
 android:id="@+id/left_drawer"
 android:layout_width="400dp"
 android:layout_height="match_parent"
 android:layout_gravity="start"
 android:choiceMode="singleChoice"
 android:divider="@android:color/transparent"
 android:dividerHeight="0dp"
 android:background="#111"/>
 
<ListView
 android:id="@+id/right_drawer"
 android:layout_width="400dp"
 android:layout_height="match_parent"
 android:layout_gravity="end"
 android:choiceMode="singleChoice"
 android:divider="@android:color/transparent"
 android:dividerHeight="0dp"
 android:background="#111"/>
 
</android.support.v4.widget.DrawerLayout>


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.
1
mDrawerLayout.openDrawer(Gravity.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.
1
mDrawerLayout.closeDrawer(Gravity.END);
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 : http://onsoftwareandstuff.com/2013/06/19/android-navigation-drawer-pattern/