1. Home
  2. Explore
  3. Computer Science
  4. Securing Stored Passwords

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Securing Stored Passwords

Transcript

[MUSIC PLAYING] DAVID J. MALAN: All right. This is CS50's Introduction to Cybersecurity. My name is David Malan, and this week, we'll focus on securing data. Last week, recall, we focused on accounts, and particularly one of the mechanisms by which we protect our accounts is generally by way of these things called passwords. But we focused last time really on our having the responsibility to keep these things secure. And yet, there's another party involved whenever

you have an account with a username and a password, and that's the server or app that is actually storing that password in some form long-term so that you can actually authenticate yourself-- that is, prove to this application or website that you are who you claim to be. Well, in the simplest form, perhaps these servers that are storing our usernames and passwords for which we have registered or maybe doing something very simple like this. For instance, if a website or app has two users at the moment, at least, Alice and Bob, and suppose for simplicity that Alice's password is Apple and Bob's password is banana, you could imagine that a website or that app, simply storing in a very simple text file these key value pairs-- username, colon, password, new line. Username, colon, password, new line. And in fact, that's actually very commonly how passwords are stored on systems, at least certain operating

systems like Linux, not necessarily as simply as this. They often have a little more information off to the right there, but in essence, it's the username and password. But this wouldn't be a good thing to store the passwords exactly like this. Why? Well, suppose that this website or this app and its database are somehow hacked by an adversary. That if someone gains access to that file containing these usernames and passwords, well, at that point, they literally have everyone's username and password. And we talked last time about attacks like credential stuffing whereby an adversary, once they know your username and password on one system, they can try stuffing that username and password into other systems, other websites, other apps just in hopes that you are, unfortunately, using the same username and password elsewhere as well. So this is generally not a good thing if an adversary gets access to everyone's usernames and passwords.

And even though, of course, in an ideal world, that would never happen, we should probably, as the administrators, as the creators of this website or app, we should probably do everything we can to at least minimize the fallout, the downsides, the damages that might result if, and daresay, when our database or this text file here are somehow compromised. So how might we go about doing that here? Rather than just storing apple and banana in clear text, so to speak, literally in the English words themselves, why don't we go ahead and employ a technique known as hashing? Now if you've studied computer science before, you might actually know this phrase in the context of hash tables and data structures. Well, it turns out the idea in this world of securing data is very similar, and in fact, this is a technique that's incredibly common for solving all sorts of problems. Well, what do we mean by hashing in this context?

Hashing is the process of taking a password as input and somehow converting it to a so-called hash or hash value. Now these hash values don't look like English. They're typically strings of text that might have letters, might have numbers, but they're generally of some fixed length typically. And in this case here, when we go about taking our password as input, converting it somehow via an algorithm or some code that we wrote, we want to convert it into this hash value and then store that hash value in that database of passwords instead. So here's a proverbial black box, and let's stipulate for the moment that I have no idea how hashing works, but I do know that this box can do it. So how do I think about this process? Well generally speaking, there's going to be some input to this box. Ultimately, I want to get some output from that box. And what this box really represents is, in fact, a hash function.

You can think of this as a device like some kind of machine; you can think of it like a program, some piece of software; or you can even think about it as a mathematical function that operates simply on numbers coming in as input. In fact, if you're mathematically inclined, though we won't use this syntax often, you can think of that hash function as being represented by f, you can think of the input as being represented by x, and you can think of the output of this process as being so-called f of x. If you're not familiar with that notation, that's fine, but this is directly connected hashing to basic mathematics as well that you might encounter before long. But what we care about is passing into this black box a password and getting out a hash, and then storing that hash and not the password in our database or text file of usernames and passwords. So how might we go about doing this?

Well, if I were to provide apple as an input to this hash function, let's think about the simplest hash function possible that doesn't output apple, but some representation of apple that I can eventually store in that database. So I'm going to propose very simply that maybe the a simplest hash function we can come up with-- and indeed, if you've studied computer science or taken CS50 itself, you might recall that we can hash our inputs unlike specific letters therein. So apple starts with A. So you know what? A is the first letter of the English alphabet. So I'm going to create a hash function here pictorially that outputs one whenever the input happens to start with an A, as does apple. Meanwhile, if we pass in banana, I'm going to have this hash function output 2 because B is the letter of the English alphabet. And dot-dot-dot, we might get to cherry or other passwords as well that might output 3 and beyond.

