//============================================================================== // tribble/crypto/SymmetricKey.java //============================================================================== package tribble.crypto; // System imports import java.lang.Exception; import java.lang.IllegalArgumentException; import java.lang.String; import java.security.InvalidKeyException; import java.security.Key; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; // Local imports // (None) /******************************************************************************* * Symmetric block cipher key. * *
* This base class provides the foundation for keys of cipher (encryption) * algorithms having fixed-length blocks and key lengths and which utilize the * same (symmetric, secret) initialization key for both encryption and * decryption. * *
* Note that none of these methods are synchronized. * * * @version $Revision: 1.1 $ $Date: 2005/06/27 00:24:25 $ * @since JCE 1.2 / JRE 1.4, 2002-05-26 * @author * David R. Tribble * (david@tribble.com). * *
* Copyright ©2005 by David R. Tribble, all rights reserved.
*
* Permission is granted to freely use and distribute this source code
* provided that the original copyright and authorship notices remain
* intact.
*
* @see SymmetricCipherSpi
*/
public class SymmetricKey
extends javax.crypto.spec.SecretKeySpec
implements java.security.Key, javax.crypto.SecretKey
{
// Identification
/** Revision information. */
static final String REV =
"@(#)tribble/crypto/SymmetricKey.java $Revision: 1.1 $ $Date: 2005/06/27 00:24:25 $\n";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Package private variables
/** Raw key contents. */
byte[] m_key;
/** Cipher algorithm name. */
String m_alg;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Public constructors
/***************************************************************************
* Constructor.
*
*
* @param key
* Raw key contents.
* A private copy of this array is made, so the contents of key
* should be wiped after constructing this key in order to protect sensitive
* information from lingering in memory.
*
* @param alg
* Name of the symmetric cipher algorithm associated with this key.
*
* @since 1.1, 2005-05-26
*/
public SymmetricKey(/*const*/ byte[] key, String alg)
{
// Initialize
this(key, 0, key.length, alg);
}
/***************************************************************************
* Constructor.
*
*
* @param key
* Raw key contents.
* A private copy of this array is made, so the contents of key
* should be wiped after constructing this key in order to protect sensitive
* information from lingering in memory.
*
* @param off
* Index of the first element in key comprising the key.
*
* @param len
* Number of elements in key comprising the key.
*
* @param alg
* Name of the symmetric cipher algorithm associated with this key.
*
* @since 1.1, 2005-05-26
*/
public SymmetricKey(/*const*/ byte[] key, int off, int len, String alg)
{
// Initialize
super(null, 0, 0, alg);
// Sanity checks
if (key == null)
throw new IllegalArgumentException("Null symmetric key");
if (key.length < 1 || len < 1)
throw new IllegalArgumentException("Empty symmetric key");
// Initialize
{
int i;
// Make a private copy of the key contents
m_key = new byte[len];
i = 0;
len += off;
while (off < len)
m_key[i++] = key[off++];
}
m_alg = alg;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Public methods
/***************************************************************************
* Determmine if this key is equal to another object.
* Symmetric keys with the same raw key contents are equal to each other.
*
*
* @param obj
* Another object to compare this one to.
*
* @return
* True if obj is a {@link SymmetricKey} and is composed of the same
* information as this key, otherwise false.
*
* @since 1.1, 2003-05-26
*/
public boolean equals(/*const*/ Object obj)
/*const*/
//overrides javax.crypto.spec.SecretKeySpec
{
// Compare this object to another object
if (obj instanceof SymmetricKey)
{
SymmetricKey o;
// Compare the key contents
o = (SymmetricKey) obj;
if (this.m_key == null || o.m_key == null)
return (false);
return (this.m_key.equals(o.m_key));
}
else
return (false);
}
/***************************************************************************
* Determmine the hash code for this key.
* Symmetric keys with the same raw key contents have the same hash values.
*
*
* @return
* A hash code for this key, derived from its raw key contents.
*
* @since 1.1, 2003-05-26
*/
public int hashCode()
/*const*/
//overrides javax.crypto.spec.SecretKeySpec
{
int h;
// Sanity check
if (m_key == null)
return (0);
// Calculate a hash code from the raw key contents
h = 0;
for (int i = 0; i < m_key.length; i++)
h = ((h << 3) | (h >>> 32-3)) ^ m_key[i];
return (h);
}
/***************************************************************************
* Retrieve the name of the cipher that uses this key.
*
*
* @return
* Name of the cipher algorithm employing this key.
*
* @since 1.1, 2003-05-26
*/
public String getAlgorithm()
/*const*/
//overrides javax.crypto.spec.SecretKeySpec
{
return (m_alg);
}
/***************************************************************************
* Retrieve the raw contents of this key.
*
*
* @return
* The raw key material comprising this key.
* Note that a copy of the key is returned, so that any subsequent
* modifications made to the copy will not affect the actual value of this
* key.
*
* @since 1.1, 2003-05-26
*/
public byte[] getEncoded()
/*const*/
//overrides javax.crypto.spec.SecretKeySpec
{
byte[] dup;
// Sanity check
if (m_key == null)
return (null);
// Return a copy of the raw key contents
dup = new byte[m_key.length];
for (int i = 0; i < dup.length; i++)
dup[i] = m_key[i];
return (dup);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Protected methods
/***************************************************************************
* Finalization.
*
* @since 1.1, 2005-05-26
*/
protected synchronized void finalize()
throws Throwable
//overrides java.lang.Object
{
// Wipe the key contents
if (m_key != null)
{
for (int i = 0; i < m_key.length; i++)
m_key[i] = 0x00;
m_key = null;
}
// Cascade the finalization
super.finalize();
}
}
// End SymmetricKey.java