Grokking Algorithms



Yüklə 348,95 Kb.
Pdf görüntüsü
səhifə106/122
tarix05.12.2023
ölçüsü348,95 Kb.
#173611
1   ...   102   103   104   105   106   107   108   109   ...   122
grokking-algorithms-illustrated-programmers-curious

The SHA algorithms
Do you remember hashing from chapter 5? Just to recap, suppose you 
have a key, and you want to put the associated value in an array.
You use a hash function to tell you what slot to put the value in.
And you put the value in that slot.


214
Chapter 11
 
 
I
 
 
Where to go next
This allows you to do constant-time lookups. When you want to know 
the value for a key, you can use the hash function again, and it will tell 
you in O(1) time what slot to check. 
In this case, you want the hash function to give you a good distribution. 
So a hash function takes a string and gives you back the slot number for 
that string. 
Comparing files
Another hash function is a secure hash algorithm (SHA) function. 
Given a string, SHA gives you a hash for that string.
The terminology can be a little confusing here. SHA is a 
hash function

It generates a 
hash
, which is just a short string. The hash function for 
hash tables went from string to array index, whereas SHA goes from 
string to string. 
SHA generates a different hash for every string.
Note
SHA hashes are long. They’ve been truncated here.
You can use SHA to tell whether two files are the same. This is useful 
when you have very large files. Suppose you have a 4 GB file. You want 
to check whether your friend has the same large file. You don’t have to 
try to email them your large file. Instead, you can both calculate the 
SHA hash and compare it.


215
The SHA algorithms
Checking passwords
SHA is also useful when you want to compare strings without revealing 
what the original string was. For example, suppose Gmail gets hacked, 
and the attacker steals all the passwords! Is your password out in the 
open? No, it isn’t. Google doesn’t store the original password, only the 
SHA hash of the password! When you type in your password, Google 
hashes it and checks it against the hash in its database.
So it’s only comparing hashes—it doesn’t have to store your password! 
SHA is used very commonly to hash passwords like this. It’s a one-way 
hash. You can get the hash of a string.


216

Yüklə 348,95 Kb.

Dostları ilə paylaş:
1   ...   102   103   104   105   106   107   108   109   ...   122




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin