EPCIS 2.0 Event Hash Generator

OpenEPCIS solution to generate unique event hash for EPCIS XML or JSON/JSON-LD document or event list.

The event hash generator is available as a web application, as an API and as source code for the core library, the command-line tool and the REST service:

Introduction

EPCIS events are created as objects move through the steps of a supply chain. They are sent to other organizations, stored in repositories, or used in other ways. Faulty hardware, poor implementations or human error sometimes cause the same event to be created more than once, and organizations usually want to neither send nor store such duplicates. That requires a way to identify each event by its content. The OpenEPCIS Event Hash Generator provides it: from the data contained in an EPCIS event, it computes a Hash-ID for that event.

Before hashing, the tool canonicalizes all event data and puts the properties in a fixed order. Two copies of the same event therefore always get the same Hash-ID and are recognized as duplicates, even when their attributes appear in a different order or their identifiers are written once as URN and once as Web URI. This lets organizations keep only unique data in their repositories and avoid the confusion that several copies of one event would cause. The tool currently supports the hash algorithms sha-256, sha-224, sha-384, sha-512, sha3-224, sha3-256 and sha3-512.

Usage

You can generate Hash-IDs in the browser, through the API or from the command line.

Web Application

In the web application you provide EPCIS events or documents in XML or JSON and get their Hash-IDs back. You can also display the pre-hash string and select several hash algorithms at once. The web application is available here.

API Endpoint

The Hash Generator API accepts an XML or JSON document and returns the generated Hash-IDs in its response. You can call it from your own application's code or try it online through Swagger-UI, which is available here.

Command Line

cURL (Client URL) is a widely used command-line tool for sending data to a server and receiving its response, and many developers prefer it to a web interface. To generate Hash-IDs with cURL, post a complete EPCIS document to https://tools.openepcis.io/api/generate/event-hash/document, or post a list of EPCIS events to https://tools.openepcis.io/api/generate/event-hash/events. The following request generates Hash-IDs for a JSON document:

curl -X 'POST' \
'https://tools.openepcis.io/api/generate/event-hash/document' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"@context": [
{
"example": "http://ns.example.com/epcis/"
}
],
"type": "EPCISDocument",
"schemaVersion": "2.0",
"creationDate": "2005-07-11T11:30:47.0Z",
"epcisBody": {
"eventList": [
{
"type": "ObjectEvent",
"eventTime": "2005-04-04T04:33:31.116+02:00",
"eventTimeZoneOffset": "-06:00",
"action": "ADD",
"bizStep": "commissioning",
"disposition": "in_transit",
"epcList":["urn:epc:id:grai:4012345.55555.987"],
"readPoint": {
"id": "urn:epc:id:sgln:0614141.07346.1234"
}
}
]
}
}'

Application Code

The generator is written in Java. Its code is published on the OpenEPCIS GitHub account, where you can read it directly or use it as a dependency in your own projects. The complete code is available here.

Generating Hash IDs from XML Document

For an EPCIS document in XML, pass it as an InputStream in the first parameter of the fromXml method. The second parameter selects the hash algorithm, which defaults to sha-256:

final InputStream xmlStream=getClass().getResourceAsStream("/XmlEpcisDocument.xml");
final EventHashGenerator eventHashGenerator=new EventHashGenerator();
        eventHashGenerator.prehashJoin("\\n");
final Multi<Map<String, String>>documentEventHash=eventHashGenerator.fromXml(xmlStream,"prehash","sha-256");
//final List<String> xmlHashIds = eventHashGenerator.fromXml(xmlStream, "sha-512").subscribe().asStream().toList();

Generating Hash IDs from JSON Document

For an EPCIS document in JSON/JSON-LD, pass it as an InputStream in the first parameter of the fromJson method. The second parameter selects the hash algorithm, which defaults to sha-256:

final InputStream jsonStream=getClass().getResourceAsStream("/JsonEpcisDocument.json");
final EventHashGenerator eventHashGenerator=new EventHashGenerator();
final Multi<Map<String, String>>jsonEventHash=eventHashGenerator.fromJson(jsonStream,"prehash","sha3-512");
//final List<String> jsonHashIds = eventHashGenerator.fromJson(jsonStream, "sha-256").subscribe().asStream().toList();

Subscription logic

A large EPCIS document in XML or JSON/JSON-LD with millions of events goes into the same fromXml or fromJson method of the EventHashGenerator class. Both methods return a reactive stream, so you can subscribe to it and print or process each Hash-ID as soon as it has been generated. The following code prints every Hash-ID to the console the moment it arrives. Processing starts with the first event and does not wait until the whole document has been hashed, which makes it much faster and more efficient.

final InputStream xmlStream=getClass().getResourceAsStream("/XmlEpcisDocument.xml");
final InputStream jsonStream=getClass().getResourceAsStream("/JsonEpcisDocument.json");
final EventHashGenerator eventHashGenerator=new EventHashGenerator();

        eventHashGenerator.fromXml(xmlStream,"sha-256").subscribe().with(
        xmlHashId->System.out.println(xmlHashId),
        failure->System.out.println("XML HashId Generation Failed with : "+failure));

        eventHashGenerator.fromJson(jsonStream,"sha-256").subscribe().with(
        jsonHashId->System.out.println(jsonHashId),
        failure->System.out.println("JSON HashId Generation Failed with "+failure));

Further References

Ralph Tröger's documentation on GitHub and the official GS1 EPCIS documentation explain event hash generation, the ordering of elements and canonicalization in detail.