© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
B. Wu, B. WuBlockchain for Teenshttps://doi.org/10.1007/978-1-4842-8808-5_2

2. Cryptography: The Backbone of Blockchain Security

Brian Wu1   and Bridget Wu1
(1)
Livingston, NJ, USA
 

Blockchains are built based on a range of different cryptographic concepts. From safeguarding wallets and securing transactions to protecting consensus protocols and encrypting private data for anonymous accounts, almost everything needs cryptography to ensure proper functioning. Cryptography is the backbone of blockchain security.

This chapter will dive into everything you need to know about cryptography in blockchains, starting with the basics. Then, you will be introduced to the classical symmetric key cryptography, asymmetric cryptography, and more. As you advance, you will become well versed in how digital signatures work.

This chapter will also cover how to utilize hash functions to hash data, and at the end of the chapter, you will get an in-depth look at elliptic curve cryptography (ECC), a key type of encryption cryptography that is used in blockchain.

In this chapter, the following topics related to cryptography will be discussed:
  • The basics of cryptography

  • Symmetric key cryptography

  • Asymmetric key cryptography

  • Digital signatures

  • Hash algorithms

  • ECC

  • Derived Ethereum addresses

The Basics of Cryptography

In Chapter 1, we have briefly discussed SHA-256 hashing cryptography which is used to hash block data.

Let's use an example to illustrate how secure messages can be sent to the public. Suppose Alice sends a personal message to Bob. Alice wants this message to be private. Only Bob can understand the message, and the message cannot be altered during transmission. The message sent over the Internet could secretly be intercepted and recorded by an intruder (see Figure 2-1). How can Alice and Bob stop this from happening? This is where cryptography comes in.

An illustration of a message sent by Alice to bob over the internet secretly intercepted and recorded by an intruder.

Figure 2-1

A message sent through a network

The word “crypto” comes from the Greek word “kryptós,” meaning “hidden or secret.”

“Cryptography” means “secret writing” and allows for the exchange of secure messages between willing parties.

The message is converted into a secret code equivalent called “ciphertext” via an encryption algorithm to prevent unauthorized access. The ciphertext is then sent over a public network, it is decrypted at the receiving end, and the recipient can view its contents.

In the preceding example, Alice uses a key to encrypt her message, converting it to ciphertext, and she sends it to Bob over the Internet. She does not need to worry about a hacker having access to her private message. To read ciphertext, a hacker must use a decryption key. When Bob receives this ciphertext message, he can use a key to recover the original plaintext via decryption.

A schematic depicts encrypting and decrypting of a message through the internet by Alice to Bob. The labeled process is my secret text, encrypt, ciphertext, decrypt, and text.

Figure 2-2

Encrypting and message over the Internet

Cryptography is mainly divided into three categories: symmetric key cryptography, asymmetric key cryptography, and keyless primitives such as hash function.

Symmetric Key Cryptography

Symmetric key cryptography is a cryptographic algorithm that uses a shared secret key between a sender and a receiver to encrypt and decrypt data. This secret key is called a symmetric key.

In the preceding example, Alice and Bob share the same secret key. Alice uses the secret key to encrypt the message, and the secret key is used in the decryption process when Bob reads the message. This shows how symmetric encryption works.

Symmetric encryption is typically more efficient than asymmetric encryption. Therefore, it is often used with large amounts of data encryption, personal data encryption, and decryption.

While there are several symmetric algorithms, the first US standard was DES.

Symmetric key cryptography can use either stream ciphers or block ciphers to encrypt data. Using stream ciphers is the preferred way to encrypt data in most cases.

Stream Ciphers

Stream ciphers encrypt plaintext messages one bit or byte at a time, resulting in a single-character-in, single-character-out cipher. It applies a random keystream of characters and the XOR operation to each binary digit in a data stream.

XOR is a Boolean logic operation and is known as the “exclusive or” or exclusive disjunction. It yields true when only one out of two inputs is true. If both or no inputs are true, the XOR operation output is false.

