Applying MD5 Algorithm In Android

Hello developers, have you ever given a thought about securing your content when it is being used in communication? If yes then this blog might help you in achieving so.

MD5 stands for Message Digest(5 denotes the series), which is a hashing algorithm used for generating the fingerprint of the content. It was designed in 1991 by Ronald Rivest at MIT.

MD5 algorithm is a one way hashing process which means you cannot generate original text from the hash code.It always produces the same output for the similar input, and this property is used for checking the data integrity. You can compare the two hash codes at destination(one which is received from server and one which is already present at destination) and check whether the files contains the same content or not.

MD5 is collision resistant as no two string content can ever have the same hash code.It generates 16 byte hash code which is expressed as 32 digit Hexadecimal number. Generally MD5 is used to generate fingerprints of small strings such as passwords.

So after reading the theory lets move on to some coding which I guess you all be waiting for:

This is the layout for my Activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/margin"
    tools:context="com.mfsi.messagedigest.MainActivity$PlaceholderFragment" >

    <EditText
        android:id="@+id/stringdata"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
         />

    <Button
        android:id="@+id/convert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/stringdata"
        android:padding="5dp"
        android:text="@string/convert_button" />
    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/stringdata"
        android:padding="5dp"
        android:layout_toRightOf="@+id/convert"
        android:text="@string/reset_button" />

	<TextView
        android:id="@+id/encryptdata"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/convert" />

</RelativeLayout>

This is my Activity.

public class MainActivity extends ActionBarActivity implements OnClickListener {

	private EditText mDataEditText;
	private TextView mMd5Text;
	private Button mConvertButton;
	private Button mResetButton;

	//String to be encoded
	private String mEncryptString = null;
	//Encoded string
	private String mMD5EncryptCode = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//Initialization
		mDataEditText = (EditText) findViewById(R.id.stringdata);
		mMd5Text = (TextView) findViewById(R.id.encryptdata);
		mConvertButton = (Button) findViewById(R.id.convert);
		mResetButton = (Button) findViewById(R.id.reset);

		mConvertButton.setOnClickListener(this);
		mResetButton.setOnClickListener(this);
	}
	/*
	 * Creating the hash code and setting the code into the TextView
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View iView) {
		switch (iView.getId()) {
		case R.id.convert:
			//Fetching the data from Edittext to String
			mEncryptString = mDataEditText.getText().toString().trim();

			MessageDigest messageDigest =null;
			try {
				messageDigest = MessageDigest.getInstance("MD5");
			} catch (NoSuchAlgorithmException e) {
				Toast.makeText(getApplicationContext(), "No such algorithm found", Toast.LENGTH_SHORT).show();
			}

			messageDigest.update(mEncryptString.getBytes());
			mMD5EncryptCode = new BigInteger(1,messageDigest.digest()).toString(16);

			mMd5Text.setText("Hash Code is:"+mMD5EncryptCode);
			break;
		case R.id.reset:
			mDataEditText.setText("");
			mMd5Text.setText("");
			break;
		}
	}
}

Below is the screenshot of the result achieved.

code

I hope you find it helpful. Thanks for reading.

Written By: – Ankit Khare, Software Developer, Mindfire Solutions

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s