Example usage for java.security Signature getProvider

List of usage examples for java.security Signature getProvider

Introduction

In this page you can find the example usage for java.security Signature getProvider.

Prototype

public final Provider getProvider() 

Source Link

Document

Returns the provider of this signature object.

Usage

From source file:com.alfaariss.oa.engine.core.crypto.CryptoManager.java

/**
 * Retrieve an instance of the configured {@link Signature}.    
 * @return The configured type of Signature.
 * @throws CryptoException If creation fails. 
 * @see <a href="http://java.sun.com/j2se/1.5.0/docs/guide/security/CryptoSpec.html#Signature">
 *  The Signature Class</a>/*ww  w. j a  v  a 2  s  . c om*/
 */
public Signature getSignature() throws CryptoException {
    Signature signature = null;
    if (_sSigningAlgorithm != null) //Signing enabled
    {
        try {
            if (_sSigningProvider != null)
                signature = Signature.getInstance(_sSigningAlgorithm, _sSigningProvider);
            else
                signature = Signature.getInstance(_sSigningAlgorithm);
        } catch (NoSuchAlgorithmException e) {
            _logger.error("Invalid signature algorithm", e);
            throw new CryptoException(SystemErrors.ERROR_INTERNAL);
        } catch (NoSuchProviderException e) {
            _logger.error("Invalid signature provider", e);
            throw new CryptoException(SystemErrors.ERROR_INTERNAL);
        }

        _logger.debug("Established Signature instance of provider " + signature.getProvider().getName());
    } else
        _logger.debug("Signing disabled");

    return signature;
}

From source file:test.integ.be.fedict.commons.eid.client.JCATest.java

private void verifySignatureAlgorithm(final String signatureAlgorithm, final PrivateKey privateKey,
        final PublicKey publicKey) throws Exception {
    Signature signature = Signature.getInstance(signatureAlgorithm);
    signature.initSign(privateKey);/*from w w w  .j a  v  a  2 s . c om*/
    assertTrue(signature.getProvider() instanceof BeIDProvider);

    final byte[] toBeSigned = "hello world".getBytes();
    signature.update(toBeSigned);
    final byte[] signatureValue = signature.sign();
    assertNotNull(signatureValue);

    signature.initVerify(publicKey);
    signature.update(toBeSigned);
    final boolean beIDResult = signature.verify(signatureValue);
    assertTrue(beIDResult);

    signature = Signature.getInstance(signatureAlgorithm);
    signature.initVerify(publicKey);
    signature.update(toBeSigned);
    final boolean result = signature.verify(signatureValue);
    assertTrue(result);

    RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
    BigInteger signatureValueBigInteger = new BigInteger(signatureValue);
    BigInteger messageBigInteger = signatureValueBigInteger.modPow(rsaPublicKey.getPublicExponent(),
            rsaPublicKey.getModulus());
    LOG.debug("Padded DigestInfo: " + new String(Hex.encodeHex(messageBigInteger.toByteArray())));
}