How preventing spam emails led to proof-of-work

Blessing Adesiji
Coinmonks

--

Photo by Pierre Borthiry on Unsplash

You are probably wondering what proof of work is? Let me explain, so you know Bitcoin ($BTC). BTC uses proof of work for block generation; it uses the Hashcash proof of work system. The proper definition of proof of work is a piece of data that is difficult to produce but easy for others to verify, and it must meet specific requirements. The word “difficult” here means that it is costly and time-consuming. It takes a lot of trial and error process before a valid proof of work can be generated.

I said Bitcoin uses proof of work; how? For a block to be valid, it must hash — hash is a random value assigned to data — to a value less than a given target. This is a way to prove that work has been done on a particular block, hence the word “proof-of-work”. With this, it becomes challenging for miners to generate new blocks and protects the blockchain from getting tampered with. Changing a block would require you to regenerate all previous blocks and then do the work they contain.

To clarify, let me explain one of the applications of this in spam email prevention with a few lines of Python code. Here we will try to prevent spam by doing some work on the email sent, and the known receiver can easily verify this (Proof-of-work). The idea is to make the email computationally expensive to hack. The most widely used proof-of-work algorithm used is SHA-256, and that is what we will use in this short tutorial. If you want more information on Hashing, you can read it here.

The code

Python Code Example

Explaining the code.

Import the hashing function — SHA-256

from hashlib import sha256

Create a Secret Phrase — This street phrase is only known by the legitimate receiver.

secret_phase = 'tweet_tutorial'

Create a hashing function that takes in the email body as input data and the street phrase. The function returns a special 8-digit character that meets some certain requirement known by the receiver.

def get_hash(input_data, secret_phase):     
combined_string = input_data + secret_phase
return int(sha256(combined_string.encode('utf-8')).hexdigest(), 16) % 10**8

Email body

email_body = "Hey man, The current price of Bitcoin is 56390.83," \     " as at the time of writing this email shehejge2339944502013e3449500020440423432"

This is the email body, and you can see that I added some random string at the end of the email that I got through a lot of trial and error, just so that the email body combined with the secret phrase meets some specific requirement known to the receiver. The receiver can easily verify this by calling the get_hash function on the email body and the secret phrase.

print(get_hash(email_body, secret_phase)) # 55673324

What I did with the was to come up with a certain constraint that I can only satisfy by inserting arbitrary data into the email body. When the receiver checks the email_body and the secret phrase, the same output will be returned, thus a proof of work done by me. If the arbitrary data that I added changes by just a string, the result — 55673324 would also change, and the receiver can confirm for sure that the email has been tampered with. Below I changed the last digit of the random data at the end of the email to 5, and the new email body is

email_body = "Hey man, The current price of Bitcoin is 56390.83," \     " as at the time of writing this email shehejge2339944502013e3449500020440423435"

My new output is

28554609

The output above is different from what the receiver is expecting, thus we know that the email has been tampered with.

In conclusion, legitimate emails will be able to do the work to generate the proof easily, but mass spam emailers will have difficulty generating the required proofs, which would require huge computational resources. And this is used as a consensus algorithm used in Bitcoin mining.

Twitter — https://twitter.com/bleso_a

Email — blessingadesiji96@gmail.com

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also Read

--

--

Blessing Adesiji
Coinmonks

A Developer Advocate, writes about Software Engineering, Data Science, ML & Blockchain Dev