Example usage for org.bouncycastle.crypto.signers RSADigestSigner generateSignature

List of usage examples for org.bouncycastle.crypto.signers RSADigestSigner generateSignature

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.signers RSADigestSigner generateSignature.

Prototype

public byte[] generateSignature() throws CryptoException, DataLengthException 

Source Link

Document

Generate a signature for the message we've been loaded with using the key we were initialised with.

Usage

From source file:org.cryptacular.util.KeyPairUtil.java

License:Open Source License

/**
 * Determines whether the given RSA public and private keys form a proper key
 * pair by computing and verifying a digital signature with the keys.
 *
 * @param  pubKey  RSA public key./*from  w  w w  .  jav  a2s  .c om*/
 * @param  privKey  RSA private key.
 *
 * @return  True if the keys form a functioning keypair, false otherwise.
 *          Errors during signature verification are treated as false.
 */
public static boolean isKeyPair(final RSAPublicKey pubKey, final RSAPrivateKey privKey) {
    final RSADigestSigner signer = new RSADigestSigner(new SHA256Digest());
    signer.init(true, new RSAKeyParameters(true, privKey.getModulus(), privKey.getPrivateExponent()));
    signer.update(SIGN_BYTES, 0, SIGN_BYTES.length);
    try {
        final byte[] sig = signer.generateSignature();
        signer.init(false, new RSAKeyParameters(false, pubKey.getModulus(), pubKey.getPublicExponent()));
        signer.update(SIGN_BYTES, 0, SIGN_BYTES.length);
        return signer.verifySignature(sig);
    } catch (CryptoException e) {
        return false;
    }
}

From source file:org.diqube.ticket.TicketSignatureService.java

License:Open Source License

/**
 * Calculates the signature of a ticket and updates the given {@link Ticket} object directly.
 * /*  w w  w.  ja va 2  s. com*/
 * @throws IllegalStateException
 *           If ticket cannot be signed.
 */
public void signTicket(Ticket ticket) throws IllegalStateException {
    byte[] serialized = TicketUtil.serialize(ticket);
    byte[] claimBytes = TicketUtil.deserialize(ByteBuffer.wrap(serialized)).getRight();

    RSAPrivateCrtKeyParameters signingKey = keyManager.getPrivateSigningKey();

    if (signingKey == null)
        throw new IllegalStateException(
                "Cannot sign ticket because there is no private signing key available.");

    RSADigestSigner signer = new RSADigestSigner(new SHA256Digest());
    signer.init(true, signingKey);
    signer.update(claimBytes, 0, claimBytes.length);
    try {
        byte[] signature = signer.generateSignature();
        ticket.setSignature(signature);
    } catch (DataLengthException | CryptoException e) {
        throw new IllegalStateException("Cannot sign ticket", e);
    }
}