And you could imagine doing this for all letters of the English alphabet. Now unfortunately, this isn't the best hash function because it's fairly simplistic. And in fact, I can quickly think of some other fruits like avocados that also start with A and that would give me the same hash value. And that's actually a characteristic we'll come back to whereby when you hash values, there can actually be ambiguities, potentially, whereby two inputs might actually have the same output, and we'll consider eventually what the implications of that might be. But for now, I dare say that's a little too simplistic. And what might be better than outputting 1 or 2 or 3 is a little something more cryptic, because that's just too helpful. That's too much of a hint. If I see that your hash value is 1, I at least know that your password now clearly starts with an A, which means at best, I can do 1/26th the amount of work to figure out what it actually is.

So we want these hashes generally to be a little weird-looking and really unguessable and not leak any information. So for instance, a very common older hash function for apple might actually output this-- ..ekWXa83dhiA with some mixed uppercase and lowercase letters therein. Now it looks weird, you probably can't and shouldn't see any kind of pattern in there. There is a fancy math formula that took as input apple and outputted as its hash value that string of text there, but in and of itself, it doesn't really leak any information like the number 1 or 2 or 3 would. So we've already made an improvement. Banana, meanwhile, would look like this. And cherry, meanwhile, would look like that. So notice that these values are indeed quite different. So using this better hash function, I claim, that doesn't just look at the first letter of the alphabet, but looks at maybe all of the letters in the input--

C-H-E-R-R-Y in this case, we can probably come up with something more interesting, more cryptic-looking, if you will, like the values that we've just seen. So let me propose now that what we should do in our database of passwords is not store alice, apple, bob, banana, but let's instead store the hashes of apple and banana respectively. So instead in this password database, I'm going to store this instead. The exact same values that we just saw coming as outputs from that black box, but in this case now, I'm storing in my database of passwords usernames and hash values. Now why is this perhaps a good thing? Well, one, if someone now attacks this server and somehow gains access to all of these usernames and hashes, what they don't have is an entire list of passwords. So they can't quite as easily go about credential stuffing and figuring out maybe if this database will give me access to my accounts somewhere else. I'm at least creating some work for the adversary.

But at the same time, I feel like I've kind of broken the whole system because previously, presumably, when you log into a website or app and you type in your username and then you type in your password, what is the website or app probably do? Well, once that username and password are sent over the internet, typically to that server, well, the server probably compares what you typed in against the username and their database, or their text file, and the server compares what you typed in as your password against whatever password is in their database. But now we have a problem. We have you typing the username and we do have the username still in the database. Case in point, Alice and Bob are still here. But what we don't have is apple and banana. We've replaced those altogether with hashes. So even if you type in-- or Alice types in apple, well we don't want to compare A-P-P-L-E to this because it obviously

doesn't match; and Bob's banana, we don't want to compare against this because it's not going to match; and so forth. So what can we do? Well, the way authentication typically works on the server side when using hashing is as follows. When you first create an account or register for this website or app, you type in, if you're Alice, Alice, Enter, and then apple, for instance, Enter. That username, Alice, that password, Apple, are sent to the server. But what the server does before saving the username and password is it runs that hash function on Alice's password, which is apple, converts it thereafter to this value, and stores Alice's username and the hash of Alice's password only and throws away apple, deletes it, it forgets it in memory. What then happens next? Well, the next time Alice tries to log into this website-- maybe the next day, a week from then, a year from then for the second or third or more time, what happens?

Well, Alice types in Alice as her username, hopefully apple as her password, hits Enter, those get sent to the server as usual, and obviously the server can't just compare username against username and password against password because it doesn't have the password in its database, so what can the server do? The server can repeat the very same process, taking Alice's password as inputted, A-P-P-L-E, run it through the exact same hash function a day, a week, a year later, and then compare that resulting hash value to whatever is stored in this text file or database. And now admittedly, we're creating a whole lot more work for ourselves, but it's not that big a deal because this is just a math function, or if you know how to program, it's just a few lines of code that you've written in software that converts passwords to hash values. And honestly, nowadays, you wouldn't even rewriting most of this code

