r/javahelp • • May 17 '20

What is a hash?

Need a body to post.

0 Upvotes

4 comments sorted by

3

u/MarkSlayer456 May 17 '20

A hash is a function that is given a variable typically a string and it returns a number by using data from the variable the simplest form of a hash is when it accepts a number and you mod the number so it is in a range you want!

1

u/CheeseGr0ve May 17 '20

Ah alright thanks that makes sense!

5

u/seanprefect Grumpy Guy Who Remembers Java 2 May 17 '20

Ohh something i'm actually kinda an expert in.

So in it's simplest form a hash is a kind of "signature" or digest of something. The core concept is you feed it an arbitrary input and you get a fixed length output. now this fixed length output is supposed to be reasonably unique (given you can put any length of input there are infinite possibilities and the output is fixed length and therefore by definition they cannot be unique)

Now why is this important? well it's important in a lot of contexts, in java a hash of an object is useful as a signature that lets you differentiate different objects.

That said the far more common use case is for cryptography. A cryptographic hash is a hash however it's got a couple defining characteristics. First a cryptographic hash cannot be reversed, as in there should be no way to use the hash output to get back to the input.

The second important feature of a cryptographic hash is that even the slightest variation in input should result in a completely different output.

Now why are those important? well they are used in several contexts but i'll give you a common one that's important.

So it's bad security practice to ever store passwords and it should never be done. You should never store a user's password ever in any way.

Now how can you do this ? you need the password to login right? well that's where the hash comes in you store the hash of the passwords. Now if they get the hashes they :

  1. cannot figure out the original passwords

2)they can't do any meta analytics because a password of cat would be wildly different than a password of cab and therefore you couldn't figure out things by patterns.

I hope that helps ?