001    package railo.transformer.util;
002    
003    import java.security.MessageDigest;
004    import java.security.NoSuchAlgorithmException;
005    
006    
007    
008    /**
009     * Class Hash produces a MessageDigest hash for a given string.
010     */
011    public final class Hash {
012            private String plainText;
013            private String algorithm;
014    
015    
016            /**
017             * Method Hash.
018             * @param plainText
019             * @param algorithm The algorithm to use like MD2, MD5, SHA-1, etc.
020             */
021            public Hash(String plainText, String algorithm) {
022                    super();
023                    setPlainText(plainText);
024                    setAlgorithm(algorithm);
025            }
026    
027            /**
028             * @see java.lang.Object#toString()
029             */
030            public String toString() {
031                    String hashText = null;
032    
033                    try {
034                            hashText = Hash.getHashText(this.plainText, this.algorithm);
035                    }
036                    catch (NoSuchAlgorithmException nsae) {
037                            System.err.println(nsae.getLocalizedMessage());
038                    }
039    
040                    return hashText;
041            }
042    
043            /**
044             * Method getHashText.
045             * @param plainText
046             * @param algorithm The algorithm to use like MD2, MD5, SHA-1, etc.
047             * @return String
048             * @throws NoSuchAlgorithmException
049             */
050            public static String getHashText(String plainText, String algorithm)
051                    throws NoSuchAlgorithmException {
052                    MessageDigest mdAlgorithm = MessageDigest.getInstance(algorithm);
053    
054                    mdAlgorithm.update(plainText.getBytes());
055    
056                    byte[] digest = mdAlgorithm.digest();
057                    StringBuffer hexString = new StringBuffer();
058    
059                    for (int i = 0; i < digest.length; i++) {
060                            plainText = Integer.toHexString(0xFF & digest[i]);
061    
062                            if (plainText.length() < 2) {
063                                    plainText = "0" + plainText;
064                            }
065    
066                            hexString.append(plainText);
067                    }
068    
069                    return hexString.toString();
070            }
071    
072            /**
073             * Returns the algorithm.
074             * @return String
075             */
076            public String getAlgorithm() {
077                    return algorithm;
078            }
079    
080            /**
081             * Returns the plainText.
082             * @return String
083             */
084            public String getPlainText() {
085                    return plainText;
086            }
087    
088            /**
089             * Sets the algorithm.
090             * @param algorithm The algorithm to set
091             */
092            public void setAlgorithm(String algorithm) {
093                    this.algorithm = algorithm;
094            }
095    
096            /**
097             * Sets the plainText.
098             * @param plainText The plainText to set
099             */
100            public void setPlainText(String plainText) {
101                    this.plainText = plainText;
102            }
103    
104    }