yourself, you'd be using a library, third-party code that someone else smarter than you, maybe, has written and gotten it just right, no bugs or mistakes, so you're just relying on someone else's code anyway to achieve this goal. But the upside now, to be clear, is if this file is compromised somehow, the server's hacked into and this data is leaked, at least they only know the usernames on your system, not the actual passwords. And let me pause here and see if there's any questions on this technique of hashing for passwords specifically. STUDENT: You said yourself, we are using libraries more often than write the hash functions ourselves if we are not taking the course on CS50. So then it's easy to hack these hashes, right? Because we can go through 10, 40, I don't know, hash functions that are available in the libraries, and then you can reverse the hash results, is that right? DAVID J. MALAN: Almost.

Can do exactly what you described first whereby you use the same library, the same code, to create hash values to then compare those against what's in the database, but generally, these hashes are not reversible, per se. You can compare them, but you can't reverse the process for reasons we'll come back to. But your intuition is right. And so really, the takeaway here is that we haven't made our system absolutely secure, we've made it relatively more secure. Why? Because we've increased the cost to the adversary, to the hacker. They now have to do more work to figure out what the actual passwords are if they want to benefit from this hack. So again, it just raises the bar, it does not keep the adversary necessarily out or even stop them from figuring out one person's password, but it might take them a lot more time, it might take them a lot more resources like server or cloud costs or money,

or it might even heighten the risk before they actually are successful. How about one other question here on hashing? STUDENT: If the password is intercepted before-- after the website is hacked and the password is intercepted before it's encrypted, so wouldn't that pose a problem? DAVID J. MALAN: Yes, absolutely. Then all bets are off. Everything we just discussed is not useful at all if the adversary has actually intercepted the password before it has even been hashed. Now thankfully, there's going to be solutions to that problem, too, and we'll come to them today, but for now, focusing only on hashes, it solves one problem but not all. In fact, it turns out that those attacks we talked about last time with respect to our accounts are still possible. You can still use a dictionary, for instance, of English words, or better yet, a dictionary of English fruits, and you could, one fruit at a time, run each of those values as input

into the same hash function, the library or code that you're using to achieve this, and then that's going to give you one hash value after another. And you could compare each of those hash values against whatever is in the database or the file of passwords that you, the hacker in this story, might have actually stolen somehow. You have to do more work though, because it's no longer as simple as just comparing apple against apple and banana against banana. You actually have to do some work. You have to do some computational work. And if the file is only a few values, of course, not a big deal. If it's thousands or millions of rows, it might actually take a lot more time, energy, and effort. So again, we're just raising the bar, but not keeping the adversary out altogether. And even if you don't have a dictionary available, and even if the passwords are not all fruits in English, well, you can still, as the adversary, resort to brute-force attacks.

And you can try even the simplest of passwords like 0000 or maybe eight 0's instead, and you can hash that and see what the resulting hash value is and compare that against what's in the database. Then you can try 00000001, hash that, compare that against what's in the database, and then move on to the next and the next, doing this not just for numbers, but for letters as well. A, A, A, A, A, A, A, A, A, hash that and compare. Eventually, apple will be on that list.

Eventually, banana will be on that list. But there, too, the brute force attack is still going to take some amount of time. So it's just increasing the cost or the complexity for the adversary in this particular case. But there's yet another threat that's possible in the context now of the hashes, which is worth knowing about. There's a term of art known as a rainbow table, which is a very beautiful way of saying that adversaries in advance might have already hashed all possible English words in a dictionary. Adversaries might have already hashed all possible passwords of length 4 or 5 or 6 or 7 or 8 or something else. And maybe if they have a big enough hard drive, they are storing a big table, like an Excel file or a CSV file of all of the words that they've tried, all of the passwords they've tried, and all of the hash values they've already computed. Then it's even easier.

Then they don't even need to do a brute-force attack, per se, hashing and hashing and hashing and hashing. Then they can just compare, compare, compare. Because indeed, a rainbow table simply contains all of the passwords they've tried, all of the hash values they've generated, and so they just compare left to right whatever the user typed in against the hash value they've already computed. Now for certain hash functions, this threat of a rainbow table is just not feasible. You might need terabytes or petabytes of data, which means a lot of hard drives and a lot of money, so there are potential downward pressures on this kind of an attack, but it can certainly speed things up. Certainly if you're pre-computing-- that is, pre-calculating some of the hashes for at least words in an English dictionary, and certainly some short list like all of the fruits in the world. But there's another problem that we might encounter on the server with regard to our passwords.

