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 **/
019/**
020 * Implements the CFML Function hash
021 */
022package lucee.runtime.functions.string;
023
024import java.security.MessageDigest;
025
026import lucee.commons.lang.MD5Legacy;
027import lucee.commons.lang.StringUtil;
028import lucee.runtime.PageContext;
029import lucee.runtime.config.Config;
030import lucee.runtime.exp.PageException;
031import lucee.runtime.ext.function.Function;
032import lucee.runtime.op.Caster;
033
034public final class Hash40 implements Function {
035
036        private static final long serialVersionUID = 937180000352201249L;
037
038        // function for old code in ra files calling this function
039        public static String call(PageContext pc, String input) throws PageException {
040                return invoke( pc.getConfig(), input, null, null, 1 );
041        }
042    public static String call(PageContext pc , String input, String algorithm) throws PageException {
043                return invoke( pc.getConfig(), input, algorithm, null, 1 );
044        }
045    public static String call(PageContext pc , String input, String algorithm, String encoding) throws PageException {
046                return invoke( pc.getConfig(), input, algorithm, encoding, 1 );
047        }
048        //////
049        
050        
051        public static String call(PageContext pc, Object input) throws PageException {
052                return invoke( pc.getConfig(), input, null, null, 1 );
053        }
054        
055    public static String call(PageContext pc , Object input, String algorithm) throws PageException {
056                return invoke( pc.getConfig(), input, algorithm, null, 1 );
057        }
058
059    public static String call(PageContext pc , Object input, String algorithm, String encoding) throws PageException {
060                return invoke( pc.getConfig(), input, algorithm, encoding, 1 );
061        }
062    
063    public static String call(PageContext pc , Object input, String algorithm, String encoding, double numIterations) throws PageException {
064        int ni=(int)numIterations;
065                if(ni<1)ni=1;
066        return invoke( pc.getConfig(), input, algorithm, encoding, ni);
067        }
068
069    /*/ this method signature was called from ConfigWebAdmin.createUUID(), comment this comment to enable
070    public synchronized static String invoke(Config config, Object input, String algorithm, String encoding) throws PageException {
071        
072        return invoke(config, input, algorithm, encoding, 1);
073    }   //*/
074    
075    public static String invoke(Config config, Object input, String algorithm, String encoding, int numIterations) throws PageException {
076
077        if(StringUtil.isEmpty(algorithm))algorithm="md5";
078                else algorithm=algorithm.trim().toLowerCase();
079                if(StringUtil.isEmpty(encoding))encoding=config.getWebCharset();
080                
081                boolean isDefaultAlgo = numIterations == 1 && ("md5".equals(algorithm) || "cfmx_compat".equals(algorithm));
082                byte[] arrBytes = null;
083                try {                   
084                        if(input instanceof byte[]) {
085                                arrBytes = (byte[])input;
086                                if(isDefaultAlgo) return MD5Legacy.getDigestAsString( arrBytes ).toUpperCase();
087                        } 
088                        else {
089                                String string = Caster.toString(input);
090                                if (isDefaultAlgo) return MD5Legacy.getDigestAsString( string ).toUpperCase();
091                                arrBytes = string.getBytes( encoding );
092                        }
093                        
094                        MessageDigest md=MessageDigest.getInstance(algorithm);
095                    md.reset();
096                    
097                        for(int i=0; i<numIterations; i++)
098                                md.update(arrBytes);
099                    
100                        return MD5Legacy.stringify( md.digest() ).toUpperCase();
101                } 
102                catch (Throwable t) {
103                        throw Caster.toPageException(t);
104                }
105        }
106
107}