FileDocCategorySizeDatePackage
SecretKeySpec.javaAPI DocAndroid 1.5 API6200Wed May 06 22:41:02 BST 2009javax.crypto.spec

SecretKeySpec

public class SecretKeySpec extends Object implements SecretKey, Serializable, KeySpec
A key specification for a SecretKey and also a secret key implementation that is provider-independent. It can be used for raw secret keys that can be specified as byte[].
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private final byte[]
key
private final String
algorithm
private final String
format
Constructors Summary
public SecretKeySpec(byte[] key, String algorithm)
Creates a new SecretKeySpec for the specified key data and algorithm name.

param
key the key data.
param
algorithm the algorithm name.
throws
IllegalArgumentException if the key data or the algorithm name is null or if the key data is empty.

 //$NON-NLS-1$

                                                                                                 
         
        if (key == null) {
            throw new IllegalArgumentException(Messages.getString("crypto.05")); //$NON-NLS-1$
        }
        if (key.length == 0) {
            throw new IllegalArgumentException(Messages.getString("crypto.35")); //$NON-NLS-1$
        }
        if (algorithm == null) {
            throw new IllegalArgumentException(Messages.getString("crypto.02")); //$NON-NLS-1$
        }

        this.algorithm = algorithm;
        this.key = new byte[key.length];
        System.arraycopy(key, 0, this.key, 0, key.length);
    
public SecretKeySpec(byte[] key, int offset, int len, String algorithm)
Creates a new SecretKeySpec for the key data from the specified buffer key starting at offset with length len and the specified algorithm name.

param
key the key data.
param
offset the offset.
param
len the size of the key data.
param
algorithm the algorithm name.
throws
IllegalArgumentException if the key data or the algorithm name is null, the key data is empty or offset and len do not specify a valid chunk in the buffer key.
throws
ArrayIndexOutOfBoundsException if offset or len is negative.

        if (key == null) {
            throw new IllegalArgumentException(Messages.getString("crypto.05")); //$NON-NLS-1$
        }
        if (key.length == 0) {
            throw new IllegalArgumentException(Messages.getString("crypto.35")); //$NON-NLS-1$
        }
        // BEGIN android-changed
        if (len < 0 || offset < 0) {
            throw new ArrayIndexOutOfBoundsException(Messages.getString("crypto.36")); //$NON-NLS-1$
        }
        // END android-changed
        if ((key.length - offset < len)) {
            throw new IllegalArgumentException(Messages.getString("crypto.37")); //$NON-NLS-1$
        }
        if (algorithm == null) {
            throw new IllegalArgumentException(Messages.getString("crypto.02")); //$NON-NLS-1$
        }
        this.algorithm = algorithm;
        this.key = new byte[len];
        System.arraycopy(key, offset, this.key, 0, len);
    
Methods Summary
public booleanequals(java.lang.Object obj)
Compares the specified object with this SecretKeySpec instance.

param
obj the object to compare.
return
true if the algorithm name and key of both object are equal, otherwise false.

        if (obj == this) {
            return true;
        }
        if (!(obj instanceof SecretKeySpec)) {
            return false;
        }
        SecretKeySpec ks = (SecretKeySpec) obj;
        return (algorithm.equalsIgnoreCase(ks.algorithm))
            && (Arrays.equals(key, ks.key));
    
public java.lang.StringgetAlgorithm()
Returns the algorithm name.

return
the algorithm name.

        return algorithm;
    
public byte[]getEncoded()
Returns the encoded form of this secret key.

return
the encoded form of this secret key.

        byte[] result = new byte[key.length];
        System.arraycopy(key, 0, result, 0, key.length);
        return result;
    
public java.lang.StringgetFormat()
Returns the name of the format used to encode the key.

return
the format name "RAW".

        return format;
    
public inthashCode()
Returns the hash code of this SecretKeySpec object.

return
the hash code.

        int result = algorithm.length();
        for (byte element : key) {
            result += element;
        }
        return result;