001    /**
002     * Implements the CFML Function encrypt
003     */
004    package railo.runtime.functions.other;
005    
006    import railo.runtime.PageContext;
007    import railo.runtime.coder.Coder;
008    import railo.runtime.crypt.CFMXCompat;
009    import railo.runtime.crypt.Cryptor;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.ext.function.Function;
012    import railo.runtime.op.Caster;
013    
014    // FUTURE 2 attr fehlen noch
015    
016    public final class Encrypt implements Function {
017        
018        // "CFMX_COMPAT" "UU"  null, 0
019        public synchronized static String call(PageContext pc , String string, String key) throws PageException {
020                    return call(pc,string,key,"cfmx_compat","uu");
021            }
022        
023        public synchronized static String call(PageContext pc , String string, String key, String algorithm) throws PageException {
024                    return call(pc,string,key,algorithm,"uu"); 
025            }
026        
027        public synchronized static String call(PageContext pc , String string, String key, String algorithm, String encoding) throws PageException {
028            return invoke(string, key, algorithm, encoding);
029            }
030        
031        public synchronized static String invoke(String input, String key, String algorithm, String encoding) throws PageException {
032            try {
033                            return Coder.encode(encoding,invoke(input.getBytes("UTF-8"), key, algorithm));
034                    } 
035            catch (Exception e) {
036                            throw Caster.toPageException(e);
037                    }
038        }
039        
040        public synchronized static byte[] invoke(byte[] input, String key, String algorithm) throws PageException {
041            try {
042                    if(algorithm==null || "cfmx_compat".equalsIgnoreCase(algorithm)){
043                            return new CFMXCompat().transformString(key, input);
044                    }
045                    return Cryptor.encrypt(algorithm, key, input);  
046                    } 
047            catch (Exception e) {
048                            throw Caster.toPageException(e);
049                    }
050            }
051    }