The truth table of an XOR logic is shown in Table 2-1 where 0 means FALSE, and 1 means TRUE.
Table 2-1

Truth table for XOR

 

Input

Output

A

B

A XOR B

0

0

0

0

1

1

1

0

1

1

1

0

So, if Bit A is 11000101, and Bit B is 10100110, the following shows what the output looks like when the XOR operation is applied for these two binary bits:
  •      10101

  • XOR     00110

  • Output     10011

Keystream characters can be random combinations of any letters or numbers. Assume that we have a stream of plaintext bytes (p1, p2, p3, ..., pi), and the keystream generator outputs a stream of bytes (k1, k2, k3, ..., ki). To encrypt the stream of ciphertext bytes, the operand XOR needs to be applied to each plaintext and key to generate and encrypt the stream of ciphertext bytes (c1, c2, ..., ci). This can be expressed through the following:

c1 = p1 XOR k1, ..., ci = pi XOR ki

Figure 2-3 illustrates stream cipher encryption. P1 is 00110101, and K1 is 11100011. With XOR operation, the ciphertext encrypt result is 11010110.
  •      00110101 (plaintext)

  • XOR     11100011 (key)

  • Output     11010110 (plaintext)

An illustration depicts the stream cipher encryption, processed in 3 steps data stream of P 1, P 2, to P i; keystream of K 1, K 2, to K i; and X O R of C 1, C 2, to C i. P 1 is 00110101 plus K 1 is 11100011 implies C 1 as 11010110.

Figure 2-3

Stream cipher encryption

To decrypt the plaintext bytes, we apply the XOR operation with the ciphertext and key. The output will get the original plaintext bytes. This operation can be expressed by pi = ci XOR ki:
  •      11010110 (ciphertext)

  • XOR     11100011 (key)

  • Output     00110101 (plaintext)

Block Ciphers

The main difference between a block cipher and a stream cipher is that a block cipher takes a fixed-size block of plaintext bytes as a single unit and encrypts block data as a ciphertext byte. Generally, the block size is the same as the key size.

Assume we have a block of plaintext bytes p and key bytes k. To encrypt the block of ciphertext byte c, we need to encrypt the plaintext with key c = encrypt (p, k) and recover the plaintext by decrypting the ciphertext with key p = decrypt (c, k).

Each block is of equal size. Let’s say the input is larger than the number of blocks, where the input size is 38 bits, and the block size is 6 bits. After 6 blocks, there will be 2 bits left (38 – 6 * 6 = 2). In this case, we typically add padding (two 0) and append it to the end of the block.

Figure 2-4 shows an example of block cipher encryption. The plaintext is divided by n block (pn), and each block uses the same key to encrypt a ciphertext. The encryption process will start from the first block and end on the last block, and eventually, all block data will be encrypted.

A diagram of 3 blocks. The labels in 3 blocks are P 1, P 2, and P n and P1 leads to key, which leads to C 1; P 2 leads to key, which leads to C 2; and P n leads to key, which leads to C n. The process starts from the first block and ends on the last block.

Figure 2-4

Block cipher encryption

The most commonly used types of block ciphers include advanced encryption standard (AES), data encryption standard (DES), and triple DES (3DES or TDEA).

Asymmetric Key Cryptography

Symmetric cryptography is relatively simpler and faster than asymmetric cryptography, only needing one key. It is typically used for big data encryption/decryption and confidentiality of bank transactions. Symmetric cryptography must share the same secret key before data encryption. In the previous example, the case becomes complex when Alice needs to send a private message to many people. If Alice uses a symmetric key (K) to encrypt all these messages and shares this key with Bob and others, there is a great risk that someone can secretly give a copy of K to others without Alice's knowledge. In this case, the entire communication channel has been compromised, and many unintended people can read and modify the messages and send them to any other members. To avoid this security risk, Alice must consider creating a large number of keys for each person that the message is sent to, but Alice will then have to remember all the secret keys. Alice needs to call her friends or be involved in face-to-face meetings over a trusted channel to distribute the secret keys. As a result, symmetric cryptography could quickly become less practical for many participants. This problem affected the industry in relation to the use of encryption for quite a long time, but in 1976, Diffie and Hellman introduced the concept of public key encryption, also known as asymmetric cryptography.1

