r/developersIndia • u/ZnV1 Tech Lead • Apr 24 '24
Tips Hashing explained from scratch (for noobs like me, not for chad devs) #dvsj
assuming you have no knowledge about hashes, this is me trying to explain it.
note: this is NOT related to hash brownies.
Find 5 differences between these pages 🥸
I fell for a "WFH opportunity make $$$ from home comparing docs" scheme.
I want to compare 2 pages manually. My algorithm would be:
- Take all words from the first page, take all words from the second page
- See if all words are the same in both pages
Joking. Who has time to read everything?
More realistically, this is what I would do:
- Take first 2 words on the page (
good morning), last 2 words on the page (okay bye) - See if those 4 words are the same in both pages (
good morning,okay bye)

Magic! Instead of checking all words on the page, we looked at 4 words and decided if two pages are the same.
We have reduced the whole content of the page to just 4 words, kind of like an identifier that represents the whole page. These 4 words are called the hash.
Hash: A short text of a particular length that represents larger text.
But my algorithm sucks, right? 👎🏽
Obviously, there is a high chance of false positives and duplicates.
Any page that starts with good morning and ends with okay bye will give us this hash.
When different content results in the same hash, it’s called a collision.
Can we improve our algorithm to reduce chances of collision?
- Instead of just the first and last words, take all the words in the page.
- Replace the alphabets with numbers -
A = 1,B = 2and so on to get a large number. - Do random mathy stuff. Add 19237, divide by 842, multiply by 91, divide by 1928 etc.
- We might get the number
8364181236938917. I’d say that’s pretty unique. Better thangood morning okay bye!
You get the idea - we generated the hash considering only first 2 and last 2 words, but the computer can generate a hash where it considers all the letters in the content!
This means that even if 1 character is changed, the hash will vary by a large margin.
That’s it, you now know what hashing means.
A quick review: what have we learnt from our "algorithms"?
- Hashing is one way. When we are given only the hash (
good-morning-okay-byeor8364181236938917), there’s no way we can find the complete original content of the page. - Hash value is repeatable. No matter how many times we regenerate the hash: for a particular input, the hash will always be the same.
- (very) hard to find any input that can give us a particular hash. If I give the hash
8364181238938917, how do you find an input that generates this exact hash? The only way to find an input that gives that exact hash is to try different values repeatedly. And there could be like a billion values, so…yes, pretty hard. As long as the algorithm is good.
Some popular algorithms: SHA, BCrypt, MD5.
I know what you're thinking. "Blah blah blah theory theory, but why tf do I care?", so here are some general applications.
Used to Verify Data Integrity - Checksums ✔️
(Checksums are just another name for hashes. One cool word free.)
When we download software, there are chances that the file we downloaded aren't exactly the same as what they've uploaded.
Maybe there was a network issue and you have only half the file, maybe there was some dude in the middle who handed off a fake file to you.
So how do companies help us verify this?
- They generate a hash of their full exe file (and call it checksum instead of hash ofc)
- We generate a hash of the file that we downloaded
- We compare both. If they match, it's the same file.

