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.store.crypto.keymanager;
019    
020    import java.util.Date;
021    
022    import org.cumulus4j.crypto.Cipher;
023    import org.cumulus4j.crypto.CryptoRegistry;
024    
025    /**
026     * {@link CryptoCache}-entry wrapping a {@link Cipher} used for symmetric data-en-/decryption.
027     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
028     */
029    public class CryptoCacheCipherEntry
030    {
031            private CryptoCacheKeyEntry keyEntry;
032            private String cipherTransformation;
033            private Cipher cipher;
034    
035            private Date lastUsageTimestamp = new Date();
036    
037            /**
038             * Create a new instance.
039             * @param keyEntry the corresponding key-entry. Must not be <code>null</code>.
040             * @param cipherTransformation the transformation (as passed to {@link CryptoRegistry#createCipher(String)}). Must not be <code>null</code>.
041             * @param cipher the cipher. Must not be <code>null</code>.
042             */
043            protected CryptoCacheCipherEntry(CryptoCacheKeyEntry keyEntry, String cipherTransformation, Cipher cipher)
044            {
045                    if (keyEntry == null)
046                            throw new IllegalArgumentException("keyEntry == null");
047    
048                    if (cipherTransformation == null)
049                            throw new IllegalArgumentException("cipherTransformation == null");
050    
051                    if (cipher == null)
052                            throw new IllegalArgumentException("cipher == null");
053    
054                    this.keyEntry = keyEntry;
055                    this.cipherTransformation = cipherTransformation;
056                    this.cipher = cipher;
057            }
058    
059            /**
060             * Create a new instance copying an old one. Used to refresh an existing entry (as it assigns a new
061             * {@link #getLastUsageTimestamp() lastUsageTimestamp}) while keeping this class immutable.
062             * @param keyEntry the key-entry.
063             * @param original the original from which to copy.
064             */
065            protected CryptoCacheCipherEntry(CryptoCacheKeyEntry keyEntry, CryptoCacheCipherEntry original)
066            {
067                    this(keyEntry, original.getCipherTransformation(), original.getCipher());
068            }
069    
070            /**
071             * Get the corresponding key-entry.
072             * @return the corresponding key-entry; never <code>null</code>.
073             */
074            public CryptoCacheKeyEntry getKeyEntry() {
075                    return keyEntry;
076            }
077    
078            /**
079             * Get the transformation (as passed to {@link CryptoRegistry#createCipher(String)}).
080             * @return the transformation (as passed to {@link CryptoRegistry#createCipher(String)}); never <code>null</code>.
081             */
082            public String getCipherTransformation() {
083                    return cipherTransformation;
084            }
085    
086            /**
087             * Get the cipher.
088             * @return the cipher; never <code>null</code>.
089             */
090            public Cipher getCipher() {
091                    return cipher;
092            }
093    
094            /**
095             * Get the timestamp when the cipher was used the last time.
096             * @return the timestamp when the cipher was used the last time.
097             */
098            public Date getLastUsageTimestamp() {
099                    return lastUsageTimestamp;
100            }
101    }