Class SecretKeySpec

java.lang.Object
javax.crypto.spec.SecretKeySpec
All Implemented Interfaces:
Serializable, Key, KeySpec, SecretKey

public class SecretKeySpec
extends Object
implements SecretKey, KeySpec, Serializable
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[].
See Also:
Serialized Form
  • Constructor Summary

    Constructors
    Constructor Description
    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.
    SecretKeySpec​(byte[] key, String algorithm)
    Creates a new SecretKeySpec for the specified key data and algorithm name.
  • Method Summary

    Modifier and Type Method Description
    boolean equals​(Object obj)
    Compares the specified object with this SecretKeySpec instance.
    String getAlgorithm()
    Returns the algorithm name.
    byte[] getEncoded()
    Returns the encoded form of this secret key.
    String getFormat()
    Returns the name of the format used to encode the key.
    int hashCode()
    Returns the hash code of this SecretKeySpec object.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SecretKeySpec

      public SecretKeySpec​(byte[] key, String algorithm)
      Creates a new SecretKeySpec for the specified key data and algorithm name.
      Parameters:
      key - the key data.
      algorithm - the algorithm name.
      Throws:
      IllegalArgumentException - if the key data or the algorithm name is null or if the key data is empty.
    • SecretKeySpec

      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.
      Parameters:
      key - the key data.
      offset - the offset.
      len - the size of the key data.
      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.
      ArrayIndexOutOfBoundsException - if offset or len is negative.
  • Method Details

    • getAlgorithm

      public String getAlgorithm()
      Returns the algorithm name.
      Specified by:
      getAlgorithm in interface Key
      Returns:
      the algorithm name.
    • getFormat

      public String getFormat()
      Returns the name of the format used to encode the key.
      Specified by:
      getFormat in interface Key
      Returns:
      the format name "RAW".
    • getEncoded

      public byte[] getEncoded()
      Returns the encoded form of this secret key.
      Specified by:
      getEncoded in interface Key
      Returns:
      the encoded form of this secret key.
    • hashCode

      public int hashCode()
      Returns the hash code of this SecretKeySpec object.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code.
      See Also:
      Object.equals(java.lang.Object)
    • equals

      public boolean equals​(Object obj)
      Compares the specified object with this SecretKeySpec instance.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare.
      Returns:
      true if the algorithm name and key of both object are equal, otherwise false.
      See Also:
      Object.hashCode()