Public and Private Keys

Whitfield Diffie and Martin Hellman described how public key encryption works when two communicating parties exchange information across an insecure channel using a key pair consisting of a public key and a private key.

A private key is known only to the owner, and a public key is considered public information that is available to anyone. Each key has been designed for a specific purpose.

The public key is used to encrypt a message and convert it into ciphertext. The private key is used to decrypt a message that has been encrypted with the public key.

How the Diffie-Hellman Algorithm Works

The Diffie-Hellman algorithm is based on a mathematic principle and uses the following formula:

ga (mod p)

Modulo is the remainder of a division operation. For example, 5 mod 3 = 2 because 2 would be left over. Mod can also be expressed as %.

With p, g as a prime number, g is a primitive root modulo p. The g and p numbers are public and can be seen and used by anyone.

In math, a g number is a primitive root modulo n if every integer relatively prime to n is congruent to a power of g modulo n.

For example, 2 is a primitive root mod 5, all the numbers relatively prime to 5 are 1, 2, 3, and 4; and each of these (mod 5) is itself, meaning that 2 (mod 5) = 2:
  • 20 = 1, 1 (mod 5) = 1, so 20 ≡ 1

  • 21 = 2, 2 (mod 5) = 2, so 21 ≡ 2

  • 23 = 8, 8 (mod 5) = 3, so 23 ≡ 3

  • 22 = 4, 4 (mod 5) = 4, so 22 ≡ 4

Now, we will look at how the Diffie-Hellman algorithm works. Let's use a simple example to understand the algorithms of key exchange. Imagine Alice and Bob want to exchange information. Now, assume a hacker named Eve is trying to intercept the message.

Step 1 – Alice and Bob agree that all messages need to be calculated by the formula ga (mod p) using a modulus p = 13 and base g = 6. Alice and Bob both select a secret number that is known only to them. However, the formula and numbers g and p are public to everyone.

An illustration of the public value agreed upon by Alice and Bob. Modulus p equals 13 and base g equals 6.

Figure 2-5

Alice and Bob agree on public value

Step 2 – Alice chooses a secret random number (a = 5) as her private key, and Bob chooses his private key (b = 4).

An illustration of secret private keys of Alice and Bob. Alice's random secret key, a equals 5 and Bob's random secret key, a equals 4.

Figure 2-6

Alice’s and Bob’s secret keys

Step 3 – Alice and Bob will then use the formula to calculate public values using the p and g parameters and their private values.

Alice will calculate using the following:

A schematic depicts the public values calculation of Alice and Bob. The public value for Alice is 6 power 5 is equal to 7776, mod 13 is equal to 2, and Bob is 6 power 4 is equal to 1296, and mod 13 is equal to 9.

Figure 2-7

Alice and Bob calculate public values

65 (mod 13) = 7776 mod 13 = 2

Bob will calculate using the following:

64 (mod 13) = 1296 mod 13 = 9

Step 4 – Alice sends her calculated result (2) to Bob over the Internet. Eve can see this. Bob sends his calculated result (9) to Alice, and Eve can see this also.

An illustration depicts the exchange of public keys each other by Alice and Bob. The exchange keys are 9 from Bob, and 2 from Alice.

Figure 2-8

Alice and Bob send their results to each other

Step 5 – Alice and Bob both received the other side's public message, and they calculated the shared secret through the formula ga (mod p).

In this step, g is a public message sent from the other side.

Alice will calculate using the following equation:

An illustration depicts the secret calculation of Alice and Bob. The calculation for Alice is 9 power 5 is equal to 59049, 59049 mod 13 is equal to 3, Bod is 2 power 4 is equal to 16, and 16 mod 13 is equal to 3.

