Blockchain – GoLang and Crypto Digital Signatures

September 13, 2018

Author: Srinivasan Swaminathan

Tutorial - MLG Blockchain

GoLang and Crypto Digital Signatures

Blockchain is a tamper-proof distributed record of transactions that is maintained by a network of computers and secured through advanced cryptography to improve the efficiency of worldwide financial transactions and to transform the global financial network.

The three key blockchain features are: transparency, decentralized structure and multi-signature. In the absence of trusted third parties, the security and maintenance of the system is a shared responsibility. In peer-to-peer networking technology, if there is no proper mathematical encryption or security, then it is not autonomous.

This is an article to explain how data transmission is encrypted and shared between both the parties who do not know each other but are able to trust each other through the blockchain.

Encryption and Hash Functions

Public and private key cryptography is a good place to start. It involves the transformation of a message, or plain text, in such a way as to render the text unreadable without a special key. When the same key is used for both encryption and decryption, the process is known as symmetric, or secret key, encryption; asymmetric, or public key, encryption uses a pair of keys for encryption and decryption, one public and the other private.

A very common use case to use them is to prove your identity. You place your public key where anyone can see it, then use the private one to later confirm you are who you say you are.

  • Step 1: – Generate a public/private key pair
  • Step 2: – Hash the document to get signed
  • Step 3: – Encrypt document with private key for digital sign
  • Step 4: – Append the signature with the document
  • Step 5: – Place the document on the blockchain

Generate Public / Private Key Pair

We can see how to create private key and generate public key practically with ‘Golang’, Go has crypto/rsa standard libraryto use.

Generate Private Key of 2048 bits length

This can be further encoded with crypto/X.509 standard format which is used to encode keys and digital certificates.

Encoded with crypto/X.509

You can now use your private key to generate a public key.

Use your private key to generate a public key

This can be further encoded with package encoding/asn1 format which serialises a public key to DER-encoded format.

Serialize public key to DER-encoded format

Public Key Cryptography

Hash the messages/document and generate signature and verify.

There is a digital signature in a method of preventing “denial” and verifying the message. A digital signature is an application of public key cryptography, a message sender signs with a secret key, and a recipient verifies with a public key. That is, even a third party can verify the transmission of the message.

In the Go language, digital signature by DSA (Digital Signature Algorithm), RSA, elliptic curve cryptography is implemented as standard. Here’s what it looks like using crypto/rsa – messages are hashed along with private key to generate a signature.

Use Golang crypto/rsa to hash a message with your private key

Here is the digital signature that is returned.

Digital signature

The public key can now confirm that the private key combined with a particular hash would have created that signature as shown above. This signature is confirming that a document has not been changed since the private key signed it. The above command return type is an error. A valid signature is indicated by returning a nil error.

Verify signature

Refer to the Github repository for the source code.

MLG Blockchain is a blockchain development and consulting firm based in Toronto that is focused on building next generation applications using blockchain and smart contract technology. View all our blockchain development tutorials at www.mlgblockchain.com/learn.html.

MLG Blockchain