utility class for java md5-methods
This commit is contained in:
parent
5fb400ec4c
commit
66f7b97e40
1 changed files with 37 additions and 0 deletions
37
src/helma/util/MD5Encoder.java
Normal file
37
src/helma/util/MD5Encoder.java
Normal file
|
@ -0,0 +1,37 @@
|
|||
package helma.util;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
|
||||
public class MD5Encoder {
|
||||
|
||||
private static MessageDigest md;
|
||||
|
||||
/** used by commandline script to create admin username & password */
|
||||
public static void main ( String args[] ) throws Exception {
|
||||
if ( args.length < 2 ) {
|
||||
System.out.println( "\n\nUsage: helma.util.MD5Encoder <username> <password>");
|
||||
System.out.println( "Output:");
|
||||
System.out.println( "adminUsername=<encoded username>");
|
||||
System.out.println( "adminPassword=<encoded password>\n");
|
||||
System.exit(0);
|
||||
}
|
||||
System.out.println( "adminUsername=" + encode(args[0]) );
|
||||
System.out.println( "adminPassword=" + encode(args[1]) );
|
||||
}
|
||||
|
||||
public static String encode(String str) throws Exception {
|
||||
md = MessageDigest.getInstance("MD5");
|
||||
byte[] b = md.digest(str.getBytes());
|
||||
StringBuffer buf = new StringBuffer();
|
||||
for ( int i=0; i<b.length; i++ ) {
|
||||
int j = (b[i]<0) ? 256+b[i] : b[i];
|
||||
if ( j<10 ) buf.append("0");
|
||||
buf.append(Integer.toHexString(j));
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Add table
Reference in a new issue