Figure 2-9

Alice and Bob receiving the same values

95 (mod 13) = 59049 mod 13 = 3

Bob will calculate using the following equation:

24 (mod 13) = 16 mod 13 = 3

Both Alice and Bob have gotten the same values.

Eve is a hacker. She intercepts the message sent between Alice and Bob. Eve can see the public value (ga (mod p)), g is, she can see what p is, and she knows the computed results from Alice and Bob. However, Eve does not know the secret numbers chosen by Alice and Bob, and she will not be able to find them easily.

A schematic depicts Alice's and Bob's secret values not known by hacker Eve. The values are a is equal to 5 for Alice and 4 for Bob. Alice and Bob agreed that g power a, modulus p is equal to 13, and base g is equal to 6.

Figure 2-10

Hacker Eve does not know Alice’s and Bob’s secret values

From the Diffie and Hellman example, we can see that both Alice and Bob, using the following equation, computed to get the same result:

(ga (mod p))b mod p = (gb (mod p))a mod p

The shared key is gab. Typically, a, b, and p are much larger values. This is needed to make results secure.

In the original Diffie and Hellman description, the algorithm does not provide identity verification for communicating parties. This led to the algorithm being vulnerable to man-in-the-middle attacks.

Look at the following example:

Eve intercepts the message between Alice and Bob and blocks communication between them.

Eve intercepts Alice’s public value (ga(mod p)) and sends Alice her own public value (gc(mod p)).

Eve intercepts Bob’s public value (gb(mod p)) and sends Bob his public value (gd(mod p)).

Neither Alice nor Bob can detect any problem, and each may assume that the other side received their message, but in reality, Eve can decrypt, read, modify, and re-encrypt all of their messages.

An image depicts the algorithm g power a mod p being vulnerable to man-in-the-middle attacks. The explanation is hacker Eve intercepts Alice and Bob's messages and blocks the communication between them.

Figure 2-11

A man-in-the-middle attack

Diffie and Hellman is the first asymmetric cryptography protocol and provided the basis for many authenticated protocols, such as the elliptic curve Diffie-Hellman asymmetric algorithm and RSA, that are widely used today. Crypto, secure shell (SSH), secure sockets layer (SSL), email, and VPN security are all based on these asymmetric algorithms.

How Digital Signatures Work

If Alice wants to send a signed message to Bob over the Internet, the following steps should be followed:
  1. 1.

    We begin with the message on Alice's (the sender’s) side.

     
  2. 2.

    The algorithm generates a one-way hash of the message based on the document checksum.

     
  3. 3.

    The hash is signed or encrypted using Alice's private key.

     
  4. 4.

    The message is sent to Bob.

     
  5. 5.

    Bob receives the message.

     
  6. 6.

    Bob decrypts the message using Alice's public key and verifies the message authentication that was signed by Alice.

     
  7. 7.

    The algorithm regenerates a hash of the message.

     
  8. 8.

    The two hash values are compared. If they are identical, we ensure that the transmitted document has not been altered since signing.

     
The process of a digital signature is shown in Figure 2-12.

A schematic depicts the process of digital signatures of Alice and Bob. The labeled process is origin document, hash, signing/encrypt, private key, public, unsigning/decrypt, origin document, and hash.

Figure 2-12

The process of digital signatures

Digital Signatures

A digital signature is an asymmetric public key cryptography technique that verifies digital messages or document owner authenticity.

We often use the public key for message encryption and the private key for message decryption in public key encryption. However, in the case of a digital signature, the message is encrypted with a sender's private key, or in other words, the message is signed.

As the signer's public key is known, anybody can verify a message's digital signature.

A digital signature includes the following characteristics:
  • Authentication (verification of who signed the origin of the document)

  • Nonrepudiation (the identity of the signer and document that they have digitally signed should be undeniable)

  • Integrity (proof that the document has not been altered since signing)

Hash Algorithms

