# Cryptography Behind the Blockchain | Guide

We can see how Blockchain and cryptography go hand in hand along with the most relevant cryptographic concepts in terms of security in the use of cryptocurrencies.

The aspects that explain the security of Blockchain and cryptocurrencies are not widely known topics dominated by most because they are constituted by rigorous cryptographic arguments. But today we can say that it is possible to obtain a basic knowledge of the subject, which allows understanding the operation of this technology. The fact that they are safe has allowed them to have an expansion and positive adoption by so many users in the world.

It is important to have a broader view of the Blockchain theme and its connection to cryptocurrencies and to review it over time to realize that there was a period of maturation and evolution of cryptographic tools that leads to the Blockchain disruption: we are referring to the birth of asymmetric cryptography, which was what made possible, along with other concepts such as consensus and cryptographic hash functions, the crystallization of a decentralized digital currency in the hands of Satoshi Nakamoto. But this was the product of many previous investigations and advances.

We can see how Blockchain and cryptography go hand in hand along with the most relevant cryptographic concepts in terms of security in the use of cryptocurrencies.

## Hash function in cryptography

Let's start by mentioning what a hash function is. A hash function is one that receives information of any length as input and outputs a fixed-length alphanumeric string, regardless of the size of the input message, the results it is called the hash of the initial string. A particularity of this hash function is that if for some reason the input message has any variation, however minimal it may be, the alphanumeric string would change radically, it remains the same size but its content will be totally different.

There are several hash functions out there; some are in disuse due to real or theoretical demonstrations of weakness, particularly, for finding what is called collisions: this is, find two different inputs that produce the same hash output.

## SHA256 hash function

For practical purposes, let us examine how works a particular hash function that is important in the Blockchain, we refer to SHA-256. Here we can see the SHA-256 in action.

If we write there a simple “Hello” it will produce the hash:

185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969

What is most interesting with the hash functions is that small changes in the input will represent a completely different output. For example, the "hello" hash is:

2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

## Merkle Tree Block at Bitcoin

In Bitcoin, the SHA-256 algorithm is used to securely record the transactions in each block. A Bitcoin block contains a summary of all transactions, using a structure known as the Merkle tree. A Merkle tree is used to efficiently summarize the integrity of a large amount of data, by means of SHA-256 hashes computed recursively.

This is an efficient process that allows verifying if a transaction was validated and added to a block. It also produces a tracking of all the Bitcoin transactions, thus being able to be validating the authenticity of each transaction with a unique value.

The procedure to generate a Merkle tree is as follows. Using the double SHA-256, suppose that **n** transactions are taken, each of which is processed with SHA-256 and added to the Merkle tree. Knowing that the output of the hash is always 256 bits, regardless of the amount of input data, given the hash of each transaction, the hash reduces the data to finally get what is known as the Merkle root of the tree.

## Digital signatures in Blockchain protocols

On the other hand, the hash function itself is not the only cryptographic tool used in Blockchain protocols. In order to understand a little more about the use and management of cryptocurrencies, we want to address the issue of digital signatures.

Any person who has made any transaction with cryptocurrencies has used a wallet to exchange funds; therefore you have made use of public key cryptography. These transactions require the use of digital signatures and these are essentially the combination between a private key and a hash of the data (like those of a transaction), which generates a unique digital identification to establish the authenticity and integrity of the message without revealing the signer's private key.

Putting everything together, it can now be drawn the process needed to generate the private and public key for Bitcoin. The public key, after some modifications are known as the Address in those Blockchain and the private key is that necessary to sign the transactions.

It needs to be randomly generated a 256-bit string. From this string, Bitcoin uses the elliptic curve algorithm secp256k1 to generate the public key. Now, this is not yet the Bitcoin Address. The hash function now serves as an authenticity tool to distribute the public key, so it is hashed and compressed using SHA-256 and RIPEMD-160, in that specific order.

## ECDSA (Elliptic Curve Digital Signature Algorithm)

Many Blockchain protocols use the algorithm ECDSA (Elliptic Curve Digital Signature Algorithm) for the creation of private and public keys where ECDSA is a variant of Digital Signature Algorithm (DSA) that uses elliptic curve cryptography. A great advantage of elliptic curve cryptography is that it can be faster and use shorter keys than older methods such as RSA while providing a higher level of security.

The first public key encryption algorithms were based on the factorization of large prime numbers, but these are no longer considered safe when using short keys. The elliptic curve cryptography with the current technical means generates "intractable" keys considering the advances existing today.

Elliptic curve cryptography offers the possibility of creating smaller keys and thus reduces storage and transmission requirements. A key based on elliptic curve cryptography can give the same level of security with a 256-bit key as an RSA algorithm with a 2048-bit key. The main reason for using elliptic curve cryptography was to facilitate the handling of public addresses of the Bitcoin protocol.

Even so, Satoshi Nakamoto decided that the 44 digits were too many for a public address and applied a process of hash functions for the creation of public keys. The initial public ECDSA key ends at the end of that 160-bit hash process which, including version data and control digits, has 27 to 34 digits.

Here you can generate your own Bitcoin address.