Class SignatureBuilder
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
AsicSignatureBuilder
Creating signatures on a container.
Here's an example of creating a signature:
Signature
signature = SignatureBuilder
.
aSignature(container)
.
withCity("San Pedro")
.
withStateOrProvince("California")
.
withPostalCode("11111")
.
withCountry("Val Verde")
.
withRoles("Manager", "Suspicious Fisherman")
.
withSignatureDigestAlgorithm(DigestAlgorithm.SHA256)
. // Digest algorithm is SHA-256
withSignatureProfile(SignatureProfile.LT)
. // Signature profile is Time Stamp
withSigningCertificate(x509Certificate)
.
withSignatureId("1")
.
withSignatureToken(signatureToken)
. // Use signature token
withEncryptionAlgorithm(EncryptionAlgorithm.RSA)
. // Encryption algorithm is RSA
withOwnSignaturePolicy(policy)
.
invokeSigning()
; // Creates a signature using signature token
Use SignatureBuilder.aSignature(container)
to create a new signature builder,
populate the builder with data and then call invokeSigning()
to create a signature on the container
using SignatureToken
. Signature token must be provided with withSignatureToken(SignatureToken)
.
Use buildDataToSign()
to create DataToSign
object
that can be used in external signing (e.g. signing in the Web). To build DataToSign
object, signer certificate
must be provided with withSigningCertificate(X509Certificate)
.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic SignatureBuilder
aSignature
(Container container) Create a new signature builder based on a container.abstract DataToSign
Creates data to be signed externally.Invokes a signing process on the container with a signature token (SeeSignatureToken
).abstract Signature
openAdESSignature
(byte[] signatureDocument) Open signature from an existing signature document (XAdES, PAdES, CAdES etc.)static void
Clears all custom signature builders to use only default signature builders.static <T extends SignatureBuilder>
voidsetSignatureBuilderForContainerType
(String containerType, Class<T> signatureBuilderClass) Setting custom signature builder implementation used when creating signatures for the particular container type.Set a city to the signature production place.withCountry
(String country) Set a country name to the signature production place.withDataFileDigestAlgorithm
(DigestAlgorithm digestAlgorithm) Set data file digest algorithm.withEncryptionAlgorithm
(EncryptionAlgorithm encryptionAlgorithm) Set encryption algorithm to be used in the signing process.withOwnSignaturePolicy
(eu.europa.esig.dss.model.Policy signaturePolicy) Set signature policy parameters.withPostalCode
(String postalCode) Set a postal code to the signature production place.Set roles to the signer.withSignatureDigestAlgorithm
(DigestAlgorithm digestAlgorithm) Set signature digest algorithm used to generate a signature.withSignatureId
(String signatureId) Set signature ID.withSignatureProfile
(SignatureProfile signatureProfile) Set a signature profile: Time Stamp, Archive Time Stamp or no profile.withSignatureToken
(SignatureToken signatureToken) Set signature token to be used in the signing process.withSigningCertificate
(X509Certificate certificate) Set a signing certificate to be used when creating data to be signed.withStateOrProvince
(String stateOrProvince) Set a state or province to the signature production place.
-
Constructor Details
-
SignatureBuilder
public SignatureBuilder()
-
-
Method Details
-
aSignature
Create a new signature builder based on a container. Container is used to determine which type of signature should be created.- Parameters:
container
- container to be signed.- Returns:
- builder for creating a signature.
-
setSignatureBuilderForContainerType
public static <T extends SignatureBuilder> void setSignatureBuilderForContainerType(String containerType, Class<T> signatureBuilderClass) Setting custom signature builder implementation used when creating signatures for the particular container type.- Type Parameters:
T
- signature builder class extendingSignatureBuilder
.- Parameters:
containerType
- container type corresponding to the signature builder.signatureBuilderClass
- signature builder class used for creating signatures for the container type.
-
removeCustomSignatureBuilders
public static void removeCustomSignatureBuilders()Clears all custom signature builders to use only default signature builders. -
invokeSigning
Invokes a signing process on the container with a signature token (SeeSignatureToken
). Signature token must be provided withwithSignatureToken(org.digidoc4j.SignatureToken)
.- Returns:
- a new signature on the container.
- Throws:
SignatureTokenMissingException
- if signature token is not provided withwithSignatureToken(org.digidoc4j.SignatureToken)
- See Also:
-
buildDataToSign
public abstract DataToSign buildDataToSign() throws SignerCertificateRequiredException, ContainerWithoutFilesExceptionCreates data to be signed externally.If the signing process involves signing the container externally (e.g. signing in the Web by a browser plugin), then
DataToSign
provides necessary data for creating a signature externally.- Returns:
- data to be signed externally.
- Throws:
SignerCertificateRequiredException
- signer certificate must be provided usingwithSigningCertificate(X509Certificate)
ContainerWithoutFilesException
- container must have at least one data file to be signed. Signature cannot be given on an empty container.
-
openAdESSignature
Open signature from an existing signature document (XAdES, PAdES, CAdES etc.)The signature document must be complete, containing all the necessary data (e.g. Signer's certificate, OCSP responses, Timestamps, signature values etc). An example would be a signature document in XAdES format which is an XML document transformed into a byte array.
- Parameters:
signatureDocument
- complete signature document in bytes.- Returns:
- a signature object representing the signatureDocument.
-
withCity
Set a city to the signature production place.- Parameters:
cityName
- city to use on the signature production place.- Returns:
- builder for creating a signature
-
withStateOrProvince
Set a state or province to the signature production place.- Parameters:
stateOrProvince
- name of the state or province on the signature production place.- Returns:
- builder for creating a signature
-
withPostalCode
Set a postal code to the signature production place.- Parameters:
postalCode
- postal code on the signature production place.- Returns:
- builder for creating a signature.
-
withCountry
Set a country name to the signature production place.- Parameters:
country
- name of the country on the signature production place.- Returns:
- builder for creating a signature.
-
withRoles
Set roles to the signer.- Parameters:
roles
- list of roles of a signer.- Returns:
- builder for creating a signature.
-
withSignatureDigestAlgorithm
Set signature digest algorithm used to generate a signature.- Parameters:
digestAlgorithm
- signature digest algorithm.- Returns:
- builder for creating a signature.
-
withDataFileDigestAlgorithm
Set data file digest algorithm.- Parameters:
digestAlgorithm
- datafile digest algorithm.- Returns:
- builder for creating a signature.
-
withSignatureProfile
Set a signature profile: Time Stamp, Archive Time Stamp or no profile. Default is Time Stamp.- Parameters:
signatureProfile
- signature profile.- Returns:
- builder for creating a signature.
-
withSigningCertificate
Set a signing certificate to be used when creating data to be signed.- Parameters:
certificate
- X509 signer's certificate.- Returns:
- builder for creating a signature.
-
withSignatureId
Set signature ID.- Parameters:
signatureId
- signature id.- Returns:
- builder for creating a signature.
-
withSignatureToken
Set signature token to be used in the signing process.- Parameters:
signatureToken
- signature token.- Returns:
- builder for creating a signature.
-
withEncryptionAlgorithm
Set encryption algorithm to be used in the signing process.- Parameters:
encryptionAlgorithm
- encryption algorithm.- Returns:
- builder for creating a signature.
-
withOwnSignaturePolicy
Set signature policy parameters.The default implementation throws
NotSupportedException
.- Parameters:
signaturePolicy
- with defined parameters.- Returns:
- SignatureBuilder
-