Skip to content

Instantly share code, notes, and snippets.

@copyninja
Created October 6, 2010 06:05
Show Gist options
  • Save copyninja/612903 to your computer and use it in GitHub Desktop.
Save copyninja/612903 to your computer and use it in GitHub Desktop.
package com.vasudev.msgdigest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MSGDigestGenerator extends Activity implements OnClickListener,
OnCheckedChangeListener {
private EditText stringHash = null;
private Button btnGenerate = null;
private TextView txtHash = null;
private RadioGroup algorithGroup = null;
private String hashAlgorithm = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
stringHash = (EditText) findViewById(R.id.string);
btnGenerate = (Button) findViewById(R.id.generate);
txtHash = (TextView) findViewById(R.id.hash);
algorithGroup = (RadioGroup) findViewById(R.id.algorithm);
btnGenerate.setOnClickListener(this);
algorithGroup.setOnCheckedChangeListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (hashAlgorithm == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Form In Complete!");
builder.setMessage("Please Select an Algorithm to generate Hash.");
builder.setPositiveButton("OK", null);
builder.create().show();
return;
}
try {
txtHash.setText((generateHash(stringHash.getText().toString(),hashAlgorithm)));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String generateHash(String string,String alg) throws NoSuchAlgorithmException {
// TODO Auto-generated method stub
MessageDigest algorithm = MessageDigest.getInstance(alg);
algorithm.update(string.getBytes());
byte[] digest = algorithm.digest();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < digest.length; i++) {
String h = Integer.toHexString(0xFF & digest[i]);
while (h.length() < 2)
h = "0" + h;
buffer.append(h);
}
return buffer.toString();
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.md5:
hashAlgorithm = "MD5";
break;
case R.id.sha1:
hashAlgorithm = "SHA-1";
break;
case R.id.sha256:
hashAlgorithm = "SHA-256";
break;
case R.id.sha512:
hashAlgorithm = "SHA-512";
break;
default:
break;
}
}
}
@copyninja
Copy link
Author

Sample for Generating cryptographic checksum in android

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment