Package java.security

Class Signature


  • public abstract class Signature
    extends SignatureSpi
    Signature is an engine class which is capable of creating and verifying digital signatures, using different algorithms that have been registered with the Security class.
    See Also:
    SignatureSpi
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static int SIGN
      Constant that indicates that this Signature instance has been initialized for signing.
      protected int state
      Represents the current state of this Signature.
      protected static int UNINITIALIZED
      Constant that indicates that this Signature instance has not yet been initialized.
      protected static int VERIFY
      Constant that indicates that this Signature instance has been initialized for verification.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Signature​(String algorithm)
      Constructs a new instance of Signature with the name of the algorithm to use.
    • Field Detail

      • UNINITIALIZED

        protected static final int UNINITIALIZED
        Constant that indicates that this Signature instance has not yet been initialized.
        See Also:
        Constant Field Values
      • SIGN

        protected static final int SIGN
        Constant that indicates that this Signature instance has been initialized for signing.
        See Also:
        Constant Field Values
      • VERIFY

        protected static final int VERIFY
        Constant that indicates that this Signature instance has been initialized for verification.
        See Also:
        Constant Field Values
      • state

        protected int state
        Represents the current state of this Signature. The three possible states are UNINITIALIZED, SIGN or VERIFY.
    • Constructor Detail

      • Signature

        protected Signature​(String algorithm)
        Constructs a new instance of Signature with the name of the algorithm to use.
        Parameters:
        algorithm - the name of algorithm to use.
    • Method Detail

      • getInstance

        public static Signature getInstance​(String algorithm)
                                     throws NoSuchAlgorithmException
        Returns a new instance of Signature that utilizes the specified algorithm.
        Parameters:
        algorithm - the name of the algorithm to use.
        Returns:
        a new instance of Signature that utilizes the specified algorithm.
        Throws:
        NoSuchAlgorithmException - if the specified algorithm is not available.
        NullPointerException - if algorithm is null.
      • getInstance

        public static Signature getInstance​(String algorithm,
                                            Provider provider)
                                     throws NoSuchAlgorithmException
        Returns a new instance of Signature that utilizes the specified algorithm from the specified provider. The provider supplied does not have to be registered.
        Parameters:
        algorithm - the name of the algorithm to use.
        provider - the security provider.
        Returns:
        a new instance of Signature that utilizes the specified algorithm from the specified provider.
        Throws:
        NoSuchAlgorithmException - if the specified algorithm is not available.
        NullPointerException - if algorithm is null.
        IllegalArgumentException - if provider == null
      • getProvider

        public final Provider getProvider()
        Returns the provider associated with this Signature.
        Returns:
        the provider associated with this Signature.
      • getCurrentSpi

        public SignatureSpi getCurrentSpi()
        Returns the SignatureSpi backing this Signature or null if no SignatureSpi is backing this Signature.
      • getAlgorithm

        public final String getAlgorithm()
        Returns the name of the algorithm of this Signature.
        Returns:
        the name of the algorithm of this Signature.
      • initVerify

        public final void initVerify​(PublicKey publicKey)
                              throws InvalidKeyException
        Initializes this Signature instance for signature verification, using the public key of the identity whose signature is going to be verified.
        Parameters:
        publicKey - the public key.
        Throws:
        InvalidKeyException - if publicKey is not valid.
      • initVerify

        public final void initVerify​(Certificate certificate)
                              throws InvalidKeyException
        Initializes this Signature instance for signature verification, using the certificate of the identity whose signature is going to be verified.

        If the given certificate is an instance of X509Certificate and has a key usage parameter that indicates, that this certificate is not to be used for signing, an InvalidKeyException is thrown.

        Parameters:
        certificate - the certificate used to verify a signature.
        Throws:
        InvalidKeyException - if the publicKey in the certificate is not valid or not to be used for signing.
      • initSign

        public final void initSign​(PrivateKey privateKey)
                            throws InvalidKeyException
        Initializes this Signature instance for signing, using the private key of the identity whose signature is going to be generated.
        Parameters:
        privateKey - the private key.
        Throws:
        InvalidKeyException - if privateKey is not valid.
      • initSign

        public final void initSign​(PrivateKey privateKey,
                                   SecureRandom random)
                            throws InvalidKeyException
        Initializes this Signature instance for signing, using the private key of the identity whose signature is going to be generated and the specified source of randomness.
        Parameters:
        privateKey - the private key.
        random - the SecureRandom to use.
        Throws:
        InvalidKeyException - if privateKey is not valid.
      • sign

        public final byte[] sign()
                          throws SignatureException
        Generates and returns the signature of all updated data.

        This Signature instance is reset to the state of its last initialization for signing and thus can be used for another signature from the same identity.

        Returns:
        the signature of all updated data.
        Throws:
        SignatureException - if this Signature instance is not initialized properly.
      • sign

        public final int sign​(byte[] outbuf,
                              int offset,
                              int len)
                       throws SignatureException
        Generates and stores the signature of all updated data in the provided byte[] at the specified position with the specified length.

        This Signature instance is reset to the state of its last initialization for signing and thus can be used for another signature from the same identity.

        Parameters:
        outbuf - the buffer to store the signature.
        offset - the index of the first byte in outbuf to store.
        len - the number of bytes allocated for the signature.
        Returns:
        the number of bytes stored in outbuf.
        Throws:
        SignatureException - if this Signature instance is not initialized properly.
        IllegalArgumentException - if offset or len are not valid in respect to outbuf.
      • verify

        public final boolean verify​(byte[] signature)
                             throws SignatureException
        Indicates whether the given signature can be verified using the public key or a certificate of the signer.

        This Signature instance is reset to the state of its last initialization for verifying and thus can be used to verify another signature of the same signer.

        Parameters:
        signature - the signature to verify.
        Returns:
        true if the signature was verified, false otherwise.
        Throws:
        SignatureException - if this Signature instance is not initialized properly.
      • verify

        public final boolean verify​(byte[] signature,
                                    int offset,
                                    int length)
                             throws SignatureException
        Indicates whether the given signature starting at index offset with length bytes can be verified using the public key or a certificate of the signer.

        This Signature instance is reset to the state of its last initialization for verifying and thus can be used to verify another signature of the same signer.

        Parameters:
        signature - the byte[] containing the signature to verify.
        offset - the start index in signature of the signature.
        length - the number of bytes allocated for the signature.
        Returns:
        true if the signature was verified, false otherwise.
        Throws:
        SignatureException - if this Signature instance is not initialized properly.
        IllegalArgumentException - if offset or length are not valid in respect to signature.
      • update

        public final void update​(byte b)
                          throws SignatureException
        Updates the data to be verified or to be signed, using the specified byte.
        Parameters:
        b - the byte to update with.
        Throws:
        SignatureException - if this Signature instance is not initialized properly.
      • update

        public final void update​(byte[] data)
                          throws SignatureException
        Updates the data to be verified or to be signed, using the specified byte[].
        Parameters:
        data - the byte array to update with.
        Throws:
        SignatureException - if this Signature instance is not initialized properly.
      • update

        public final void update​(byte[] data,
                                 int off,
                                 int len)
                          throws SignatureException
        Updates the data to be verified or to be signed, using the given byte[], starting form the specified index for the specified length.
        Parameters:
        data - the byte array to update with.
        off - the start index in data of the data.
        len - the number of bytes to use.
        Throws:
        SignatureException - if this Signature instance is not initialized properly.
      • update

        public final void update​(ByteBuffer data)
                          throws SignatureException
        Updates the data to be verified or to be signed, using the specified ByteBuffer.
        Parameters:
        data - the ByteBuffer to update with.
        Throws:
        SignatureException - if this Signature instance is not initialized properly.
      • toString

        public String toString()
        Returns a string containing a concise, human-readable description of this Signature including its algorithm and its state.
        Overrides:
        toString in class Object
        Returns:
        a printable representation for this Signature.
      • getParameters

        public final AlgorithmParameters getParameters()
        Returns the AlgorithmParameters of this Signature instance.
        Returns:
        the AlgorithmParameters of this Signature instance, maybe null.
      • getParameter

        @Deprecated
        public final Object getParameter​(String param)
                                  throws InvalidParameterException
        Deprecated.
        There is no generally accepted parameter naming convention.
        Returns the value of the parameter with the specified name.
        Parameters:
        param - the name of the requested parameter value
        Returns:
        the value of the parameter with the specified name, maybe null.
        Throws:
        InvalidParameterException - if param is not a valid parameter for this Signature or an other error occurs.