import java.util.Scanner;
import java.security.MessageDigest;
import javax.swing.JOptionPane;
import java.io.UnsupportedEncodingException;
/**
* Application to practice creating a message digest with specifying algorithm
* @author Zoltan
* @version 1.0.0
*
*/
public class Driver {
/**
* Entry point to the application
* @param args String array that is not used in this code
*/
public static void main(String[] args) {
//variable declarations
byte[] messageBytes;
String password;
MessageDigest mDigest;
byte[] theHash;
//Let user specify the clear text password by entering a string
password = JOptionPane.showInputDialog("Please enter your password");
JOptionPane.showMessageDialog(null, "Your password was: "+password);
//Convert the entered password string to a byte array in UTF-8 encoding
//The encoding should never throw an exception in this case, but it is required for this method.
try{
messageBytes= password.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e) {
throw new AssertionError("UTF-8 is unknown");
}
//Generate the message digest for the clear text of byte array
try{
//Lookup the available algorithms and adjust the code to be dynamic of fix
// http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#MessageDigest
mDigest = MessageDigest.getInstance("MD5");
theHash = mDigest.digest(messageBytes);
//Convert the byte array to Hex values for display
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i < theHash.length; i++) {
sBuffer.append(Integer.toString((theHash[i] & 0xff) + 0x100, 16).substring(1));
}
// Verify the results on-line with a simple string
// like "password" http://www.miraclesalad.com/webtools/md5.php
JOptionPane.showMessageDialog(null, "Your password hash is: "+sBuffer);
}
catch (java.security.NoSuchAlgorithmException e) { }
}// end of main method
}//end of Driver class
No comments:
Post a Comment