001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program 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
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.crypto.internal.asymmetric.keypairgenerator;
019    
020    import java.math.BigInteger;
021    import java.security.SecureRandom;
022    
023    import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
024    import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
025    import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;
026    
027    /**
028     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
029     */
030    public class RSAKeyPairGeneratorFactory
031    extends AbstractAsymmetricCipherKeyPairGeneratorFactory
032    {
033            public RSAKeyPairGeneratorFactory() {
034                    setAlgorithmName("RSA");
035            }
036    
037            private static final BigInteger defaultPublicExponent = BigInteger.valueOf(0x10001);
038            private static final int defaultTests = 12;
039    
040            private SecureRandom random;
041    
042            @Override
043            public AsymmetricCipherKeyPairGenerator createAsymmetricCipherKeyPairGenerator(boolean initWithDefaults)
044            {
045                    RSAKeyPairGenerator generator = new RSAKeyPairGenerator();
046    
047                    if (initWithDefaults) {
048                            if (random == null)
049                                    random = new SecureRandom();
050    
051                            generator.init(new RSAKeyGenerationParameters(defaultPublicExponent, random, 4096, defaultTests));
052                    }
053    
054                    return generator;
055            }
056    }