In Chapter 1, “How Blockchain Works” section, the hash concept was introduced, and the way that blockchain uses the SHA-256 hash function to hash transaction data was discussed. In the previous section, the process of digital signatures was discussed. The process indicated that the process needed to apply a hash function.

Hash algorithm is a mathematical function that divides original data input into smaller blocks of equal size and then executes the hash function to two fixed-size data blocks to generate a hash code. The algorithm starts from the first block of message data with a seed value that passes into the hash function, outputting the first hash code.

An illustration depicts the algorithm code of the 2 data blocks generate a hash function. It gives the hash value 356A192B7913B04C54574 D18C28D46E6395428AB.

Figure 2-13

Two fixed-size data blocks used to generate a hash code

The process is similar to block cipher, discussed in the previous section. The hash process will start from the first block and end at the last block. This process can be repeated for as many rounds as are required by the algorithm. Eventually, all block data will be chained together and hashed.

A flow diagram of the hash algorithm. The labeled flows are data seed, message block, and hash function. The process is continued till receiving the hash value.

Figure 2-14

The hash algorithm

The Keccak-256 Algorithm

The Keccak-256 algorithm is a hash function that is widely used in an Ethereum blockchain. A few examples include Ethereum addresses, some smart contract functions, and the Ethereum consensus engine known as Ethash that plays an important role in producing blocks and other security actions.

The Keccak-256 is a family of SHA-3 hash functions. The function input can be a variable-length string or number, and generated output will always be a fixed-length, 64-character (letters and numbers) output. The output can be converted to hexadecimal numbers. Like all other hash functions, it is a one-way cryptographic hash function.

Keccak-256 is based on the sponge construction and is a sponge function family. Keccak-256 sponge function (Keccak[r,c]) needs two parameters: one of size r (the bitrate and the amount of data encoded for a single unit of time) and the other of size c (the capacity).

Padding

A padding function will append enough bits to the input data (M), and the length of the padded input can be split into multiple r-bit blocks.

Initialization

The padded input is broken into r-bit blocks, assuming the blocks' names are called M0, M1, M2, and so on.

The Absorbing Phase

The r-bit block is XORed with small chunks of the input data M0. The result is then passed to a compression function f. The output of function f XORed next message M1. The process is repeated until each of the message blocks Mn is processed. In each step, a small chunk of the input data (the bit length of r) is “absorbed” into the buffer.

The Squeezing Phase

The same process is repeated. The r-bit block of the buffer consists of the next r bits of output (Z0, Z1, Z2, and so on). The function f is used to extract r bits of data as the next r bits of the output. The process is repeated until the results are produced.

Figure 2-15 illustrates the Keccak-256 hash algorithm.

An algorithm depicts the Keceeak-256 hash values, through 2 processes absorbing and squeezing. The labeled process is the message, pad, and output.3658-89/+/

Figure 2-15

The Keccak-256 hash algorithm

Elliptic Curve Cryptography (ECC)

ECC was discovered in 1985 by Victor Miller (IBM) and Neil Koblitz (University of Washington) independently and is currently one of the most robust and widely used types of encryption cryptography. Blockchain networks, such as Bitcoin and Ethereum, use ECC.

In 2005, the US National Security Agency (NSA) announced a set of unpublished algorithms known as Suite B protocols and posted a paper titled “The Case for Elliptic Curve Cryptography” in which they recommended that the US government use ECC to secure sensitive and unclassified communications. 2

The NSA commented that analysts should “take advantage of the past 30 years of public key research and analysis and move from first generation public key algorithms and on to elliptic curves.”

Suite B’s protocols included both elliptic curve Diffie-Hellman (ECDH) and elliptic curve Menezes-Qu-Vanstone (ECMQV) for key exchange, the elliptic curve digital signature algorithm (ECDSA) for digital signatures, the advanced encryption standard (AES) for symmetric encryption, and the secure hashing algorithm (SHA). After the release of these protocols, ECC soon became the de facto standard for protecting modern industry communications.

