Salting, peppering, and hashing passwords

mCoding · Beginner ·🔧 Backend Engineering ·4y ago

Key Takeaways

Explains the concept of salting, peppering, and hashing passwords

Full Transcript

hello and welcome i'm james murphy from m coding and today we're talking about storing passwords and how hashing salting and peppering your passwords can improve your security before we get started if you're interested in password security there's a good chance you'd like your own website and if so check out this video's sponsor hostinger host a website for just 139 a month with the single shared hosting plan or for just 259 a month host up to 100 websites with the premium shared hosting plan which comes with a free domain use code m coding at checkout for up to 91 off claim your free domain and don't forget to secure it with https all right let's talk about database leaks whether it be from a sql injection attack a misconfigured server or a disgruntled employee database leaks are bound to happen from time to time not necessarily to your company but to some companies so let's go through a hypothetical database leak and see what happens if plain text passwords are leaked versus hash passwords versus salted hashed passwords versus peppered salted hashed passwords each of these things provides a different extra level of security that the previous did not all right let's get started with plain text passwords it's the simplest case and it provides the least security for obvious reasons updating a password in plain text just involves taking the password from the user storing it in some kind of user object and then sending that to the database to you know update the record verifying it is just comparing the inputted password against the stored one in the database and one thing you can do is to use a compare digest which is a constant time compare to avoid things like timing attacks although if you're storing passwords at all you've already messed up in main here i've just got a dummy database class that prints things out about the user when you call the store method our users are just going to be a data class with an email and a password and i'll simulate updating the password for a user and then verifying the correct and an incorrect password in the event of a database leak this is obviously the worst case the hacker gets every user's email and password in plain text that means that they can use that email and password to log in as any user for this service or they could try using those credentials to log into other services so if a hacker gets your facebook password they may just try to log into your bank account with that same information of course you're supposed to use a unique strong password for every service that you use but who actually does that let's see how we can improve the situation by incorporating a hash function in this version we take in a hash function and instead of storing the raw password we store the hash version of the password we additionally stored the name of the hash function that we used so that in the future if we ever want to migrate to a different hash function we can tell which function was used to hash the password verifying the password then just comes down to looking up the hash function that we used and then hashing the password with the given hash function and comparing against the one we have stored it's also important to note here that for all of these examples i'm actually doing some base64 encoding this is very common in practice and it makes it a lot easier to read when we print things out it's not super important but i do also want to note that the dollar sign is not used in base64 encoding that makes it safe to use as a separator for the name and hash and that's why we can just split the password like this to recover those things okay so let's get to taking a look at what the hacker sees when a database of hashed passwords is leaked so here's what the hacker would see if hashed passwords were leaked they can't see the passwords directly so they do have to do at least some work but they can recognize and break common passwords because these hashes could be precomputed they can also guess passwords offline for those that aren't precomputed since they know which hash function was used this is a big win since the server won't ban them for too many wrong attempts potentially allowing them to break weak passwords even if they haven't been pre-computed also if they are able to crack a password that multiple users have used it cracks the password for everyone that used that password all at once they can also see if any two people have the same password since those two passwords would have the same hash even if the hacker isn't able to identify what the password is from this data this is a lot better situation than plain text passwords being leaked but at this point you should probably assume that any one of your users that had a weak password is compromised so how can we improve this by using salts well what is assault anyway assault is just a little bit of extra randomness that we sprinkle in with the user's password assault should be unique for each user you can see here that instead of just hashing the password directly we hash the salt together with the password then when we store the password we actually stored the name the salt and the hashed password when we verify we recover the salt from the user's stored password and then use that together with the inputted password hash it and use that to compare against the hashed password that was stored for the user so here's what the hacker sees when salted hash passwords are leaked of course they can identify the hashing algorithm that's used and because we put the salt in there they also get the salt out of course then there's the hash but if the salt is right there what's the purpose of using it the hacker just gets it for free well the big win here is that hackers can no longer recognize hashes of weak passwords because throwing those salts in there made them all unique precomputed hashes become effectively worthless a hacker can still guess passwords offline and check to see if they're correct since the salt is known that was part of the information that was leaked in the database this allows them to potentially break weak passwords but if the hacker does spend the time to crack a weak password it only cracks it for that one person it doesn't crack it for everyone that shared the same password they also can no longer tell if two people have the same password by looking at the hash okay then what's a pepper and how does it differ from assault well a pepper is similar to a salt in that it's some randomness that you sprinkle in with the password but its primary difference is that a pepper is specifically something that you do not store in the database assault was stored in the database with the password but a pepper is stored somewhere else it's stored perhaps in your application code or in a secure memory enclave because it's not stored in the database it's something that a hacker doesn't see even if the database is leaked additionally it's typical to only have one pepper for your entire application since we can't store the pepper in the database it wouldn't make sense to have one for each user because where would we store that information the way that you use a pepper though is pretty similar to the way that you use a salt you just sprinkle it in with the salt and the password at hash time and then when you want to look it up you still get the salt from the hashed password that was stored in the database but you get the pepper from wherever you get it stored in some secure memory enclave so what does using a pepper in addition to the salt and hashing by us well the hacker can still see the hash function and the salt but the pepper isn't there because it wasn't stored in the database the hacker doesn't even know a pepper is involved and because the hacker doesn't know the pepper they can no longer guess weak passwords offline the hacker would need to simultaneously leak the database and the application code to get any useful information out of the database leak at which point it would be similar to the situation where we were just using hashed and salted passwords the only downside to using a pepper is that since no database is used it may be harder to ensure that every server has access to this pepper because of these benefits i think the peppers are going to start picking up a lot more steam in the future but currently most libraries don't support peppers but they do support salts i'd like to see that start to change in the future but right now that's just how it is because you really shouldn't be writing your own authentication systems you should just be using open source well proven ones this probably means that you won't be able to use peppers in the near future because they don't support it yet but i will at least link to a best practices document so that you can at least see what you should be doing in the modern day and how you should be storing passwords securely using the tools and projects and libraries that are available to us right now so that's all i've got on hashing salting and peppering your passwords i hope you learned a little bit if you enjoyed the video please don't forget to subscribe leave a like leave a comment it really helps out with the algorithm see you next time

