@@ -65,6 +65,7 @@ public class JcaContentSignerBuilder
6565
6666 private OperatorHelper helper = new OperatorHelper (new DefaultJcaJceHelper ());
6767 private SecureRandom random ;
68+ private DigestAlgorithmIdentifierFinder digestAlgIdFinder ;
6869
6970 private AlgorithmIdentifier sigAlgId ;
7071 private AlgorithmParameterSpec sigAlgSpec ;
@@ -78,6 +79,14 @@ public JcaContentSignerBuilder(String signatureAlgorithm, AlgorithmIdentifier si
7879 {
7980 this .signatureAlgorithm = signatureAlgorithm ;
8081 this .signatureDigestAlgorithm = signatureDigestAlgorithmID ;
82+ this .digestAlgIdFinder = null ;
83+ }
84+
85+ public JcaContentSignerBuilder (String signatureAlgorithm , DigestAlgorithmIdentifierFinder digestAlgIdFinder )
86+ {
87+ this .signatureAlgorithm = signatureAlgorithm ;
88+ this .signatureDigestAlgorithm = null ;
89+ this .digestAlgIdFinder = digestAlgIdFinder ;
8190 }
8291
8392 public JcaContentSignerBuilder (String signatureAlgorithm , AlgorithmParameterSpec sigParamSpec )
@@ -147,20 +156,7 @@ public ContentSigner build(PrivateKey privateKey)
147156 {
148157 if (sigAlgSpec == null )
149158 {
150- if (isAlgIdFromPrivate .contains (Strings .toUpperCase (signatureAlgorithm )))
151- {
152- this .sigAlgId = SIGNATURE_ALGORITHM_IDENTIFIER_FINDER .find (privateKey .getAlgorithm ());
153- if (this .sigAlgId == null )
154- {
155- this .sigAlgId = PrivateKeyInfo .getInstance (privateKey .getEncoded ()).getPrivateKeyAlgorithm ();
156- }
157- this .sigAlgSpec = null ;
158- }
159- else
160- {
161- this .sigAlgId = SIGNATURE_ALGORITHM_IDENTIFIER_FINDER .find (signatureAlgorithm );
162- this .sigAlgSpec = null ;
163- }
159+ this .sigAlgId = getSigAlgId (privateKey );
164160 }
165161 final AlgorithmIdentifier signatureAlgId = sigAlgId ;
166162 final Signature sig = helper .createSignature (sigAlgId );
@@ -201,11 +197,11 @@ public byte[] getSignature()
201197 }
202198 };
203199
204- if (signatureDigestAlgorithm != null )
200+ if (signatureDigestAlgorithm != null || digestAlgIdFinder != null )
205201 {
206202 return new ExtendedContentSigner ()
207203 {
208- private final AlgorithmIdentifier digestAlgorithm = signatureDigestAlgorithm ;
204+ private final AlgorithmIdentifier digestAlgorithm = ( signatureDigestAlgorithm != null ) ? signatureDigestAlgorithm : digestAlgIdFinder . find ( contentSigner . getAlgorithmIdentifier ()) ;
209205 private final ContentSigner signer = contentSigner ;
210206
211207 public AlgorithmIdentifier getDigestAlgorithmIdentifier ()
@@ -240,6 +236,23 @@ public byte[] getSignature()
240236 }
241237 }
242238
239+ private AlgorithmIdentifier getSigAlgId (PrivateKey privateKey )
240+ {
241+ if (isAlgIdFromPrivate .contains (Strings .toUpperCase (signatureAlgorithm )))
242+ {
243+ AlgorithmIdentifier sigAlgId = SIGNATURE_ALGORITHM_IDENTIFIER_FINDER .find (privateKey .getAlgorithm ());
244+ if (sigAlgId == null )
245+ {
246+ return PrivateKeyInfo .getInstance (privateKey .getEncoded ()).getPrivateKeyAlgorithm ();
247+ }
248+ return sigAlgId ;
249+ }
250+ else
251+ {
252+ return SIGNATURE_ALGORITHM_IDENTIFIER_FINDER .find (signatureAlgorithm );
253+ }
254+ }
255+
243256 private ContentSigner buildComposite (CompositePrivateKey privateKey )
244257 throws OperatorCreationException
245258 {
0 commit comments