public abstract class SignatureBuilder extends Object implements Serializable
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)
.
Constructor and Description |
---|
SignatureBuilder() |
Modifier and Type | Method and Description |
---|---|
static SignatureBuilder |
aSignature(Container container)
Create a new signature builder based on a container.
|
abstract DataToSign |
buildDataToSign()
Creates data to be signed externally.
|
Signature |
invokeSigning()
Invokes a signing process on the container with a signature token (See
SignatureToken ). |
abstract Signature |
openAdESSignature(byte[] signatureDocument)
Open signature from an existing signature document (XAdES, PAdES, CAdES etc.)
|
static void |
removeCustomSignatureBuilders()
Clears all custom signature builders to use only default signature builders.
|
static <T extends SignatureBuilder> |
setSignatureBuilderForContainerType(String containerType,
Class<T> signatureBuilderClass)
Setting custom signature builder implementation used when creating signatures for the particular container type.
|
SignatureBuilder |
withCity(String cityName)
Set a city to the signature production place.
|
SignatureBuilder |
withCountry(String country)
Set a country name to the signature production place.
|
SignatureBuilder |
withDataFileDigestAlgorithm(DigestAlgorithm digestAlgorithm)
Set data file digest algorithm.
|
SignatureBuilder |
withEncryptionAlgorithm(EncryptionAlgorithm encryptionAlgorithm)
Set encryption algorithm to be used in the signing process.
|
SignatureBuilder |
withOwnSignaturePolicy(eu.europa.esig.dss.model.Policy signaturePolicy)
Set signature policy parameters.
|
SignatureBuilder |
withPostalCode(String postalCode)
Set a postal code to the signature production place.
|
SignatureBuilder |
withRoles(String... roles)
Set roles to the signer.
|
SignatureBuilder |
withSignatureDigestAlgorithm(DigestAlgorithm digestAlgorithm)
Set signature digest algorithm used to generate a signature.
|
SignatureBuilder |
withSignatureId(String signatureId)
Set signature ID.
|
SignatureBuilder |
withSignatureProfile(SignatureProfile signatureProfile)
Set a signature profile: Time Stamp, Archive Time Stamp or no profile.
|
SignatureBuilder |
withSignatureToken(SignatureToken signatureToken)
Set signature token to be used in the signing process.
|
SignatureBuilder |
withSigningCertificate(X509Certificate certificate)
Set a signing certificate to be used when creating data to be signed.
|
SignatureBuilder |
withStateOrProvince(String stateOrProvince)
Set a state or province to the signature production place.
|
public static SignatureBuilder aSignature(Container container)
container
- container to be signed.public static <T extends SignatureBuilder> void setSignatureBuilderForContainerType(String containerType, Class<T> signatureBuilderClass)
T
- signature builder class extending SignatureBuilder
.containerType
- container type corresponding to the signature builder.signatureBuilderClass
- signature builder class used for creating signatures for the container type.public static void removeCustomSignatureBuilders()
public Signature invokeSigning() throws SignatureTokenMissingException
SignatureToken
).
Signature token must be provided with withSignatureToken(org.digidoc4j.SignatureToken)
.SignatureTokenMissingException
- if signature token is not provided with withSignatureToken(org.digidoc4j.SignatureToken)
SignatureToken
public abstract DataToSign buildDataToSign() throws SignerCertificateRequiredException, ContainerWithoutFilesException
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.
SignerCertificateRequiredException
- signer certificate must be provided using withSigningCertificate(X509Certificate)
ContainerWithoutFilesException
- container must have at least one data file to be signed. Signature cannot be given on an empty container.public abstract Signature openAdESSignature(byte[] signatureDocument)
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.
signatureDocument
- complete signature document in bytes.public SignatureBuilder withCity(String cityName)
cityName
- city to use on the signature production place.public SignatureBuilder withStateOrProvince(String stateOrProvince)
stateOrProvince
- name of the state or province on the signature production place.public SignatureBuilder withPostalCode(String postalCode)
postalCode
- postal code on the signature production place.public SignatureBuilder withCountry(String country)
country
- name of the country on the signature production place.public SignatureBuilder withRoles(String... roles)
roles
- list of roles of a signer.public SignatureBuilder withSignatureDigestAlgorithm(DigestAlgorithm digestAlgorithm)
digestAlgorithm
- signature digest algorithm.public SignatureBuilder withDataFileDigestAlgorithm(DigestAlgorithm digestAlgorithm)
digestAlgorithm
- datafile digest algorithm.public SignatureBuilder withSignatureProfile(SignatureProfile signatureProfile)
signatureProfile
- signature profile.public SignatureBuilder withSigningCertificate(X509Certificate certificate)
certificate
- X509 signer's certificate.public SignatureBuilder withSignatureId(String signatureId)
signatureId
- signature id.public SignatureBuilder withSignatureToken(SignatureToken signatureToken)
signatureToken
- signature token.public SignatureBuilder withEncryptionAlgorithm(EncryptionAlgorithm encryptionAlgorithm)
encryptionAlgorithm
- encryption algorithm.public SignatureBuilder withOwnSignaturePolicy(eu.europa.esig.dss.model.Policy signaturePolicy)
The default implementation throws NotSupportedException
.
signaturePolicy
- with defined parameters.Copyright © 2023. All rights reserved.