Original Description

What can a hacker do when a database is leaked? Try Hostinger: https://hostinger.com/mcoding Use coupon code MCODING at checkout for up to 91% off all yearly hosting plans! Your password database just leaked. What info does the hacker get and what can they do with it if you used plaintext passwords, hashed passwords, salted hashed passwords, or peppered salted hashed passwords? In this video we will talk about hashes, salts, and peppers, which can be used to more securely store passwords in your application's database. We use builtin secure Python primitives. Big open source libraries like Flask and Django use these techniques. Note: DO NOT WRITE YOUR OWN CRYPTO. This video is for educational purposes to explain the purpose and benefits of salting and peppering, it is not an example of secure production code. Note: Hash functions used in hashing passwords should be purposefully and configurably slow so that it takes an attacker a long time to check hashes even offline, making it more time-consuming to crack even weak passwords. ― mCoding with James Murphy (https://mcoding.io) Source code: https://github.com/mCodingLLC/VideosSampleCode Password storage best practices: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html Crypto.SE on peppering: https://security.stackexchange.com/questions/3272/password-hashing-add-salt-pepper-or-is-salt-enough Timing attacks video: https://youtu.be/XThL0LP3RjY SUPPORT ME ⭐ --------------------------------------------------- Patreon: https://patreon.com/mCoding Paypal: https://www.paypal.com/donate/?hosted_button_id=VJY5SLZ8BJHEE Other donations: https://mcoding.io/donate Top patrons and donors: John M, Laura M, Pieter G, Vahnekie, Sigmanificient BE ACTIVE IN MY COMMUNITY 😄 --------------------------------------------------- Discord: https://discord.gg/Ye9yJtZQuN Github: https://github.com/mCodingLLC/ Reddit: https://www.reddit.com/r/mCoding/ Facebook: https://www.facebook.com/james.mcoding CHAPTER
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from mCoding · mCoding · 39 of 60

