1
package org.bouncycastle.crypto.signers;
3
import org.bouncycastle.crypto.AsymmetricBlockCipher;
4
import org.bouncycastle.crypto.CipherParameters;
5
import org.bouncycastle.crypto.CryptoException;
6
import org.bouncycastle.crypto.DataLengthException;
7
import org.bouncycastle.crypto.Digest;
8
import org.bouncycastle.crypto.Signer;
9
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
10
import org.bouncycastle.crypto.params.ParametersWithRandom;
11
import org.bouncycastle.util.Arrays;
13
public class GenericSigner
16
private final AsymmetricBlockCipher engine;
17
private final Digest digest;
18
private boolean forSigning;
21
AsymmetricBlockCipher engine,
29
* initialise the signer for signing or verification.
32
* true if for signing, false otherwise
34
* necessary parameters.
38
CipherParameters parameters)
40
this.forSigning = forSigning;
41
AsymmetricKeyParameter k;
43
if (parameters instanceof ParametersWithRandom)
45
k = (AsymmetricKeyParameter)((ParametersWithRandom)parameters).getParameters();
49
k = (AsymmetricKeyParameter)parameters;
52
if (forSigning && !k.isPrivate())
54
throw new IllegalArgumentException("signing requires private key");
57
if (!forSigning && k.isPrivate())
59
throw new IllegalArgumentException("verification requires public key");
64
engine.init(forSigning, parameters);
68
* update the internal digest with the byte b
77
* update the internal digest with the byte array in
84
digest.update(input, inOff, length);
88
* Generate a signature for the message we've been loaded with using the key
89
* we were initialised with.
91
public byte[] generateSignature()
92
throws CryptoException, DataLengthException
96
throw new IllegalStateException("GenericSigner not initialised for signature generation.");
99
byte[] hash = new byte[digest.getDigestSize()];
100
digest.doFinal(hash, 0);
102
return engine.processBlock(hash, 0, hash.length);
106
* return true if the internal state represents the signature described in
107
* the passed in array.
109
public boolean verifySignature(
114
throw new IllegalStateException("GenericSigner not initialised for verification");
117
byte[] hash = new byte[digest.getDigestSize()];
118
digest.doFinal(hash, 0);
122
byte[] sig = engine.processBlock(signature, 0, signature.length);
124
return Arrays.constantTimeAreEqual(sig, hash);