View Javadoc

1   /***
2    * Simple Web Spider - <http://simplewebspider.sourceforge.net/>
3    * Copyright (C) 2009  <berendona@users.sourceforge.net>
4    *
5    * This program is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  package simplespider.simplespider.util;
19  
20  import java.io.UnsupportedEncodingException;
21  import java.security.MessageDigest;
22  import java.security.NoSuchAlgorithmException;
23  
24  public final class MD5 {
25  	private static ThreadLocal<MessageDigest>	MD5	= new ThreadLocal<MessageDigest>() {
26  
27  														@Override
28  														protected MessageDigest initialValue() {
29  															try {
30  																return MessageDigest.getInstance("MD5");
31  															} catch (final NoSuchAlgorithmException e) {
32  																throw new RuntimeException("Failed to get MD5 digest instance", e);
33  															}
34  														}
35  													};
36  
37  	public static final String encodeString(final String string) throws RuntimeException {
38  		return encodeString(string, null);
39  	}
40  
41  	/***
42  	 * * Retrieves a hexidecimal character sequence representing the MD5 * digest of the specified character sequence, using the specified * encoding
43  	 * to first convert the character sequence into a byte sequence. * If the specified encoding is null, then ISO-8859-1 is assumed * * @param string
44  	 * the string to encode. * @param encoding the encoding used to convert the string into the * byte sequence to submit for MD5 digest * @return a
45  	 * hexidecimal character sequence representing the MD5 * digest of the specified string * @throws HsqlUnsupportedOperationException if an MD5
46  	 * digest * algorithm is not available through the * java.security.MessageDigest spi or the requested * encoding is not available
47  	 */
48  	public static final String encodeString(final String string, final String encoding) throws RuntimeException {
49  		return StringUtils.bytesToHex(digestString(string, encoding), false);
50  	}
51  
52  	/***
53  	 * * Retrieves a byte sequence representing the MD5 digest of the * specified character sequence, using the specified encoding to * first convert
54  	 * the character sequence into a byte sequence. * If the specified encoding is null, then ISO-8859-1 is * assumed. * * @param string the string to
55  	 * digest. * @param encoding the character encoding. * @return the digest as an array of 16 bytes. * @throws HsqlUnsupportedOperationException if
56  	 * an MD5 digest * algorithm is not available through the * java.security.MessageDigest spi or the requested * encoding is not available
57  	 */
58  	public static byte[] digestString(final String string, String encoding) throws RuntimeException {
59  		byte[] data;
60  		if (encoding == null) {
61  			encoding = "ISO-8859-1";
62  		}
63  		try {
64  			data = string.getBytes(encoding);
65  		} catch (final UnsupportedEncodingException x) {
66  			throw new RuntimeException("Unsupported encoding: " + encoding, x);
67  		}
68  		return digestBytes(data);
69  	}
70  
71  	/***
72  	 * * Retrieves a byte sequence representing the MD5 digest of the * specified byte sequence. * * @param data the data to digest. * @return the MD5
73  	 * digest as an array of 16 bytes. * @throws HsqlUnsupportedOperationException if an MD5 digest * algorithm is not available through the *
74  	 * java.security.MessageDigest spi
75  	 */
76  	public static final byte[] digestBytes(final byte[] data) throws RuntimeException {
77  		return MD5.get().digest(data);
78  	}
79  }