001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.transformer.util;
020
021import java.security.MessageDigest;
022import java.security.NoSuchAlgorithmException;
023
024
025
026/**
027 * Class Hash produces a MessageDigest hash for a given string.
028 */
029public final class Hash {
030        private String plainText;
031        private String algorithm;
032
033
034        /**
035         * Method Hash.
036         * @param plainText
037         * @param algorithm The algorithm to use like MD2, MD5, SHA-1, etc.
038         */
039        public Hash(String plainText, String algorithm) {
040                super();
041                setPlainText(plainText);
042                setAlgorithm(algorithm);
043        }
044
045        /**
046         * @see java.lang.Object#toString()
047         */
048        public String toString() {
049                String hashText = null;
050
051                try {
052                        hashText = Hash.getHashText(this.plainText, this.algorithm);
053                }
054                catch (NoSuchAlgorithmException nsae) {
055                        System.err.println(nsae.getLocalizedMessage());
056                }
057
058                return hashText;
059        }
060
061        /**
062         * Method getHashText.
063         * @param plainText
064         * @param algorithm The algorithm to use like MD2, MD5, SHA-1, etc.
065         * @return String
066         * @throws NoSuchAlgorithmException
067         */
068        public static String getHashText(String plainText, String algorithm)
069                throws NoSuchAlgorithmException {
070                MessageDigest mdAlgorithm = MessageDigest.getInstance(algorithm);
071
072                mdAlgorithm.update(plainText.getBytes());
073
074                byte[] digest = mdAlgorithm.digest();
075                StringBuffer hexString = new StringBuffer();
076
077                for (int i = 0; i < digest.length; i++) {
078                        plainText = Integer.toHexString(0xFF & digest[i]);
079
080                        if (plainText.length() < 2) {
081                                plainText = "0" + plainText;
082                        }
083
084                        hexString.append(plainText);
085                }
086
087                return hexString.toString();
088        }
089
090        /**
091         * Returns the algorithm.
092         * @return String
093         */
094        public String getAlgorithm() {
095                return algorithm;
096        }
097
098        /**
099         * Returns the plainText.
100         * @return String
101         */
102        public String getPlainText() {
103                return plainText;
104        }
105
106        /**
107         * Sets the algorithm.
108         * @param algorithm The algorithm to set
109         */
110        public void setAlgorithm(String algorithm) {
111                this.algorithm = algorithm;
112        }
113
114        /**
115         * Sets the plainText.
116         * @param plainText The plainText to set
117         */
118        public void setPlainText(String plainText) {
119                this.plainText = plainText;
120        }
121
122}