001    package users;
002    import java.io.UnsupportedEncodingException;
003    import java.security.MessageDigest;
004    import java.security.NoSuchAlgorithmException;
005    
006    /**
007     * An class for generating a securly encoded passwort string using the MD5 algorithm 
008     * @author Alexander Herrmann
009     */
010    public final class MD5 {
011    
012        /**
013         * The MD5 generator.
014         */
015        private static MessageDigest md5;
016        
017        /**
018         * An Array with all hexadecimal characters.
019         * Needed for converting byte[] to hexadecimal
020         */
021        private static final char chHexchar[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
022    
023        /**
024         * Retrieves a hexadecimal character sequence representing the MD5
025         * digest of the specified character sequence by using ISO-8859-1 encoding
026         *
027         * @param sPhrase the string to encode.
028         * @return a hexadecimal character sequence representing the MD5 digest of the specified string
029         * @throws RuntimeException if an MD5 digest algorithm is not available through the java.security.MessageDigest spi
030         */
031        public static final String encodeString(String sPhrase) throws RuntimeException {
032            return byteToHex(digestString(sPhrase));
033        }
034        
035        /**
036         * Converts a byte-Array to hexadecimal string
037         * 
038         * @param b the byte[] to be converted
039         * @return hexadecimal string
040         */
041        public static String byteToHex(byte b[]) {
042            int len = b.length;
043            char[] s = new char[len * 2];
044            for(int i = 0, j = 0; i < len; i++) {
045                int c = ((int) b[i]) & 0xff;
046                s[j++] = chHexchar[c >> 4 & 0xf];
047                s[j++] = chHexchar[c & 0xf];
048            }
049            return new String(s);
050        }
051    
052        /**
053         * Retrieves a byte sequence that represents the MD5 digest string.
054         * Used encoding is ISO-8859-1
055         *
056         * @param sPhrase the string to digest.
057         * @return the digest as an array of 16 bytes.
058         * @throws RuntimeException if an MD5 digest algorithm is not available through the java.security.MessageDigest spi 
059         */
060        public static byte[] digestString(String sPhrase) throws RuntimeException {
061            byte[] bData;
062            String sEncoding = "ISO-8859-1";
063            try {
064                bData = sPhrase.getBytes(sEncoding);
065                }
066            catch (UnsupportedEncodingException ue) {
067                throw new RuntimeException(ue.toString());
068            }
069            return digestBytes(bData);
070        }
071    
072        /**
073         * Retrieves a byte sequence representing the MD5 digest of the
074         * specified byte sequence.
075         *
076         * @param bData the data to digest.
077         * @return the MD5 digest as an array of 16 bytes.
078         * @throws RuntimeException if an MD5 digest algorithm is not available through the java.security.MessageDigest spi
079         */
080        public static final byte[] digestBytes(byte[] bData) throws RuntimeException {
081            synchronized (MD5.class) {
082                if (md5 == null) {
083                    try {
084                        md5 = MessageDigest.getInstance("MD5");
085                    } catch (NoSuchAlgorithmException e) {
086                        throw new RuntimeException(e.toString());
087                    }
088                }
089                return md5.digest(bData);
090            }
091        }
092    }