|
|||||||
|
|
![]() |
|
|
|
|
||
|
Generating a Validation String in Java Hey, David: I am porting an application to the iSeries and need to generate an MD5 hash that is used to validate a license key, but the only utility I can find to do this relies on a hardware coprocessor to generate the MD5 hash. This procedure will run a few times a day, and very few of our customers have this hardware anyway, so API is out of the question. Do you know of any software-based solutions (preferably free or one-time license) that will generate an MD5 hash for a text string? A callable API would be best, but a Java program would be OK. --Walt The request for comment (RFC) 1321, which defines the MD5 algorithm, provides an example program written in C. You may be able to get that C program to run on the iSeries, but I decided to use Java because it has built-in support for the MD5 algorithm as part of the Java security package and it definitely runs on the iSeries. The example Hash program I wrote is fairly simple because all of the complex processing occurs in Java's MessageDigest class. The sample program MessageDigest, found in the java.security package, converts a string value to a corresponding hash value. In addition to MD5, the security package supports formats like MD2 and SHA-1. There are several ways to use the example Hash class. First, you can create a Hash object using something like this:
Hash myHash = new Hash("This is the plaintext", "MD5");
String myHashString = myHash.toString();
In this case, myHashString ends up being 035469ce84945b7da20d75b9899aed40. Another way to use the Hash class is to call the getHashText method directly. Because getHashText is static, you won't even have to create a Hash object. Your code will look something like this:
try {
hashText = Hash.getHashText(this.plainText, this.algorithm);
}
catch (NoSuchAlgorithmException nsae) {
System.err.println(nsae.getLocalizedMessage());
}
To understand how the Hash class works, I will walk you through the code. Most of it is pretty standard. The important part is in the getHashText method, so that is what I will explain. Here is the complete Hash.java source:
package demo;
import java.security.*;
/**
* Class Hash produces a MessageDigest hash for a given string.
* @author David Morris
*/
public class Hash {
private String plainText;
private String algorithm;
/**
* @see java.lang.Object#Object()
*/
public Hash() {
super();
}
/**
* Method Hash.
* @param plainText
* @param algorithm The algorithm to use like MD2, MD5, SHA-1, etc.
*/
public Hash(String plainText, String algorithm) {
super();
setPlainText(plainText);
setAlgorithm(algorithm);
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
String hashText = null;
try {
hashText = Hash.getHashText(this.plainText, this.algorithm);
}
catch (NoSuchAlgorithmException nsae) {
System.err.println(nsae.getLocalizedMessage());
}
return hashText;
}
/**
* Method getHashText.
* @param plainText
* @param algorithm The algorithm to use like MD2, MD5, SHA-1, etc.
* @return String
* @throws NoSuchAlgorithmException
*/
public static String getHashText(String plainText, String algorithm)
throws NoSuchAlgorithmException {
MessageDigest mdAlgorithm = MessageDigest.getInstance(algorithm);
mdAlgorithm.update(plainText.getBytes());
byte[] digest = mdAlgorithm.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < digest.length; i++) {
plainText = Integer.toHexString(0xFF & digest[i]);
if (plainText.length() < 2) {
plainText = "0" + plainText;
}
hexString.append(plainText);
}
return hexString.toString();
}
/**
* Returns the algorithm.
* @return String
*/
public String getAlgorithm() {
return algorithm;
}
/**
* Returns the plainText.
* @return String
*/
public String getPlainText() {
return plainText;
}
/**
* Sets the algorithm.
* @param algorithm The algorithm to set
*/
public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
/**
* Sets the plainText.
* @param plainText The plainText to set
*/
public void setPlainText(String plainText) {
this.plainText = plainText;
}
/**
* Method main Usage: java demo.Hash "plaintext" "MD5"
* @param args
*/
public static void main(String[] args) {
if (args.length == 2) {
System.out.println(new Hash(args[0], args[1]));
}
else {
System.err.println(
"Usage: java demo.Hash \"plaintext\" \"MD2/MD5/SHA-1\"");
}
}
}
The getHashText method starts out by creating a MessageDigest object for the algorithm passed. The algorithm is passed as a string like MD5. Next, the plaintext is converted to a byte array. You may want to set the encoding scheme when you create your byte array, especially if you plan to run this class on more than one type of computer. This class could also work with a file by reading a FileInputStream into a ByteArrayOutputStream. After setting the algorithm to use, the getHashText method loops through the digest byte array and converts the digest output to hex using the static Integer.getHexString method. The converted output is stored in a StringBuffer, which is returned as a string. This example should get you started and could be simplified all the way down to a static getHashText method with a private constructor. A private constructor should be used when all methods are static, to eliminate the possibility that a programmer will create an instance. --David
|
Editors
Contact the Editors |
| Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |