Questions tagged [hash]

A hash function is any well-defined procedure or mathematical function that converts a large amount of data into a small datum, usually a single integer. For questions about hashtags as used to label content on social media, use hashtag. For questions about URLs and HTML anchors, use fragment-identifier. For questions about Ruby's hash type, use ruby-hash.

A is any well-defined procedure or mathematical function that converts a large, possibly variable-sized amount of data into a small datum, usually a single integer that may serve as an index to an array. The values returned by a hash function are called hash values, s, hash sums, checksums or simply hashes. A occurs when two unequal datums generate the same hash code with a particular hash function. This can have various negative effects and good hash functions minimize the number of collisions.

For data structures that make use of hash functions and hashcodes, see , , , , and .

A cryptographically strong hash function has two additional features: it is mathematically proven to be irreversible and minimizes collisions. Irreversibility means that the original data cannot be reconstructed from its hash. For questions specifically about cryptographically secure uses of hash functions, use combined with the tag. Contrast with , which must be reversible.

Hash functions are related to (and often confused with) checksums, check digits, fingerprints, randomization functions, and error-correcting codes. Although these concepts overlap to some extent (some hash functions are specifically designed to also serve as checksums), each has its own uses and requirements and is designed and optimized differently.

For questions about hashtags as used to label and navigate content on social media, use . For questions about URLs and HTML anchors, use . For questions about Ruby's hash type, use .

23903 questions
137
votes
5 answers

What does hash do in python?

I saw an example of code that where hash function is applied to a tuple. As a result it returns a negative integer. I wonder what does this function do? Google does not help. I found a page that explains how hash is calculated but it does not…
Roman
  • 124,451
  • 167
  • 349
  • 456
137
votes
14 answers

How to create a HashMap with two keys (Key-Pair, Value)?

I have a 2D array of Integers. I want them to be put into a HashMap. But I want to access the elements from the HashMap based on Array Index. Something like: For A[2][5], map.get(2,5) which returns a value associated with that key. But how do I…
Crocode
  • 3,056
  • 6
  • 26
  • 31
135
votes
3 answers

How to implement a good __hash__ function in python

When implementing a class with multiple properties (like in the toy example below), what is the best way to handle hashing? I guess that the __eq__ and __hash__ should be consistent, but how to implement a proper hash function that is capable of…
abahgat
  • 13,360
  • 9
  • 35
  • 42
131
votes
9 answers

MD5 is 128 bits but why is it 32 characters?

I read some docs about md5, it said that its 128 bits, but why is it 32 characters? I can't compute the characters. 1 byte is 8 bits if 1 character is 1 byte then 128 bits is 128/8 = 16 bytes right? EDIT: SHA-1 produces 160 bits, so how many…
hash_jr90
  • 1,313
  • 2
  • 9
  • 4
130
votes
4 answers

Making a python user-defined class sortable, hashable

What methods need to be overridden/implemented when making user-defined classes sortable and/or hashable in python? What are the gotchas to watch out for? I type dir({}) into my interpreter to get a list of methods on built-in dicts. Of those, I…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
129
votes
9 answers

Salt and hash a password in Python

This code is supposed to hash a password with a salt. The salt and hashed password are being saved in the database. The password itself is not. Given the sensitive nature of the operation, I wanted to make sure everything was kosher. import…
Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258
129
votes
11 answers

What integer hash function are good that accepts an integer hash key?

What integer hash function are good that accepts an integer hash key?
Lear
  • 1,893
  • 2
  • 13
  • 9
129
votes
14 answers

How do I compare two hashes?

I am trying to compare two Ruby Hashes using the following code: #!/usr/bin/env ruby require "yaml" require "active_support" file1 = YAML::load(File.open('./en_20110207.yml')) file2 = YAML::load(File.open('./locales/en.yml')) arr =…
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
128
votes
16 answers

How to create a laravel hashed password

I am trying to create an hashed password for Laravel. Now someone told me to use Laravel hash helper but I can't seem to find it or I'm looking in the wrong direction. How do I create a laravel hashed password? And where? Edit: I know what the code…
Graham
  • 1,850
  • 4
  • 21
  • 41
127
votes
8 answers

How to find the key of the largest value hash?

I have the following hash {"CA"=>2, "MI"=>1, "NY"=>1} How can I return the maximum key value pair using ruby? I would like it to return "CA"
JZ.
  • 21,147
  • 32
  • 115
  • 192
127
votes
8 answers

Hash string in c#

I have a problem when trying get a hash string in c#. I already tried a few websites, but most of them are using files to get the hash. Others that are for strings are a bit too complex. I found examples for Windows authentication for web like…
Edy Cu
  • 3,262
  • 5
  • 20
  • 19
123
votes
3 answers

Meaning of Open hashing and Closed hashing

Open Hashing (Separate Chaining): In open hashing, keys are stored in linked lists attached to cells of a hash table. Closed Hashing (Open Addressing): In closed hashing, all keys are stored in the hash table itself without the use of linked…
hareendra reddy
  • 1,542
  • 2
  • 14
  • 17
122
votes
5 answers

Why can a Python dict have multiple keys with the same hash?

I am trying to understand the Python hash function under the hood. I created a custom class where all instances return the same hash value. class C: def __hash__(self): return 42 I just assumed that only one instance of the above class…
Praveen Gollakota
  • 37,112
  • 11
  • 62
  • 61
122
votes
3 answers

How to specialize std::hash::operator() for user-defined type in unordered containers?

To support user-defined key types in std::unordered_set and std::unordered_map one has to provide operator==(Key, Key) and a hash functor: struct X { int id; /* ... */ }; bool operator==(X a, X b) { return a.id == b.id; } struct…
René Richter
  • 3,839
  • 3
  • 34
  • 42
122
votes
9 answers

What's the safest way to iterate through the keys of a Perl hash?

If I have a Perl hash with a bunch of (key, value) pairs, what is the preferred method of iterating through all the keys? I have heard that using each may in some way have unintended side effects. So, is that true, and is one of the two following…
Rudd Zwolinski
  • 26,712
  • 17
  • 57
  • 60