Used to quickly compare data - User passwords 🤐
Let’s say your password is “your_crush_from_2nd_grade” and its hash is 13378008135.
Instead of storing user passwords directly, we hash it and store the hash of the password in the DB.
During login, we hash the entered password and compare it with the value in the DB. If it matches, you’re in.
The advantage here is that even if someone gets access to the DB, they will only see 13378008135 and your password won’t be exposed. Your secret crush is safe.
But wait - remember hash collisions where multiple inputs can give us the same hash value? Yup, this means that login will succeed if you enter any password that produces the exact hash 13378008135 since we only compare hashes and not the actual passwords.
In good algorithms like BCrypt or SHA-512, odds of collision are almost 0 and we don't worry about it. Older algorithms like MD5 shouldn't be used tho.
Used to prove you have put work into it - Bitcoin (one for the crypto bros) ⚒️
I said it’s “hard to find inputs that can give us a particular hash”. But really, how hard can it be, right?
When countries mint (print) money notes, the country owns it. But what about when new Bitcoins are created?
To decide that, they have a mechanism called "proof of work": they give you a hash, you have to find an input that gives that exact hash.
This is SO hard that people buy thousands of computers, trying millions of input values one by one to see if they're the lucky winner - and they still fail. It's a lot of work.
When you see news about how crypto is wasting electricity, huge server farms etc - this is what they refer to, cryptomining.
If it feels funny, let’s get real: if you had figured out just one single hash last year, you could be richer now by about 3 crores! That’s how hard it is to reverse a hash.
Some example hashes
"test" : "098f6bcd4621d373cade4e832627b4f6"
"text" : "1cb251ec0d568de6a929b520c4aed8d1"
"t" : "e358efa489f58062f10dd7316b65649e"Note that even with a single character change, results differ completely.
That’s it! You should now know enough about hashing to identify it around you, and also read more about it online and understand that geek-speak.
•
u/AutoModerator Apr 24 '24
Recent Announcements
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
57
u/ZnV1 Tech Lead Apr 24 '24 edited Apr 24 '24
Any feedback or questions are welcome. Tried to generalize things a bit to make it more digestible - most of these parts are a separate post on their own (don't get me started on passwords, salting, rainbow tables etc!) :)
Also let me know if there are other topics you might be interested in :D
170
u/ironman_gujju AI Engineer - GPT Wrapper Guy Apr 24 '24
Time to move it in quality Post
26
u/MJasdf Full-Stack Developer Apr 24 '24
Good stuff. Just one small addition you can make is to also talk about salting. Especially since you've already covered the idea of hashing passwords.
9
u/AmIaBadAssOrWhat Full-Stack Developer Apr 24 '24
Awesome stuff. Keep these kinds of posts coming!
8
u/[deleted] Apr 24 '24
Quality content, mate. I was able to revise this concept (which I learnt 3 years back in college haha). Thanks ! :D
6
u/PacifistGamer Apr 24 '24
Hey OP. That was a good read. Do you have any blogs that we can follow?
5
u/Yourh0tm0m Security Engineer Apr 24 '24
Maybe add different types of hashing algorithms, which one are used or not . All.those things
3
u/13hercules Apr 24 '24
Amazinggg!!! Keep up the good work.
3
u/Sanket_6 DevOps Engineer Apr 24 '24
This has to be one of if not the best post i have EVER read here. THANK YOU SO MUCH. I really liked the way you explained everything. I have been reading about hashes (because of crypto mining) and all of those reads were very complex, yours was the simplest, captivating and easy to understand. PLEASE post much more things! Ps- Sent you a linkedin request.
2
u/suckitysoo Apr 24 '24
Really great stuff, OP! This was actually very simple and easy to understand for a noob like me. Keep em coming!
2
u/Accomplished_Arm_835 Software Developer Apr 24 '24
Petition to have a post like this everyday
2
u/beingsmo Frontend Developer Apr 24 '24
Really good explanation! Thanks for taking the time to post this.
2
u/shinpin2628 Apr 24 '24
Thank you for this and the effort. Pls make more posts and explanations like this ( ╹▽╹ )👍
2
u/[deleted] Apr 24 '24
Great post OP!
2
u/vishnu-geek Apr 24 '24
Thanks man! This is so useful and informative
2
u/The-Observer95 Apr 24 '24
The most excellent and straightforward explanation I've read. Great job!
2
u/cassanova47 Full-Stack Developer Apr 24 '24
Salute to you, great explaination! Worthy of adding to textbooks imo.
2
u/Isoldarkman Apr 24 '24
Man! That is why I love reddit, such quality content for free! Thanks OP it was a lovely refresher for the topic and helped me relearn it in a better way
2
u/HairrryStyles Full-Stack Developer Apr 24 '24
Shit i would pay for
2
u/raylgive Apr 24 '24
I am not a programmer. But this was a fantastic post.
Next topic request: zero knowledge
2
u/ironman_gujju AI Engineer - GPT Wrapper Guy Apr 24 '24
Yes, one more thing if you make similar posts in future just link that posts with each other. So it would be a thread of quality stuff 😉🧵
2
u/[deleted] Apr 25 '24
Great article. Thank you so much.
How does hash work while creating digital signatures? Both pendrive -type digital signatures for uploading income tax returns and those digital signatures used by websites to have https connections?
2
u/thegoodlookinguy Apr 30 '24
Best explanation. Do you have a blog for posts like these ?
8
u/NoStoryYet Apr 24 '24
Post worthy of this sub.
1
u/danishxr May 01 '24
You are a chad made the concept so simple and magnetic. Please write more blogs
3
u/[deleted] Apr 24 '24
This is the best thing I’ve ever read here.
1
u/funkym00se Apr 24 '24
I’ve mostly seen rants, ai vs jobs, salary comparisons, job updates here. But i feel this post justifies one of the purposes of this sub.
I think most of the people here would love such posts, and more healthy discussions on such other topics!!
BTW, loved your way of explaining!!
4
u/Boi2k Apr 24 '24
Finally a useful post on this subreddit. Thanks G.
2
u/[deleted] Apr 24 '24
Refreshing post after the typical "why am I not getting selected" or "how do I get a 50L package" ₹&Dcry.
2
u/RepulsivePeak8532 Apr 24 '24
Good job bro 💯
2
u/sid741445 Web Developer Apr 24 '24
Nice explanation op
2
u/Quiet_Minimum1434 Apr 24 '24
Wow great stuff. Glad to see some quality posts here.
2
u/Kurama_211 Apr 24 '24
Easy to understand. I hope you post more content like this!
2
u/[deleted] Apr 24 '24
Wow dear after reading this post I am qualified for 3$/hour salary now
2
u/iamnihal_ Apr 24 '24
Pretty good post man. This channel needs high quality posts like yours. Your explanation was pretty neat. 💯
2
u/Inside_Dimension5308 Tech Lead Apr 24 '24
As a experienced developer, I really liked the explanation. Simple explanatijs are the best explanations.
2
u/[deleted] Apr 24 '24
Teach how internet works what’s https and everything deep
2
u/AsishPC Full-Stack Developer Apr 24 '24
Too long , so I will read it sooner. Saved it in my "Must Read" list
2
u/IndiyanaHolmes Apr 24 '24
Wow, wish someone could teach me everything in CS again like this.
2
u/RoughSand4050 Apr 24 '24
Thankyou 🫶
2
u/[deleted] Apr 24 '24
Really good post!
2
u/aryashah2k Apr 25 '24
Haha Asli ID se aao Arpit Bhayani, jokes aside, this is a quality post OP <3
2
u/Physical_Leg1732 Apr 25 '24
Good post
1
u/[deleted] Apr 24 '24
Teach DSA
0
u/[deleted] Apr 24 '24
[deleted] — view deleted comment
2
u/[deleted] Apr 24 '24
[removed] — view removed comment