diff --git a/src/java/com/samskivert/util/StringUtil.java b/src/java/com/samskivert/util/StringUtil.java index aca090f4..12571faf 100644 --- a/src/java/com/samskivert/util/StringUtil.java +++ b/src/java/com/samskivert/util/StringUtil.java @@ -811,12 +811,17 @@ public class StringUtil */ public static String md5hex (String source) { - try { - MessageDigest digest = MessageDigest.getInstance("MD5"); - return hexlate(digest.digest(source.getBytes())); - } catch (NoSuchAlgorithmException nsae) { - throw new RuntimeException("MD5 codec not available"); - } + return digest("MD5", source); + } + + /** + * Returns a hex string representing the SHA-1 encoded source. + * + * @exception RuntimeException thrown if the SHA-1 codec was not available in this JVM. + */ + public static String sha1hex (String source) + { + return digest("SHA-1", source); } /** @@ -1059,23 +1064,6 @@ public class StringUtil return join(values, separator, false); } - /** - * Helper function for the various join methods. - */ - protected static String join (Object[] values, String separator, boolean escape) - { - StringBuilder buf = new StringBuilder(); - int vlength = values.length; - for (int i = 0; i < vlength; i++) { - if (i > 0) { - buf.append(separator); - } - String value = (values[i] == null) ? "" : values[i].toString(); - buf.append((escape) ? replace(value, ",", ",,") : value); - } - return buf.toString(); - } - /** * Joins an array of strings into a single string, separated by commas, and escaping commas * that occur in the individual string values such that a subsequent call to {@link @@ -1307,6 +1295,36 @@ public class StringUtil return code; } + /** + * Helper function for the various join methods. + */ + protected static String join (Object[] values, String separator, boolean escape) + { + StringBuilder buf = new StringBuilder(); + int vlength = values.length; + for (int i = 0; i < vlength; i++) { + if (i > 0) { + buf.append(separator); + } + String value = (values[i] == null) ? "" : values[i].toString(); + buf.append((escape) ? replace(value, ",", ",,") : value); + } + return buf.toString(); + } + + /** + * Helper function for {@link #md5hex} and {@link #sha1hex}. + */ + protected static String digest (String codec, String source) + { + try { + MessageDigest digest = MessageDigest.getInstance(codec); + return hexlate(digest.digest(source.getBytes())); + } catch (NoSuchAlgorithmException nsae) { + throw new RuntimeException(codec + " codec not available"); + } + } + /** Used to easily format floats with sensible defaults. */ protected static final NumberFormat _ffmt = NumberFormat.getInstance(); static {