Skip to content

Examples

All the examples follow the same procedure: they compute the document’s SHA-256 hash, send a request with a random nonce that asks for the service’s certificate, check that the response is for that hash and nonce, print the time and the serial number of the time stamp and save the response with the document. The address, the user name and the password are on the Integration tab of your service’s page; the examples use the test address. They read the user name and the password from the environment variables TSA_USER and TSA_PASSWORD.

Read the password from a secret in your environment (an environment variable, a secret store), not from the code.

Java 25, the Bouncy Castle library 1.87 (org.bouncycastle:bcpkix-jdk18on):

import java.math.BigInteger;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Base64;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.tsp.TimeStampRequest;
import org.bouncycastle.tsp.TimeStampRequestGenerator;
import org.bouncycastle.tsp.TimeStampResponse;
import org.bouncycastle.tsp.TimeStampToken;
public class TimeStampExample {
public static void main(String[] args) throws Exception {
String userName = System.getenv("TSA_USER");
String password = System.getenv("TSA_PASSWORD");
byte[] hash = MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(Path.of("document.pdf")));
TimeStampRequestGenerator generator = new TimeStampRequestGenerator();
generator.setCertReq(true);
TimeStampRequest request = generator.generate(NISTObjectIdentifiers.id_sha256, hash, new BigInteger(63, new SecureRandom()));
String basic = Base64.getEncoder().encodeToString((userName + ":" + password).getBytes(StandardCharsets.UTF_8));
HttpRequest http = HttpRequest.newBuilder(URI.create("https://tsa.test.certilia.com/qts"))
.header("Content-Type", "application/timestamp-query")
.header("Authorization", "Basic " + basic)
.POST(HttpRequest.BodyPublishers.ofByteArray(request.getEncoded()))
.build();
HttpResponse<byte[]> answer = HttpClient.newHttpClient().send(http, HttpResponse.BodyHandlers.ofByteArray());
if (answer.statusCode() != 200) throw new IllegalStateException("TSA: HTTP " + answer.statusCode());
TimeStampResponse response = new TimeStampResponse(answer.body());
// Throws TSPException if the response is not for this hash and nonce.
response.validate(request);
TimeStampToken token = response.getTimeStampToken();
if (token == null) throw new IllegalStateException("TSA: " + response.getStatusString());
System.out.println(token.getTimeStampInfo().getGenTime() + " " + token.getTimeStampInfo().getSerialNumber().toString(16));
Files.write(Path.of("document.pdf.tsr"), response.getEncoded());
}
}

The Go example also checks the time stamp’s signature with the certificate in the time stamp; the other examples do not check the signature. None of them checks the chain to AKD’s root certificate; this requires the certificate chain of AKD’s time-stamping service (see Archives and re-stamping).

For time stamps on signatures in Adobe Acrobat Reader, add a new time-stamp server in its settings (Edit → Preferences → Signatures → Document Timestamping → More… → Time Stamp Servers → New). Enter a name (e.g. Certilia), the service’s address, the user name and the password, then set it as the default (Set Default).