blob: a7b45fcbfb636338753d00be7e18977474c16bab (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
package org.bouncycastle.operator.bc;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.util.Map;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.Signer;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.RuntimeOperatorException;
public abstract class BcContentSignerBuilder
{
private SecureRandom random;
private AlgorithmIdentifier sigAlgId;
private AlgorithmIdentifier digAlgId;
protected BcDigestProvider digestProvider;
public BcContentSignerBuilder(AlgorithmIdentifier sigAlgId, AlgorithmIdentifier digAlgId)
{
this.sigAlgId = sigAlgId;
this.digAlgId = digAlgId;
this.digestProvider = BcDefaultDigestProvider.INSTANCE;
}
public BcContentSignerBuilder setSecureRandom(SecureRandom random)
{
this.random = random;
return this;
}
public ContentSigner build(AsymmetricKeyParameter privateKey)
throws OperatorCreationException
{
final Signer sig = createSigner(sigAlgId, digAlgId);
if (random != null)
{
sig.init(true, new ParametersWithRandom(privateKey, random));
}
else
{
sig.init(true, privateKey);
}
return new ContentSigner()
{
private BcSignerOutputStream stream = new BcSignerOutputStream(sig);
public AlgorithmIdentifier getAlgorithmIdentifier()
{
return sigAlgId;
}
public OutputStream getOutputStream()
{
return stream;
}
public byte[] getSignature()
{
try
{
return stream.getSignature();
}
catch (CryptoException e)
{
throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e);
}
}
};
}
protected abstract Signer createSigner(AlgorithmIdentifier sigAlgId, AlgorithmIdentifier algorithmIdentifier)
throws OperatorCreationException;
}
|