1 Goodbye, List! Type hinting standard collections - New in Python 3.9
Goodbye, List! Type hinting standard collections - New in Python 3.9
mCoding
2 Python's comma equals ,= operator?
Python's comma equals ,= operator?
mCoding
3 Finding Primes in Python with the Sieve of Eratosthenes
Finding Primes in Python with the Sieve of Eratosthenes
mCoding
4 Find the First Missing Positive Int | Hard Interview Question on LeetCode
Find the First Missing Positive Int | Hard Interview Question on LeetCode
mCoding
5 JSON Tutorial Python | Basic Python Recipes
JSON Tutorial Python | Basic Python Recipes
mCoding
6 Simulating Brownian Motion in Python
Simulating Brownian Motion in Python
mCoding
7 The Single Most Useful Decorator in Python
The Single Most Useful Decorator in Python
mCoding
8 The Fastest Way to Loop in Python - An Unfortunate Truth
The Fastest Way to Loop in Python - An Unfortunate Truth
mCoding
9 Numpy Array Broadcasting In Python Explained
Numpy Array Broadcasting In Python Explained
mCoding
10 Brownian Motion Single Path Zoom
Brownian Motion Single Path Zoom
mCoding
11 Brownian Motion Fractal Zoom
Brownian Motion Fractal Zoom
mCoding
12 Magic Methods - Making Python builtins work with your classes
Magic Methods - Making Python builtins work with your classes
mCoding
13 50 Million Primes In 5 Seconds - Segmented Sieve of Eratosthenes
50 Million Primes In 5 Seconds - Segmented Sieve of Eratosthenes
mCoding
14 The Hottest New Feature Coming In Python 3.10 - Structural Pattern Matching / Match Statement
The Hottest New Feature Coming In Python 3.10 - Structural Pattern Matching / Match Statement
mCoding
15 How Fast is Python's Sort? Performance Testing
How Fast is Python's Sort? Performance Testing
mCoding
16 C++ First Missing Int, faster than 100%!
C++ First Missing Int, faster than 100%!
mCoding
17 [April Fools 2021] Python 4.0! New old print, mandatory static typing, StackOverflow integration
[April Fools 2021] Python 4.0! New old print, mandatory static typing, StackOverflow integration
mCoding
18 Python dataclasses will save you HOURS, also featuring attrs
Python dataclasses will save you HOURS, also featuring attrs
mCoding
19 C++ Sudoku Solver in 7 minutes using Recursive Backtracking
C++ Sudoku Solver in 7 minutes using Recursive Backtracking
mCoding
20 Every PROOF you've seen that .999... = 1 is WRONG
Every PROOF you've seen that .999... = 1 is WRONG
mCoding
21 Python's sharpest corner is ... plus equals? (+=)
Python's sharpest corner is ... plus equals? (+=)
mCoding
22 Binary Search - A Different Perspective | Python Algorithms
Binary Search - A Different Perspective | Python Algorithms
mCoding
23 The Best Way to Check for Optional Arguments in Python
The Best Way to Check for Optional Arguments in Python
mCoding
24 Local and Global Variable Lookup Weirdness in Python
Local and Global Variable Lookup Weirdness in Python
mCoding
25 Efficient Exponentiation
Efficient Exponentiation
mCoding
26 How To Install Python for Data Science
How To Install Python for Data Science
mCoding
27 0.1 + 0.2 is NOT 0.3 in Most Programming Languages
0.1 + 0.2 is NOT 0.3 in Most Programming Languages
mCoding
28 Python 3.10's new type hinting features
Python 3.10's new type hinting features
mCoding
29 Python 3.10's Quality of Life improvements
Python 3.10's Quality of Life improvements
mCoding
30 Introducing mZips! Python Zip and Zip Longest
Introducing mZips! Python Zip and Zip Longest
mCoding
31 Match statement tips
Match statement tips
mCoding
32 Using except: is a HUGE mistake
Using except: is a HUGE mistake
mCoding
33 Python + YouTube API | Automating descriptions
Python + YouTube API | Automating descriptions
mCoding
34 Anaphones, phonetic anagrams
Anaphones, phonetic anagrams
mCoding
35 Cracking passwords using ONLY response times | Secure Python
Cracking passwords using ONLY response times | Secure Python
mCoding
36 Python f-strings can do more than you thought. f'{val=}', f'{val!r}', f'{dt:%Y-%m-%d}'
Python f-strings can do more than you thought. f'{val=}', f'{val!r}', f'{dt:%Y-%m-%d}'
mCoding
37 Diagnose slow Python code. (Feat. async/await)
Diagnose slow Python code. (Feat. async/await)
mCoding
38 Python MD5 implementation
Python MD5 implementation
mCoding
Salting, peppering, and hashing passwords
Salting, peppering, and hashing passwords
mCoding
40 x to bool conversion in Python, C++, and C
x to bool conversion in Python, C++, and C
mCoding
41 You should put this in all your Python scripts | if __name__ == '__main__': ...
You should put this in all your Python scripts | if __name__ == '__main__': ...
mCoding
42 Find the Skyline Problem with C++ Solution Explained
Find the Skyline Problem with C++ Solution Explained
mCoding
43 The ONLY C keyword with no C++ equivalent
The ONLY C keyword with no C++ equivalent
mCoding
44 Should you use "not not x" instead of "bool(x)" in Python? (NO!)
Should you use "not not x" instead of "bool(x)" in Python? (NO!)
mCoding
45 Multiple Assignments in Python
Multiple Assignments in Python
mCoding
46 Why I don't like Python's chained comparisons
Why I don't like Python's chained comparisons
mCoding
47 Automated Testing in Python with pytest, tox, and GitHub Actions
Automated Testing in Python with pytest, tox, and GitHub Actions
mCoding
48 You can pip install directly from GitHub
You can pip install directly from GitHub
mCoding
49 __new__ vs __init__ in Python
__new__ vs __init__ in Python
mCoding
50 Metaclasses in Python
Metaclasses in Python
mCoding
51 The easy way to keep your repos tidy.
The easy way to keep your repos tidy.
mCoding
52 Which Python @dataclass is best? Feat. Pydantic, NamedTuple, attrs...
Which Python @dataclass is best? Feat. Pydantic, NamedTuple, attrs...
mCoding
53 Python __slots__ and object layout explained
Python __slots__ and object layout explained
mCoding
54 C++ cache locality and branch predictability
C++ cache locality and branch predictability
mCoding
55 Avoiding import loops in Python
Avoiding import loops in Python
mCoding
56 25 nooby Python habits you need to ditch
25 nooby Python habits you need to ditch
mCoding
57 Python staticmethod and classmethod
Python staticmethod and classmethod
mCoding
58 Building a Python app with Anvil to email me if my website goes down (includes paid features)
Building a Python app with Anvil to email me if my website goes down (includes paid features)
mCoding
59 31 nooby C++ habits you need to ditch
31 nooby C++ habits you need to ditch
mCoding
60 Interviewing the creator of C++, Bjarne Stroustrup
Interviewing the creator of C++, Bjarne Stroustrup
mCoding

Related AI Lessons

Up next
This Cop Was Held Accountable For His Brutality! #police #lawyer
Hampton Law
Watch →