Alice might have a password of apple, Bob might have a password of banana, but suppose that both Carol and Charlie have a password of cherry. And just by coincidence, they both chose the same password and are in this same database. Now we've already concluded, I think, that we definitely don't want to store the plaintext passwords. We don't want to store literally in the clear apple, banana, cherry, and cherry because this is just too easy for the adversary to do bad things with it. So we at least want to hash this, but here's where hashing can leak information, so to speak. If I go ahead and use the same function I've been using to hash apple and banana and now cherry, what do you notice about Carol's and Charlie's hash values? Curiously, but maybe not surprisingly, they're exactly the same. That's, after all, how functions typically work, be it in math or in software, in code. If you pass the exact same input, unless there's some randomness going on,

you're going to get the same output again and again. Now why is this a big deal? Well, if some adversary attacks this database and gains access to all of these usernames and hashes, we have leaked information in the sense that the adversary, just by glancing at this file, knows that, OK, I don't know what Carol's password is or what Charlie's password is, but I know it's the same password, and that alone might be enough information to figure out with higher probability what it is. Maybe Carol and Charlie are related. So maybe you focus on words or numbers that are common to both of them. Maybe there's some information that's implied by this if they both are-- they both like the same TV shows, they both like the same movies. You can try to find, in your mind, maybe the intersection of information that might lead you, with higher probability, to figure out, without brute force, even, what Carol's password is and Charlie's password is.

So this is a common problem, and we only have four users in this database. You can imagine having many more. Odds are, some of us are going to have the same username-- not the same username, some of us are going to have the same passwords. In fact, without raising your hands or admitting to this for the whole world to see, do any of you have a password of 1234 In some website or app? Maybe a little harder? 12345678? Something very simple like this. Maybe it's an account you don't really care about. Well, that's a perfect example of where, if you have an account on the same system as someone else here in the classroom, you're going to have, in that database, presumably, the same hash values, and that might be alone enough information to leak and increase the probability that you, and not Alice or Bob, are actually compromised with respect to your account. So how can we fix this?

Well, it turns out there's another technique in the world of data that we can use to perturb this process. And you can think of it metaphorically as like sprinkling a little bit of salt on the hash function so as to change what its output is. It's not random, per se, but you are perturbing the output so that it's much less likely that two people with the same passwords are going to have the same hash value. So how does this work? In this case before, when we passed in cherry as our input, we got the same hash again and again. But let me propose that we modify our hash function to take two inputs now. Not just the password, but also a salt value, so to speak. A little bit of a sprinkling of, in this case, just two characters-- two numbers, two letters, or a combination thereof. Now this hash function that I'm describing is still going to output a hash value, but notice, it's different from the one before, and even if you don't quite remember what it was before, it was not this.

But worth noting is that in the output of this hash function now is the salt itself. So the salt isn't something that's meant to be private or secret or secure, it's just sprinkled in there to make sure that whatever hash value comes out of this black box is a little bit different than if you had put a different salt value instead. So for instance, suppose that for Carol and for Charlie, we use different salts. And that's the idea. Different users should have different salt values

just in case they choose the same passwords. So instead of 50 and cherry, suppose that Charlie uses a salt value of, say, 49. 49 is not a number that Charlie or you or me have to pick. This is all done by the server automatically, picking a random two characters like 4-9 or 5-0. But notice what just happened. If I rewind to cherry with a salt of 5, this was the hash value, the first two characters of which are the salt. If, though, I change the salt from 50 to 49, the hash changes completely,

and it prefixes it with now 49 instead of 50. This ensures that even if Carol and Charlie have the exact same password, there's no way I, the adversary, am going to know by looking at it. Because indeed, what ends up in the file now are these two values. One is prefixed with 50, one is prefixed with 49, the rest of the hash values clearly are completely different. So again, the upside is this approach where the hash function takes two inputs, the password and a salt, and then outputs one hash value means that we're not leaking information except-- except-- so there is a corner case-- if by chance, by bad luck, the system chooses the same salt for both Carol and Charlie, yes, there might still be information leaked. And honestly, that may very well happen if you've got thousands, millions of users, then you're going to run out of two-character possibilities, you're going to have to reuse salt. But the idea is that we're just trying to put

