java.security

Class Signature

    • Field Summary

      Fields 
      Modifier and Type Field and Description
      protected static int SIGN
      Possible state value which signifies that this instance has been initialized for signing purposes.
      protected int state
      Current sate of this instance.
      protected static int UNINITIALIZED
      Possible state value which signifies that this instance has not yet been initialized.
      protected static int VERIFY
      Possible state value which signifies that this instance has been initialized for verification purposes.
    • Constructor Summary

      Constructors 
      Modifier Constructor and Description
      protected Signature(java.lang.String algorithm)
      Constructs a new Signature instance for a designated digital signature algorithm.
    • Method Summary

      Methods 
      Modifier and Type Method and Description
      java.lang.Object clone()
      Returns a clone of this instance.
      java.lang.String getAlgorithm()
      Returns the name of the algorithm currently used.
      static Signature getInstance(java.lang.String algorithm)
      Returns an instance of Signature representing the specified signature.
      static Signature getInstance(java.lang.String algorithm, Provider provider)
      Returns an instance of Signature representing the specified signature from the specified Provider.
      static Signature getInstance(java.lang.String algorithm, java.lang.String provider)
      Returns an instance of Signature representing the specified signature from the named provider.
      java.lang.Object getParameter(java.lang.String param)
      Deprecated. 
      use the other getParameter
      AlgorithmParameters getParameters()
      Return the parameters of the algorithm used in this instance as an AlgorithmParameters.
      Provider getProvider()
      Returns the Provider of this instance.
      void initSign(PrivateKey privateKey)
      Initializes this class with the private key for signing purposes.
      void initSign(PrivateKey privateKey, SecureRandom random)
      Initializes this class with the private key and source of randomness for signing purposes.
      void initVerify(java.security.cert.Certificate certificate)
      Verify a signature with a designated Certificate.
      void initVerify(PublicKey publicKey)
      Initializes this instance with the public key for verification purposes.
      void setParameter(java.security.spec.AlgorithmParameterSpec params)
      Sets the signature engine with the specified AlgorithmParameterSpec.
      void setParameter(java.lang.String param, java.lang.Object value)
      Deprecated. 
      use the other setParameter
      byte[] sign()
      Returns the signature bytes of all the data fed to this instance.
      int sign(byte[] outbuf, int offset, int len)
      Generates signature bytes of all the data fed to this instance and stores it in the designated array.
      java.lang.String toString()
      Returns a rstring representation of this instance.
      void update(byte b)
      Updates the data to be signed or verified with the specified byte.
      void update(byte[] data)
      Updates the data to be signed or verified with the specified bytes.
      void update(byte[] data, int off, int len)
      Updates the data to be signed or verified with the specified bytes.
      void update(java.nio.ByteBuffer input)
      Update this signature with the Buffer.remaining() bytes of the input buffer.
      boolean verify(byte[] signature)
      Verifies a designated signature.
      boolean verify(byte[] signature, int offset, int length)
      Verifies a designated signature.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • UNINITIALIZED

        protected static final int UNINITIALIZED
        Possible state value which signifies that this instance has not yet been initialized.
        See Also:
        Constant Field Values
      • SIGN

        protected static final int SIGN
        Possible state value which signifies that this instance has been initialized for signing purposes.
        See Also:
        Constant Field Values
      • VERIFY

        protected static final int VERIFY
        Possible state value which signifies that this instance has been initialized for verification purposes.
        See Also:
        Constant Field Values
      • state

        protected int state
        Current sate of this instance.
    • Constructor Detail

      • Signature

        protected Signature(java.lang.String algorithm)
        Constructs a new Signature instance for a designated digital signature algorithm.
        Parameters:
        algorithm - the algorithm to use.
    • Method Detail

      • getInstance

        public static Signature getInstance(java.lang.String algorithm)
                                     throws NoSuchAlgorithmException
        Returns an instance of Signature representing the specified signature.
        Parameters:
        algorithm - the algorithm to use.
        Returns:
        a new instance repesenting the desired algorithm.
        Throws:
        NoSuchAlgorithmException - if the algorithm is not implemented by any provider.
        java.lang.IllegalArgumentException - if algorithm is null or is an empty string.
      • getInstance

        public static Signature getInstance(java.lang.String algorithm,
                            java.lang.String provider)
                                     throws NoSuchAlgorithmException,
                                            NoSuchProviderException
        Returns an instance of Signature representing the specified signature from the named provider.
        Parameters:
        algorithm - the algorithm to use.
        provider - the name of the provider to use.
        Returns:
        a new instance repesenting the desired algorithm.
        Throws:
        NoSuchProviderException - if the named provider was not found.
        NoSuchAlgorithmException - if the algorithm is not implemented by the named provider.
        java.lang.IllegalArgumentException - if either algorithm or provider is null or empty.
      • getInstance

        public static Signature getInstance(java.lang.String algorithm,
                            Provider provider)
                                     throws NoSuchAlgorithmException
        Returns an instance of Signature representing the specified signature from the specified Provider.
        Parameters:
        algorithm - the algorithm to use.
        provider - the Provider to use.
        Returns:
        a new instance repesenting the desired algorithm.
        Throws:
        NoSuchAlgorithmException - if the algorithm is not implemented by the Provider.
        java.lang.IllegalArgumentException - if either algorithm or provider is null, or if algorithm is an empty string.
      • initVerify

        public final void initVerify(java.security.cert.Certificate certificate)
                              throws InvalidKeyException
        Verify a signature with a designated Certificate. This is a FIPS 140-1 compatible method since it verifies a signature with a certificate.

        If the Certificate is an X.509 one, has a KeyUsage parameter and that parameter indicates this key is not to be used for signing then an exception is thrown.

        Parameters:
        certificate - a Certificate containing a public key to verify with.
        Throws:
        InvalidKeyException - if the key is invalid.
      • sign

        public final byte[] sign()
                          throws SignatureException
        Returns the signature bytes of all the data fed to this instance. The format of the output depends on the underlying signature algorithm.
        Returns:
        the signature bytes.
        Throws:
        SignatureException - if the engine is not properly initialized.
      • sign

        public final int sign(byte[] outbuf,
               int offset,
               int len)
                       throws SignatureException
        Generates signature bytes of all the data fed to this instance and stores it in the designated array. The format of the result depends on the underlying signature algorithm.

        After calling this method, the instance is reset to its initial state and can then be used to generate additional signatures.

        IMPLEMENTATION NOTE: Neither this method nor the GNU provider will return partial digests. If len is less than the signature length, this method will throw a SignatureException. If it is greater than or equal then it is ignored.

        Parameters:
        outbuf - array of bytes of where to store the resulting signature bytes.
        offset - the offset to start at in the array.
        len - the number of the bytes to use in the array.
        Returns:
        the real number of bytes used.
        Throws:
        SignatureException - if the engine is not properly initialized.
        Since:
        1.2
      • verify

        public final boolean verify(byte[] signature)
                             throws SignatureException
        Verifies a designated signature.
        Parameters:
        signature - the signature bytes to verify.
        Returns:
        true if verified, false otherwise.
        Throws:
        SignatureException - if the engine is not properly initialized or the signature does not check.
      • verify

        public final boolean verify(byte[] signature,
                     int offset,
                     int length)
                             throws SignatureException
        Verifies a designated signature.
        Parameters:
        signature - the signature bytes to verify.
        offset - the offset to start at in the array.
        length - the number of the bytes to use from the array.
        Returns:
        true if verified, false otherwise.
        Throws:
        java.lang.IllegalArgumentException - if the signature byte array is null, or the offset or length is less than 0, or the sum of the offset and length is greater than the length of the signature byte array.
        SignatureException - if the engine is not properly initialized or the signature does not check.
      • update

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

        public final void update(byte[] data)
                          throws SignatureException
        Updates the data to be signed or verified with the specified bytes.
        Parameters:
        data - the array of bytes to use.
        Throws:
        SignatureException - if the engine is not properly initialized.
      • update

        public final void update(byte[] data,
                  int off,
                  int len)
                          throws SignatureException
        Updates the data to be signed or verified with the specified bytes.
        Parameters:
        data - an array of bytes to use.
        off - the offset to start at in the array.
        len - the number of bytes to use from the array.
        Throws:
        SignatureException - if the engine is not properly initialized.
      • update

        public final void update(java.nio.ByteBuffer input)
                          throws SignatureException
        Update this signature with the Buffer.remaining() bytes of the input buffer.
        Parameters:
        input - The input buffer.
        Throws:
        SignatureException - If this instance was not properly initialized.
      • getAlgorithm

        public final java.lang.String getAlgorithm()
        Returns the name of the algorithm currently used. The names of algorithms are usually SHA/DSA or SHA/RSA.
        Returns:
        name of algorithm.
      • toString

        public java.lang.String toString()
        Returns a rstring representation of this instance.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a rstring representation of this instance.
        See Also:
        Object.getClass(), Object.hashCode(), Class.getName(), Integer.toHexString(int)
      • setParameter

        public final void setParameter(java.lang.String param,
                        java.lang.Object value)
                                throws InvalidParameterException
        Deprecated. use the other setParameter
        Sets the specified algorithm parameter to the specified value.
        Parameters:
        param - the parameter name.
        value - the parameter value.
        Throws:
        InvalidParameterException - if the parameter is invalid, the parameter is already set and can not be changed, a security exception occured, etc.
      • setParameter

        public final void setParameter(java.security.spec.AlgorithmParameterSpec params)
                                throws InvalidAlgorithmParameterException
        Sets the signature engine with the specified AlgorithmParameterSpec.

        By default, and unless overriden by the concrete SPI, this method always throws an UnsupportedOperationException.

        Parameters:
        params - the parameters to use for intializing this instance.
        Throws:
        InvalidParameterException - if the parameter is invalid, the parameter is already set and cannot be changed, a security exception occured, etc.
        InvalidAlgorithmParameterException
      • getParameters

        public final AlgorithmParameters getParameters()
        Return the parameters of the algorithm used in this instance as an AlgorithmParameters.
        Returns:
        the parameters used with this instance, or null if this instance does not use any parameters.
      • getParameter

        public final java.lang.Object getParameter(java.lang.String param)
                                            throws InvalidParameterException
        Deprecated. use the other getParameter
        Returns the value for the specified algorithm parameter.
        Parameters:
        param - the parameter name.
        Returns:
        the parameter value.
        Throws:
        InvalidParameterException - if the parameter is invalid.
      • clone

        public java.lang.Object clone()
                               throws java.lang.CloneNotSupportedException
        Returns a clone of this instance.
        Overrides:
        clone in class SignatureSpi
        Returns:
        a clone of this instace.
        Throws:
        java.lang.CloneNotSupportedException - if the implementation does not support cloning.
        See Also:
        Cloneable