What should I observe when creating a hash code?

Asked

Viewed 284 times

6

Still talking about the reply about hash code it was not clear what should be observed to produce good hash codes. I understood that it needs to be a number that doesn’t generate a lot of repetition, but just this?

3 answers

7


I went to see the blog from Eric Lippert where he has legal information:

  • it must always generate the same code for the same object
  • if two objects have identical characteristics should generate the same code, in a way we can say that it is the opposite of the GUID
  • ideally it should use only immutable fields of the type, perhaps until the whole object is immutable. In fact it is common to use types by value that are almost always immutable, strings which are immutable, and more possibly other immutable types. Another reason why the GetHashCode() should not be part of all objects of language as is what occurs by being defined in Object.
  • Do not throw or let throw exceptions during your run, you have to return an entire every time
  • do not do something very complex, has to be very fast, avoid even complexity O(n), although it is not always possible, as the case of string
  • it is worth noting that the calculation should generate a number without a fixed criterion, the ideal is that it does not look like a sequence, the more distributed, the better
  • one of the best known attacks is to make a hash table always have the same data forcing O(1) to go to O(n) which can create huge complications, so avoid the use of data that you have no control on hash Tables.

They are not obligations, but in almost all cases it is better to do so.

3

in my opinion, a good hashing algorithm should:

  • define a value for the protocol and for the data size to be used;

let’s assume for example that wants to hash six digits with a hexadecimal encoding. must:

  • perform a power operation to define a range scope (which in this case will be something identical to 2 6, or 6 2);
  • iterate over all dimensions of this range (which in this case would be something like 32 or 64);
  • when iterating over each of the indices, must convert the value of integer, to hexadecimal;

by doing this, you can get all the permutation keys from that hash can also do the same with ascii protocol, or for any other data representation system.

0

a good simple and easy way to create a hash code in Unix is:

  • using the pwgen command, which can be used to create passwords (which has to be installed in the shell with a package manager for the system -> Homebrew for mac, yum, apt-get, Pacman and many others for linux).

can be called pwgen by in any other backend language, usand a method, object, etc. to invoke Unix commands, and return them to the console of the website

to generate a hash with pwgen:

  • install pwgen on your operating system using a compatible package manager (Brew install pwgen for macos);
  • call pwgen, using a command like: pwgen 20 1, which should return an identity hash to Ruzooyae6chiiQuei5ah

So the only thing you have to do is figure out how to invoke shell commands from c#, invoke the pwgen command, and arm the output in a variable that you will then use somewhere in the program. the answer to something identical can be found here:

  • provide an example of how to generate the hash please, so we all learn.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.