What should I observe when creating a hash code?

5

Still talking about answer about hash code it was not clear what should be observed to produce good hash codes . I understand that it needs to be a number that does not generate a lot of repetition, but is this enough?

    
asked by anonymous 22.03.2017 / 13:16

3 answers

6

I went to Eric Lippert's blog where he has a legal information :

  • it should always generate the same code for the same object
  • If two objects have identical characteristics, it should generate the same code, so we can say that it is the opposite of the GUID
  • ideally it should use only immutable fields of the type, perhaps even the entire object is immutable. In fact, it is common to use value types that are almost always immutable, strings that are immutable, and most often other immutable types. One more reason why GetHashCode() should not be part of every object.
  • Do not throw or throw exceptions during execution, must return an integer
  • Do not do something very complex, it must be very fast, avoid even complexity O (N), although it is not always possible, as the case of string
  • It is worth emphasizing that the calculation must generate a number without a fixed criterion, ideally it does not look like a sequence, the more distributed, the better
  • One of the best-known DOS attacks is to have a hash table always have the same force as O (1) to go to O (N) which can create huge complications, so avoid the use of data that you have no control over hash tables .
22.03.2017 / 13:37
3
A good hashing algorithm should:

  • set a value for the protocol and for the data dimension to be used;
22.03.2017 / 13:42
0

A simple and easy way to create a hash code in unix is

  • Using the pwgen command, which can be used to create passwords (which must 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 another backend language, using a method, object, and so on. to recall unix commands, and return them to the console

to generate a hash with pwgen:

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

Then the only thing you need to do is find out how to call shell commands from C #, evoke the pwgen command, and build the output into a variable that you'll later use in the program . the answer to something identical can be found here:

04.02.2018 / 20:54