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.runtime.functions.displayFormatting;
020
021import java.nio.charset.Charset;
022
023import javax.crypto.Mac;
024import javax.crypto.SecretKey;
025import javax.crypto.spec.SecretKeySpec;
026
027import lucee.commons.digest.MD5;
028import lucee.commons.io.CharsetUtil;
029import lucee.commons.lang.StringUtil;
030import lucee.runtime.PageContext;
031import lucee.runtime.PageContextImpl;
032import lucee.runtime.exp.PageException;
033import lucee.runtime.ext.function.Function;
034import lucee.runtime.op.Caster;
035import lucee.runtime.op.Decision;
036
037public class HMAC implements Function {
038
039        private static final long serialVersionUID = -1999122154087043893L;
040
041        public static String call(PageContext pc,Object oMessage, Object oKey) throws PageException {
042                return call(pc, oMessage, oKey, null, null);
043        }
044        
045        public static String call(PageContext pc,Object oMessage, Object oKey, String algorithm) throws PageException {
046                return call(pc, oMessage, oKey, algorithm, null);
047        }
048        
049        public static String call(PageContext pc,Object oMessage, Object oKey, String algorithm, String charset) throws PageException {
050                // charset
051                Charset cs;
052        if(StringUtil.isEmpty(charset,true))
053            cs=((PageContextImpl)pc).getWebCharset();
054        else
055                cs = CharsetUtil.toCharset(charset);
056
057        // message
058                byte[] msg=toBinary(oMessage,cs);
059                
060        // message
061                byte[] key=toBinary(oKey,cs);
062                
063                // algorithm
064        if(StringUtil.isEmpty(algorithm,true)) algorithm = "HmacMD5";
065        
066        SecretKey sk = new SecretKeySpec(key, algorithm);
067            try {
068            Mac mac = Mac.getInstance(algorithm);
069            mac.init(sk);
070            mac.reset();
071            mac.update(msg);
072            msg = mac.doFinal();
073            return MD5.stringify(msg).toUpperCase();
074        }
075        catch(Exception e) {
076            throw Caster.toPageException(e);
077        }
078        }
079        
080        private static byte[] toBinary(Object obj, Charset cs) throws PageException {
081                if(Decision.isBinary(obj)){
082                        return Caster.toBinary(obj);
083                }
084                return Caster.toString(obj).getBytes(cs);
085        }
086}