ECC is based on algebraic properties of elliptic curves and provides equivalent security with much smaller key sizes than other asymmetric cryptography systems, such as RSA or DSA. For example, a 256-bit ECC key is equal in power to a 3072-bit RSA key. This also makes elliptic curves significantly faster.

What Is an Elliptic Curve?

An elliptic curve is a curve given through the following equation:

y 2 = x 3 + Ax + B

The curve, as required, is nonsingular and needs to have no repeated roots or self-intersections. To ensure that the curve is nonsingular, the condition can be expressed through the following equation:

4A3 + 27B2 ≠ 0

An example of the elliptic curve when A = -1 and B = 1 can be seen in Figure 2-16:

A curve graph depicts the elliptic of the equation y square is equal to cubic x minus x plus 1. The curve started at point negative 3, bent in negative 1.5, and ended at 3 approximately.

Figure 2-16

Elliptic curve y2 = x3 - x + 1

y2 = x3 - x + 1

In an elliptic curve, there is a useful property to create an addition on the curve, turning it into an abelian group. Take two points, P and Q, on the curve, and draw a line through them. The line will intersect the curve at one more point (R). Take P + Q as R, and from R, another line can be drawn either straight up (if R is below the x-axis) or straight down (if R is above the x-axis) to the other side of the curve, opposite to the point -R (see Figure 2-17).

A curve graph depicts the addition of 2 points R and negative R of elliptic. The curve passes a line of axis P, Q, and R. The result is calculated by negative R implies P plus Q.

Figure 2-17

The elliptic curve addition of two points (R and -R)

The group law for an elliptic curve is expressed through either of the following equations:

P + Q + R = 0, or P + Q = - R

The sum of the points P and Q is equal to the point - R.

With the addition of points P = (x1, y1) and Q = (x2, y2) of an elliptic curve, a third point can be calculated using the following formulas:

P + Q = R = (x3, y3) where

Equations for x cube and y cube. Value of lambda for P not equal to Q and P equal to Q

Elliptic curve E over Zp is defined by the following equations, and Zp is field modulo p, meaning that {0, 1, 2, …, n - 1}:

A formula depicts the mod p equation of y square equals cubic 3 plus A x plus B, and A B E Z of p, 4 cubic A plus 27 B square not equal to 0.

By applying this formula, the elliptic curve E over Zp - E(Zp) has a list of points (x, y). In the following example, elliptic curve y2 = x3 + Ax + B = x 3 + 2x + 3 → y2(mod 5), where A = 2, B = 3, and P = 5. This can be seen through the following:
  • X = 0 → y2 = 3 → no solution (mod 5)

  • X = 1 → y2 = 6 → 6 (mod 5) = 1

  • → y = 1, 4 because 12 (mod 5) = 1, and 42 (mod 5) = 1

  • X = 2 → y2 = 15 → 15 (mod 5) = 0

  • → y = 0 because 02 (mod 5) = 0

  • X = 3 → y2 = 36 → 36 (mod 5) = 1

  • → y = 1, 4 because 12 (mod 5) = 1, and 42 (mod 5) = 1

  • X = 4 → y2 = 75 → 75 (mod 5) = 0

  • → y = 0 because 02 (mod 5) = 0

Then, the elliptic curve has the following seven points:

(1, 1), (1, 4), (2, 0), (3, 1), (3, 4), (4, 0), ∞.

Now, rearranging the modulus operator P = 263 leaves the following equation:

(x3 + 2x + 3) (mod 263)

Scalar multiplication over the elliptic curve y2 = x3 + 2x + 3 in mod 263. The curve has 270 points, including the point at infinity.

A dot depicts the elliptic curve of equation y square equal to cubic 3 plus 2 x plus 3, representing the point as Q and P. Both axis ranges from 0 to 250.

Figure 2-18

Elliptic curve y2 = x3 + 2x + 3 (mod 263)