downward pressure on the probability of being attacked successfully. We're trying to equivalently raise the bar to the adversary so that they are not as likely to gain access to my data or, in turn, my account. Questions now on salting or hashing itself? STUDENT: Oh, I'm curious. Where do we store the salt? DAVID J. MALAN: So where do you store the salt? The salt is actually stored in the hash value itself, according to this algorithm, in the first two characters.

And the value of storing the salt in the first two characters of the hash is as follows. The next time Carol logs in, she types in her username, Carol, and hits Enter. The server now knows, OK, I'm expecting a password from Carol, let's see what she types in. Suppose that she types in correctly cherry. Now the system is not storing cherry, so it's not going to compare literally what Carol typed in, but it is going to hash cherry, but first, the system is going to check, what is Carol's hash-- what is Carol's salt? And it's going to infer as much by looking at Carol's hash value and looking only at the first two characters by convention. Then what the server is going to do, it's going to take whatever Carol typed in, cherry, C-H-E-R-R-Y, it's going to pass in 50, 5-0, and then hopefully, it's going to get back to this same value here, this whole string in yellow. And if those are correct, then Carol will be considered authenticated.

By contrast, if the username happens to be Charlie and Charlie hits Enter, then what the server is going to do is look at Charlie's hash value, grab the first two characters for Charlie's salt, use that salt and cherry as the input to the hash function, and hope that the result is Charlie's value, not Carol's. Really good question. Other questions on salting or hashing? STUDENT: Is there any sense in rehashing a password? So hashing it a first time to get a string, then rehashing it for a second string? Or it's just impractical? DAVID J. MALAN: No, you could certainly hash the value multiple times, but a good hash function should not require that of you. Especially now, more recent modern hashes, one of which we'll look at in a moment, they should have sufficiently calculated and proven characteristics that allow you to hash it just once and you will get a seemingly random string that represents whatever that input is.

And here, too, is where I should emphasize that when it comes to this world of hashing and salting and today's other topics ultimately, these are not wheels that you or I should be reinventing. Unless you are the researcher or the company that's actually developing the algorithm, stress-testing them, analyzing them theoretically and practically so often in industry or the real world, when people like you and me invent our own systems for storing information, we just haven't spent nearly as much time or we're just not nearly as sharp as some of the security researchers out there who have really given this some thought. So when it comes to all things security-- and let me get on my soapbox here and say, you and I should not be solving these problems unless it is your full-time job or calling in life. There's just too many corner cases unless you're collaborating with a smart team. All right. With that said, here is what hashes generally

look like nowadays in practice. For the sake of discussion, I deliberately chose a fairly simple hash function that was using a fairly short salt, just two characters, and a fairly short hash value as output. Here, in a smaller font, no less, is how Alice's and Bob's and Carol's and Charlie's passwords would probably be stored nowadays using a more recent modern hash function that, notice, by the shear length of the text on the screen, outputs a much larger value. If you're familiar from computer science with the notion of bits, 0's and 1's that are used to store information in systems, these hash values use many more bits, many more 0's and 1's. You and I as humans are seeing them as alphabetical letters and as numbers, but underneath the hood, these are just more and more 0's and 1's that the computer is storing, which means it's much, much less likely that someone who steals this kind of file is going to be able to figure out efficiently what

those original passwords were. And you can see, too, that for both Carol and Charlie, even though their passwords are still cherry, these two strings along the bottom look completely different. Except in one location here. It turns out that the scheme a lot of systems have adopted is that if you look between dollar signs at the beginning of what seems to be the hash value, you'll see a code like y or y or y or y or other numbers or letters as well. That's a little cheat sheet that tells the system exactly what hash function was used to generate the rest of it. And that's in the documentation that you can read online for any number of hash functions. So that's just to say, when you create an account on some new website or app, if they are doing things well in a manner consistent with best practices and they are being mindful of your security, they are probably in a file or in a database or some other mechanism storing

values that look quite like these based on whatever password you actually typed in. In fact, just to give you a sense of how easy or difficult it might be to crack passwords-- that is, figure out what they are based only on these hashes, in the case of our first hash function whereby we had a fairly short hash value being outputted with or without the salt, turns out, there's 18 quintillion possible hash values. Now that's a lot. That's bigger than last times quadrillion value.

