PDF Stamping Guide


Contents

1. Instroduction and Target

2. Supporting Environment

3. Prepare PDF Stamping

4. Start PDF Stamping with Sample

4.1 Guide
4.2 Feature
4.2.1 Register Stamping
4.2.2 Certificate Stamping
4.3 Sample
4.3.1 Client Side

5. PDF Stamping API


1. Instroduction and Target

This document is written for clients to hope to establish Time Stemping Authority, TSA, in a new or existing service. Contents in this document is written for a customer who can understand programming and services using SDK. Before reading this document, you may need to understand what is Blockchain, Web Application Server, Application, Transport Layer Security, HTTPS, Certificate sign & verification.

2. Supporting Environment

  • Java
    • Android
    • Tomcat
    • Jeus
    • JSP
    • Any Server supporting Java

3. Prepare PDF Stamping

  • Install Coinstack SDK, PDF Stamping library file WAS
    • JSP: tsagateway.war
    • JAR: coinstack-3.0.23.jar
  • Append below to JSP configuration in web.xml
<init-param>
    <param-name>publicKey</param-name>
    <param-value>1Eq1n3oEuZ7GwUURB86TeNePHobnjAE2eY</param-value>
</init-param>
<init-param>
    <param-name>privateKeyWIF</param-name>
    <param-value>L1Dc3RULZisMKGg9jTXwFS9ubxU9NBN2jLhjjhRidNd8kz5NRjja</param-value>
</init-param>
<init-param>
    <param-name>sdk_endpoint</param-name>
    <param-value>http://testchain.blocko.io</param-value>
</init-param>

4. Start PDF Stamping with Sample

4.1 Guide

Extract a hash value of PDF file and register it in Blockchain. You can check time when the file is registered.

4.2 How to Use
4.2.1 Register Stamping
  1. Extract File hash
  2. Register File hash
4.2.2 Certificate Stamping
  1. Certificate File and Stamping hash
4.3 Sample
4.3.1 Client Side
import io.blocko.coinstack.util.PDFController;
import io.blocko.coinstack.util.TSAGatewayClient;

...

try {
    inputstream raw = this.getapplicationcontext().getassets().open("test.pdf");
    final pdfcontroller controller = new pdfcontroller(raw);
    system.out.println(controller.calculatehash());
    tsagatewayclientasync client = new tsagatewayclientasync(tsagateway);
    client.requeststamp(controller.calculatehash(), new tsagatewayclientasync.callback() {
        @override
        public void onsuccess(string stamp) {
            file tempfile = null;
            try {
                tempfile = file.createtempfile("tempfile", "pdf");
                controller.writestampid(tempfile, stamp);
                system.out.println("complete");
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
        @override
        public void ontsafailure(tsagatewayexception e) {
            system.out.println("tsa failure");
            e.printstacktrace();
            int errorcode = e.geterrorcode();
            system.out.println(errorcode);
            // error code
            //    410 = hash format
            //    411 = gateway error
            //    412 = blockchain error
            //    420 = internal error
        }
        @override
        public void oniofailure(ioexception e) {
            system.out.println("io failure");
            e.printstacktrace();
        }
    });
} catch (IOException e) {
    e.printStackTrace();
}
...
4.3.2 Server Side
import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

import org.junit.Before;
import org.junit.Test;

import io.blocko.coinstack.CoinStackClient;
import io.blocko.coinstack.exception.CoinStackException;

public class TestStamp {
    public static String PATH_SCRATCH = "src/test/resources/scratch/";

    CoinStackClient coinstack = null;
    char[] pkey = null;

    @Before
    public void before() {
        coinstack = CoinStackManager.getInstance().getCoinStack();
        pkey = CoinStackManager.getInstance().getKey();
    }

    @Test
    public void main() throws Exception {
        String original = PATH_SCRATCH + "test-original.pdf";
        String stamped = PATH_SCRATCH + "test-stamped.pdf";
        stamped = appendDateTimeStr(stamped, ".pdf", System.currentTimeMillis());
        System.out.println("original="+original);
        System.out.println("stamped="+stamped);

        testWriteStampId(original, stamped);
        testVerifyStampId(stamped);
    }

    public static String appendDateTimeStr(String source, String ext, long time) {
        String dt = new SimpleDateFormat("yyyyMMdd-HHmmss").format(System.currentTimeMillis());
        return source.replace(ext, "-"+dt+ext);
    }

    public String stampDocument(String hash) throws IOException, CoinStackException {
        String stampId = StampClient.stampDocument(coinstack, pkey, hash);
        //String stampId = coinstack.stampDocument(hash); // cloudstamp
        return stampId;
    }

    public String getStampData(String stampId) throws IOException, CoinStackException {
        String stampHash = StampClient.getStampData(coinstack, stampId);
        //String stampHash = coinstack.getStampIndirect(stampId).getHash();
        return stampHash;
    }

    public void testWriteStampId(String src, String res) throws IOException, CoinStackException {
        PDFController controller = new PDFController(new File(src));
        String hash = controller.calculateHash();
        String stampId = stampDocument(hash);
        System.out.printf("[%10s] hash=%s, stampId=%s\n", "original", hash, stampId);
        controller.writeStampId(new File(res), stampId);
        controller.close();
    }

    public void testVerifyStampId(String src) throws IOException, CoinStackException {
        PDFController controller = new PDFController(new File(src));
        if (!controller.hasStampId()) {
            System.out.printf("[%10s] hash=%s, (stamp not found)\n", "stamped", controller.calculateHash());
            controller.close();
            return;
        }
        String hash = controller.calculateHashStamped();
        String stampId = controller.getStampId();
        System.out.printf("[%10s] hash=%s, stampId=%s\n", "stamped", hash, stampId);
        controller.close();

        String blockchainHash = getStampData(stampId);
        System.out.printf("[%10s] hash=%s\n", "blockchain", blockchainHash);
        assertEquals(blockchainHash, hash);
    }
}

5. PDF Stamping API

  • Java
Class StampClient - Register & Certificate Stamping
Constructor
Static use
Method
String stampDocument(CoinStackClient coinstack, char[] privateKeyWIF, String documentHash) - Register file Stamping
String stampDocument(CoinStackClient coinstack, String privateKeyWIF, String documentHash) - Register file Stamping
String getStampData(CoinStackClient coinstack, String stampId) - Check Stamping hash value in Blockchain
Class PDFController - Control PDF
Constructor
PDFController(byte[] input)
PDFController(InputStream input)
PDFController(File input)
Method
boolean hasStampId() - Check Stamping ID of PDF file existence
String getStampId() - Get Stamping ID of PDF file
String calculateHashStamped() - Get hash value of stamped PDF file
String calculateHash() - Get hash value of PDF file
void writeStampId(File out, String stampId) - Write Stampe ID in PDF file
void writeStampId(OutputStream out, String stampId) - Write Stampe ID in PDFfile
void writeStampId(OutputStream out, String stampId, SignatureOptions options) - Write Stamp ID in PDF file

results matching ""

    No results matching ""