001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.util.crypt;
018
019import java.security.NoSuchAlgorithmException;
020import java.security.SecureRandom;
021import java.security.spec.InvalidKeySpecException;
022import java.security.spec.KeySpec;
023
024import javax.crypto.Cipher;
025import javax.crypto.KeyGenerator;
026import javax.crypto.SecretKey;
027import javax.crypto.SecretKeyFactory;
028import javax.crypto.spec.PBEKeySpec;
029import javax.crypto.spec.SecretKeySpec;
030
031/**
032 * Utility class meant to help building {@link Cipher}.
033 */
034public class CipherUtils
035{
036        /**
037         * Generate a new {@link SecretKey} based on the given algorithm and with the given length.
038         * 
039         * @param algorithm
040         *              the algorithm that will be used to build the key.
041         * @param keyLength
042         *              the key length
043         * @return a new {@link SecretKey}
044         */
045        public static SecretKey generateKey(String algorithm, int keyLength, SecureRandom secureRandom)
046        {
047                try
048                {
049                        KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
050                        keyGenerator.init(keyLength, secureRandom);
051                        SecretKey key = keyGenerator.generateKey();
052                        return key;
053                }
054                catch (NoSuchAlgorithmException e)
055                {
056                        throw new RuntimeException(e);
057                }
058        }
059
060        /**
061         * 
062         * 
063         * @param password
064         *              the password that will be used to build the key.
065         * @param pbeAlgorithm
066         *              the password-based algorithm that will be used to build the key.
067         * @param keyAlgorithm
068         *              the algorithm that will be used to build the key.
069         * @param salt
070         *              salt for encryption.
071         * @param iterationCount
072         *                              iteration count.
073         * @param keyLength
074         *              the key length.
075         * @return a new {@link SecretKey}
076         */
077        public static SecretKey generatePBEKey(String password, String pbeAlgorithm,
078                String keyAlgorithm, byte[] salt, int iterationCount, int keyLength)
079        {
080                try
081                {
082                        SecretKeyFactory factory = SecretKeyFactory.getInstance(pbeAlgorithm);
083                        KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength);
084                        SecretKey secret = new SecretKeySpec(factory.generateSecret(spec).getEncoded(),
085                                keyAlgorithm);
086                        return secret;
087                }
088                catch (NoSuchAlgorithmException | InvalidKeySpecException e)
089                {
090                        throw new RuntimeException(e);
091                }
092        }
093}