In the elliptic curve, when if P is (x, y) ∈ E(Zp), then (x, y) + (x, -y) = ∞ (the point at infinity).

So, if adding ∞ to any point P in E(Zp), we can always get P back. This can be expressed through the following equation:

P + ∞ = ∞ + P = P for all ∈ E(Zp)

In Alice’s and Bob’s message communication, P is a (x,y) point on the curve that both Bob and Alice will agree to. Bob’s private key is represented by n, and K is his public key. We multiply P and n together to produce K, as shown as follows:

K = n P

If we multiply K with P, it will get the point on the elliptic curve.

Deriving an Ethereum Address
We have learned about cryptography so far. Now it is time to do some practice and apply our knowledge to generate an Ethereum address. We will utilize multiple online tools to do this exercise. Of course, you can choose a different tool if you find a better one, or you can even write code to implement the same result:
  1. 1.

    Create a random private key and derive the public key from this private key using EC curve (secp256k1).

    The private key is 64 hexadecimal characters long (32 bytes).

    Here we use an online tool (https://​kjur.​github.​io/​jsrsasign/​sample/​sample-ecdsa.​html) to get a public and private key pair. The generated public and private key pair can be seen in Figure 2-19.

     

An algorithm depicts the elliptic supported by the chosen E C curve name and generates key pair. The process is generated by 2 keys E C private key and E C public key.

Figure 2-19

The elliptic curve generating public and private keys

Private key: 8c2b80899dd44981d7f8b38c1f5b13dbf1fbb98c360d9d1cfd63a3aed0d7b498

Public key: 04502fa444861915b6a258c3daa2beaa41c9b912e6fd6cd526fa179c60362f602bd75e36cfe1513a1e460ca646476e54fab08aa42730068326ab0a8a2e57d2829b
  1. 2.

    Derive the address from the following public key:

    Start with the public key (128 characters) and apply the Keccak-256 hash of the public key. It will generate a string that is 64 characters long (32 bytes). Let’s use the Keccak-256 online tool (https://​emn178.​github.​io/​online-tools/​keccak_​256.​html) to see the hash result (see Figure 2-20).

     

Two algorithms depict the address and derivation of the Keccak-256 online hash function. The keys labeled are input type, hash, and auto-update.

Figure 2-20

Keccak-256 hash public key example

The result is db97247835ec1f9d0bd8b6ed6a6d125cf3029f4ccbe72cc1b1a4c7a8c72467a3
  1. 3.

    Get the Ethereum address

    Take the last 20 bytes (40 characters) of the generated hash to get the Ethereum address with prefix 0x. 0x lets the people know the address in hexadecimal format. When prefixed with 0x, it becomes 42 characters long. So, in our example, the Ethereum address is

    0x6a6d125cf3029f4ccbe72cc1b1a4c7a8c72467a3

     
  2. 4.

    Verify the Ethereum address

    We use one of the online Ethereum address validator tools (www.rfctools.com/ethereum-address-validator/) to verify our new Ethereum address.

     

A screenshot of the valid Ethereum address validator. The labeled R F C tools are cryptocurrency tools, of 4 steps, image tools, J S O N tools, and H T M L tools.

Figure 2-21

Validating the generated Ethereum address

Congratulations! We just generated a valid Ethereum address using our cryptography knowledge. At this level, you should get a good sense of how blockchain cryptography works.

Summary

Cryptography is an essential mechanism for securing blockchain technology. It is used to secure the blockchain consensus mechanism, protect blockchain data, keep user accounts safe, and more. The main purpose of this chapter was to give a more thorough understanding of cryptography by giving a quick overview of how it works.

Although it only scratches the surface of cryptography technologies, this chapter will help you to enrich your knowledge of symmetric key cryptography and asymmetric key cryptography. More importantly, we now know how digital signatures work. We covered the hash algorithm, and we walked-through elliptic curve cryptography to understand how it works. Lastly, we learned how to generate an Ethereum address.

We will continue our journey and learn about Bitcoin, the future of money in the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.28.94