Created
October 6, 2010 06:05
-
-
Save copyninja/612903 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample for Generating cryptographic checksum in android