1 30 31 32 package org.hsqldb.lib; 33 34 import java.io.UnsupportedEncodingException ; 35 import java.security.MessageDigest ; 36 import java.security.NoSuchAlgorithmException ; 37 38 41 81 public final class MD5 { 82 83 86 private static MessageDigest md5; 87 88 104 public static final String encodeString(String string, 105 String encoding) throws RuntimeException { 106 return StringConverter.byteToHex(digestString(string, encoding)); 107 } 108 109 124 public static byte[] digestString(String string, 125 String encoding) 126 throws RuntimeException { 127 128 byte[] data; 129 130 if (encoding == null) { 131 encoding = "ISO-8859-1"; 132 } 133 134 try { 135 data = string.getBytes(encoding); 136 } catch (UnsupportedEncodingException x) { 137 throw new RuntimeException (x.toString()); 138 } 139 140 return digestBytes(data); 141 } 142 143 153 public static final byte[] digestBytes(byte[] data) 154 throws RuntimeException { 155 156 synchronized (MD5.class) { 157 if (md5 == null) { 158 try { 159 md5 = MessageDigest.getInstance("MD5"); 160 } catch (NoSuchAlgorithmException e) { 161 throw new RuntimeException (e.toString()); 162 } 163 } 164 165 return md5.digest(data); 166 } 167 } 168 } 169 | Popular Tags |