Using blockchain technology to store documents or document hashes on a blockchain provides many advantages including easy validation and tamper resistance. In an older blog article, we explained how to store signed documents on a separate blockchain for each document.
The ASP.NET Core (.NET 6) sample project in this article is based on the same concept, but uses only one blockchain to store all transactions and to validate document hashes based on the integrity of previous blocks.
Each block in a blockchain is cryptographically linked to the previous block by hashing the previous block hash into the current block hash. The highly effective tamper resistance of a blockchain prevents document fraud and enables easy validation processes. Storing a complete document on a blockchain is technically possible, but file size limitations might prevent you from utilizing this strategy. Hashes are smaller in size and therefore a more efficient option to store documents on a blockchain. Every time a document is changed, the cryptographic hash will change. As the block hash contains the document data, the blockchain can be used to verify the document by validating the integrity of the blockchain.
In this sample, the hash of each block is created by hashing the following components:
- Previous block hash
- Data (our document hash)
A nonce ("number only used once") is a number added to the block hash that, when rehashed, meets the difficulty level restrictions.
If any component in the blockchain is tampered, the complete blockchain is invalid. In this sample, the Data that is stored is an MD5 hash of the signed document and additional information:
A simplified workflow is shown in the illustration below:
Storing the Document
This sample uses the TX Text Control DocumentViewer to sign a document. The signed PDF is stored as a hash on the blockchain and can be downloaded locally. After a document is stored, the resulting blockchain JSON looks similar this:
You can see that each block contains the PreviousBlockHash and the Data. The BlockHash itself is generated based on the PreviousBlockHash, the timestamp and the Data itself.
After signing the document, the created PDF can be validated by uploading it:
The method ValidateDocument creates an MD5 hash of the uploaded document and compares that value with the stored value in the block specified by the blockHash:
Before the hashes are compared, the integrity of the blockchain is checked by calling the IsValid method:
This method is literally looping through all blocks (until the current one), re-generates and compares the hashes for each block.
The sample also includes an overview of the current blockchain by displaying all entries and the stored data:
A blockchain can be used to store document hashes to validate the integrity of a document. A blockchain always contains information about the most current version of an uploaded or signed document.
You can download this sample application from our GitHub repository.