But, with enough time, enough money, and enough cloud computing, those early hash functions can be broken. That is, with enough time and energy, you can probably figure out what someone's password is. If you fast forward to the other strings that I showed you on the screen, the much longer ones that use more bits, so to speak, then you have this many possible hash values nowadays. And I actually did look up how to pronounce this, but based on reading it on my screen, I wasn't actually sure how to say the word since this is a really big number that my mathematician colleagues could do a better job pronouncing. But given how many digits are on the screen, given how many commas are on the screen here, this is a really big number such that you and I probably don't need to worry about an adversary using brute force figuring out and still being able to figure out by the end of time what the corresponding password might be unless there are other weaknesses in the system.

Now speaking of weaknesses. Has anyone ever forgotten your password? Yes, of course. But have you ever gone to a website or app, clicked that link that says, Forgot Password, question mark, in hopes of getting an email of some sort so that you can reset the password? I mean, odds are, almost everyone here has experienced that. But has anyone ever clicked on that link, gotten back an email that actually contains your password so that you're just immediately reminded what it is? I'm seeing a few nods of the head. You can copy-paste it, then, into the website. Do not use that website anymore. That is evidence of-- that is a symptom of a website or application not practicing best practices. Why? Well, if it is the case that the website can email you your password, that means they can see and they know what your password is. That means this database, this text file we've been talking about is probably vulnerable to some hacker

eventually getting into it and stealing all of those usernames and passwords in the clear, no less. Because recall what these hashes are. They're generally meant to be irreversible. When you take as input apple, banana, and cherry, the output looks completely different with no obvious relationship to what those original passwords actually were. And so if that's what's being stored in the database, the company who made that website, the person who made that website or app, they should not be able to reverse that process either, otherwise surely, the adversary can. So it is the case, and I've experienced this myself, often from smaller shops or companies that maybe haven't really invested a lot of time or care into their website, if they are able to email you your original password, it is, by definition, not secure. And it's certainly not up to today's standards, it's just too easy for it to be compromised. So maybe minimally stop using that service

and make sure you're not using that password anywhere else. Maximally, maybe send them a note explaining your concern and maybe linking them to some reference online-- maybe this video-- in which you explain why you have that concern. Questions, then, on forgetting passwords or hashing or salting? STUDENT: So as you said, some companies may not be practicing these hashes and maybe practicing something very bad. So if I were, let's say, a company and I--

because of my practices, I had a leak of passwords and all the data, do I as a company have any obligations or responsibility for what happened since I have all the customer's data and all their passwords, do I have any obligations or responsibilities? DAVID J. MALAN: It's a really good, a noble question. The answer to that ethically is probably yes you should, quite simply. However, the more nuanced answer is that it's probably going to depend on the industry that you're in, the country that you're in, any regulatory requirements that your company faces which might oblige you to report out in that way. So I would read up on the context that's specific to you yourself. And I will say, unfortunately, it is not that common in the world, I dare say, that companies document and detail publicly when there have been security exploits. They might announce that something indeed has happened,

but it is rare that companies will go into any amount of detail. Now this is understandable because, one, they're already embarrassed, or if not in legal trouble or financial trouble because that has happened already, but they probably, typically, don't want to provide other adversaries-- other future attackers-- with more information about their systems and the weaknesses that those systems have. The downside, of course, societally, is that if each of us is secretly getting attacked in ways we didn't expect, learning things that would be ideal to share with others in the world. This itself is actually a big question in the world of cybersecurity, just how much and how often to share, especially when you discover a bug or a mistake in someone's system, do you tell them privately, do you tell the world publicly? These are ethical questions that we'll touch on indeed in the coming days as well.

Allow me to propose that separate from these concerns here, we can come back to some of those recommendations that we started the class with from this, the National Institute for Standards and Technology. Notice that this was one other quote we did not share last time. A recommendation from NIST is that "Verifiers shall store memorized secrets in the form that is resistant to offline attacks. Memorized secrets SHALL be salted and hashed using a suitable one-way key derivation function. Their purpose is to make each password guessing trial by an attacker who has obtained a password hash file expensive, and therefore, the cost of guessing attack-- of a guessing attack high or prohibitive." So when I refer to best practices, I'm really referring to actual documentation like this, either from the United States, from other countries, from other companies. There are indeed these best practices, and among our goals for this class is to expose you to some of those,

