Moved MD5 encoding routine into StringUtil.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@864 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2002-10-16 00:42:26 +00:00
parent 2ed07b5590
commit 056072405c
2 changed files with 23 additions and 11 deletions
@@ -1,5 +1,5 @@
//
// $Id: UserUtil.java,v 1.4 2001/08/11 22:43:28 mdb Exp $
// $Id: UserUtil.java,v 1.5 2002/10/16 00:42:26 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
@@ -20,8 +20,6 @@
package com.samskivert.servlet.user;
import java.security.*;
import com.samskivert.util.Crypt;
import com.samskivert.util.StringUtil;
@@ -42,17 +40,14 @@ public class UserUtil
buf.append(Math.random());
// and MD5 hash it
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] enc = digest.digest(buf.toString().getBytes());
return StringUtil.hexlate(enc);
} catch (NoSuchAlgorithmException nsae) {
String auth = StringUtil.md5hex(buf.toString());
if (auth == null) {
throw new RuntimeException("JVM missing MD5 message digest " +
"algorithm implementation. User " +
"management facilities require MD5 " +
"encoding capabilities.");
}
}
return auth;
}
/**
@@ -1,5 +1,5 @@
//
// $Id: StringUtil.java,v 1.37 2002/09/24 07:43:50 mdb Exp $
// $Id: StringUtil.java,v 1.38 2002/10/16 00:42:26 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
@@ -29,6 +29,9 @@ import java.lang.reflect.Modifier;
import java.net.InetAddress;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
@@ -575,6 +578,20 @@ public class StringUtil
return (bytes == null) ? "" : hexlate(bytes, bytes.length);
}
/**
* Returns a hex string representing the MD5 encoded source or null if
* the MD5 codec was not available in this JVM.
*/
public static String md5hex (String source)
{
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
return hexlate(digest.digest(source.getBytes()));
} catch (NoSuchAlgorithmException nsae) {
return null;
}
}
/**
* Parses an array of integers from it's string representation. The
* array should be represented as a bare list of numbers separated by