001    package railo.runtime.functions.conversion;
002    
003    
004    import javax.crypto.KeyGenerator;
005    
006    import railo.runtime.PageContext;
007    import railo.runtime.coder.Coder;
008    import railo.runtime.exp.PageException;
009    import railo.runtime.ext.function.Function;
010    import railo.runtime.op.Caster;
011    
012    /**
013     * Generates a Secret Key
014     */
015    public final class GenerateSecretKey implements Function {
016            
017            public static String call(PageContext pc, String algorithm) throws PageException {
018                    return call(pc, algorithm,0);
019            }
020            
021            public static String call(PageContext pc, String algorithm, double keySize) throws PageException {
022                    try     {
023                KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm.toUpperCase());
024                if(keySize>0) keyGenerator.init(Caster.toIntValue(keySize));
025                return Coder.encode(Coder.ENCODING_BASE64, keyGenerator.generateKey().getEncoded());
026            }
027            catch(Exception e) {
028                throw Caster.toPageException(e);
029            }
030            }
031            
032    }