-
Notifications
You must be signed in to change notification settings - Fork 1
Examples of using it
Jaan Murumets edited this page Aug 27, 2018
·
9 revisions
Here is a example of using Digidoc4J Smart-ID adapter with DigiDoc4J
First we configure the Smart-ID client:
// Client setup. Note that these values are demo environment specific.
SmartIdClient client = new SmartIdClient();
client.setRelyingPartyUUID("00000000-0000-0000-0000-000000000000");
client.setRelyingPartyName("DEMO");
client.setHostUrl("https://sid.demo.sk.ee/smart-id-rp/v1/");
Then we create a SmartIdSignatureToken
instance:
NationalIdentity identity = new NationalIdentity("EE", "31111111111"); // identity of the signer
SmartIdSignatureToken smartIdSignatureToken = new SmartIdSignatureToken(client, identity);
Now we can use the SmartIdSignatureToken
instance along with DigiDoc4J to create and sign a BDOC container:
// For Smart-ID Basic (ADVANCED) signatures add issuer to trusted list, see also https://github.com/SK-EID/smart-id-documentation/wiki/Environment-technical-parameters#smart-id-basic-advanced-level-accounts
Configuration configuration = Configuration.of(Configuration.Mode.PROD);
configuration.getTSL().addTSLCertificate(Helper.loadCertificate("path/TEST_of_EID-SK_2016.der.crt"));
// To get SK root certificates please refer to https://sk.ee/en/repository/certs/
//Create a container with a text file to be signed
Container container = ContainerBuilder.
aContainer().
withConfiguration(configuration).
withDataFile("testFiles/legal_contract_1.txt", "text/plain").
build();
// Get the signer's certificate
X509Certificate signingCert = smartIdSignatureToken.getCertificate()
// Get the data to be signed by the user
DataToSign dataToSign = SignatureBuilder.
aSignature(container).
withSigningCertificate(signingCert).
withSignatureDigestAlgorithm(DigestAlgorithm.SHA256).
buildDataToSign();
// Data to sign contains the digest that should be signed
byte[] digestToSign = dataToSign.getDigestToSign();
// Data to sign contains the digest that should be signed starting digidoc4j version 2.x
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] digestToSign = digest.digest(dataToSign.getDataToSign());
// Calculate the Smart-ID verification code to display on the web page or e-service
String verificationCode = VerificationCodeCalculator.calculate(digestToSign);
// Sign the digest
byte[] signatureValue = smartIdSignatureToken.signDigest(DigestAlgorithm.SHA256, digestToSign);
// Finalize the signature with OCSP response and timestamp (or timemark)
Signature signature = dataToSign.finalize(signatureValue);
// Add signature to the container
container.addSignature(signature);
For testing in demo environment TEST TSL must be used:
https://github.com/open-eid/digidoc4j/wiki/Using-test-TSL-lists