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;
019    
020    import java.security.SecureRandom;
021    
022    import org.bouncycastle.crypto.params.KeyParameter;
023    import org.bouncycastle.crypto.params.ParametersWithIV;
024    
025    /**
026     * <p>
027     * Abstract base class for implementing a {@link MACCalculatorFactory}.
028     * </p><p>
029     * Implementors should subclass this class instead of directly implementing the interface
030     * <code>MACCalculatorFactory</code>.
031     * </p>
032     *
033     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
034     */
035    public abstract class AbstractMACCalculatorFactory implements MACCalculatorFactory
036    {
037            private String algorithmName;
038    
039            @Override
040            public String getAlgorithmName() {
041                    return algorithmName;
042            }
043    
044            @Override
045            public void setAlgorithmName(String algorithmName)
046            {
047                    if (this.algorithmName != null && !this.algorithmName.equals(algorithmName))
048                            throw new IllegalStateException("this.algorithmName is already assigned! Cannot modify!");
049    
050                    if (algorithmName == null)
051                            throw new IllegalArgumentException("algorithmName == null");
052    
053                    this.algorithmName = algorithmName;
054            }
055    
056            @Override
057            public MACCalculator createMACCalculator(boolean initWithDefaults)
058            {
059                    MACCalculator macCalculator = _createMACCalculator();
060    
061                    if (initWithDefaults) {
062                            SecureRandom random = new SecureRandom();
063                            byte[] key = new byte[macCalculator.getKeySize()];
064                            random.nextBytes(key);
065                            if (macCalculator.getIVSize() > 0) {
066                                    byte[] iv = new byte[macCalculator.getIVSize()];
067                                    random.nextBytes(iv);
068                                    macCalculator.init(new ParametersWithIV(new KeyParameter(key), iv));
069                            }
070                            else
071                                    macCalculator.init(new KeyParameter(key));
072                    }
073    
074                    macCalculator.setAlgorithmName(getAlgorithmName());
075    
076                    return macCalculator;
077            }
078    
079            protected abstract MACCalculator _createMACCalculator();
080    }