both on the consumer side-- you and me as individual computer users, but also on the corporate or the academic side as well as to what you should be doing when you are in a position of being responsible for someone else's data as well. Now as for the actual hash functions to use nowadays, these are just some of them that are generally recommended nowadays that can be categorized as SHA-2 and SHA-3. These refer to fairly sophisticated mathematical functions that take as input, typically, a password, or some input more generally, and then output a hash value thereof. There are other algorithms, too, that can even be used to verify the authenticity and integrity of messages as well. In fact, today, we'll also focus on how we can use primitives like these to ensure that data was not actually changed in transit when you sent it over the internet from one person to another. But ultimately, what we've been focusing on and what you've seen on this list

here are what are generally known as one-way hash functions. That is, these are mathematical functions, or, in the context of programming, these are functions written in code, languages like Python or otherwise, that take as input a string of arbitrary length. That is, a password that's this long, maybe this long, maybe this long, but what's key to these cryptographic functions is they output a hash value of fixed length that is always this many bytes or characters or this many bytes

or characters. That is, it doesn't matter how short or how long the password is, these cryptographic, these one-way hash functions are one-way in the sense that they take a potentially infinite domain, if you know this term for mathematics, and condense it into a finite range. That is, a huge number of values, all possible passwords in the world, to just a finite list of possible hash values. It might be a long list of possible hash values, but indeed, no matter how long a string of text is, if it's of some fixed length-- 16 characters, 32 characters, something else, there's only a finite number of those values. Now there's an implication of this. When you take a really large input space or domain mathematically and map it to a smaller finite range, so to speak, mathematically, it turns out that if you do try to reverse the process, there will be multiple inputs that yield the same output. Think about it this way. If you've got 100 possible passwords in the world,

but you only have 10 possible hash values-- so 100 passwords, 10 hash values, you have to figure out how to put all of those passwords into 10 buckets, so to speak. So surely, some of those passwords are going to be in the same bucket. Think about it in terms of the English alphabet. If we stuck with that original hash function where A was 1, B was 2, C was 3, presumably Z was 26, there's more than one fruit that starts with a-- apple, avocado, and so forth.

So there, too, you are going to have multiple fruits mapping to the same finite range of values, hash values 1 through 26. What that means is that if an adversary, or even you, the owner of the system, look at that hash value and see the number 1, you don't know if the password was apple or avocado or some other word that started with A. And so that's what we mean by one-way hash functions. You cannot reliably reverse the process by any means and know definitively what the original input is.

Now there is a catch. That technically means on some systems, it might be possible to log in with apple or avocado, or more generally, your actual password and some other seemingly random password that might make no sense to you, but just because mathematically it has the same hash value, that password, too, might let you into the system. But the idea is, especially as we're using really large numbers of bits, really long hash values, the probability of you or me figuring

out or an adversary even guessing what that other hash value or what those other inputs-- passwords might be is just so small that we tend not to worry about it as well. The algorithms we've looked at on the board here are also known as cryptographic hash functions, which means they have utility in the world of cryptography where the world of cryptography is all about the practice and the study of securing data. Securing data while in transit from one point to another or while at rest on your own system. Let's go ahead here and take a five-minute break, and when we come back, we'll explore precisely that world of cryptography with respect to our data. All right. We're back. And indeed, cryptography is all about the practice and study of securing our data, particularly when we want to transmit it from one person to another. So cryptography can be broken down into a couple of different categories, one of which are codes.

And codes are not the type of code that you might write in Python or the like. It has nothing to do with software, but rather, a mapping between what we'll call code words and the actual message or true reading that those words represent. Here, for instance, is an actual book from over 100 years ago that was used to map these code words in the left column to these, indeed, messages or true readings on the right. The idea is, that if that one party wanted to send a secure message to another party, they wouldn't just write it out in plain English. Why? Because if that message, written on a piece of paper or parchment, were intercepted by another human, that other human, assuming they, too, know English, could just read the actual message, the so-called plaintext. In a code, though, you can convert the words that you want to say to code words that make no sense necessarily to someone who's intercepted the message in and of itself

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent