Reputation: 31
I am modeling the user sign-in and account creation of a social network and have to create a hash function (not using hashlib) using my own hashing algorithm. The point of it is to take a password and hash it so that it becomes a random string of letters and numbers. The hashed password should also change dramatically when only one letter of the password is changed. For example, if "heyguys" goes to 7h8362, "hayguys" would go to something totally different like "bbb362". A small change in input string should result in a large change in output string. The reason I am doing this is because I am storing user data in a dictionary and it is dangerous to store a password in plaintext.
How would I go about doing this? I am a beginner and know hashlib but other than that, I cannot seem to figure out where to even begin.
Upvotes: 2
Views: 4605
Reputation: 2968
As others have said here, this is an advanced topic, and you shouldn't try to make a feasible Hash function unless you know what you're doing.
However, if you want to understand the basics of hashing, here are some things to think about.
Equivalent output: In every Hash function, you should be able to get the same output for every input that is identical to each other, such that, hash(8) = 'y758tff'
should be 'y758tff'
every time hash(8)
is called.
Avoiding Collisions: Good Hashing functions give unique outputs for as many inputs as possible. Meaning, Hash(n)
and Hash(x)
, should not give the same Hash output, and if it has to happen, it should be very rare.
Irreversibility: A good hash function, will be near impossible to reverse back to its key. Meaning, for every Hash(n) = N
, there should be no function so that function(N) = n
. As an example, if you had a hash function that simply reverses the input, it would be very easy to make a function that reverses that Hash output.
Identical lengths of keys: Regardless of the length of an input for a good hash function, the output must be the same length of all inputs. Such that, Hash('a') = '46fhur78'
, and Hash('Tomatoes') = 'yfih78rr'
, both length of 8.
Upvotes: 4