Questions tagged [hash-function]

A hash function is an algorithm that maps large data sets of keys to smaller data sets of a fixed length. Hash functions are not reversible.

The values returned by a hash function are called hash values, hash codes, hash sums, checksums or simply hashes.

Hash functions are mostly used to accelerate lookup times for data comparison tasks such as finding items in a hash table, database, detecting duplicated or similar records in a large file, etc.

Read more on Wikipedia.

264 questions
266
votes
15 answers

How does a Java HashMap handle different objects with the same hash code?

As per my understanding I think: It is perfectly legal for two objects to have the same hashcode. If two objects are equal (using the equals() method) then they have the same hashcode. If two objects are not equal then they cannot have the same…
akshay
  • 5,235
  • 14
  • 39
  • 49
188
votes
9 answers

Maximum length for MD5 input/output

What is the maximum length of the string that can have md5 hashed? Or: If it has no limit, and if so what will be the max length of the md5 output value?
Arun David
  • 2,714
  • 3
  • 18
  • 18
158
votes
1 answer

Why does Git use a cryptographic hash function?

Why does Git use SHA-1, a cryptographic hash function, instead of a faster non-cryptographic hash function? Related question: Stack Overflow question Why does Git use SHA-1 as version numbers? asks why Git uses SHA-1 as opposed to sequential numbers…
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
36
votes
5 answers

How can I use a C++ unordered_set for a custom class?

How can I store objects of a class in an unordered_set? My program needs to frequently check if an object exists in this unordered_set and if it does, then do some update on that object. I have looked up online on how to use unordered_set, but sadly…
daydayup
  • 2,049
  • 5
  • 22
  • 47
28
votes
3 answers

How to write a hash function in C?

Hash Tables are said to be the fastest/best way of Storing/Retrieving data. My understanding of a hash table, hashing is as follows (Please correct me if I am wrong or Please add If there is anything more): A Hash Table is nothing but an array…
aks
  • 4,695
  • 8
  • 36
  • 37
27
votes
2 answers

Are there no specializations of std::hash for standard containers?

I just found myself a little bit surprised being unable to simply use a std::unordered_set > test; because there does not seem to be a std::hash specialization for std::arrays. Why is that? Or did I simply not find it? If there…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
25
votes
7 answers

Hash function for floats

I'm currently implementing a hash table in C++ and I'm trying to make a hash function for floats... I was going to treat floats as integers by padding the decimal numbers, but then I realized that I would probably reach the overflow with big…
Pacane
  • 20,273
  • 18
  • 60
  • 97
22
votes
3 answers

unordered_map hash function c++

I need to define an unordered_map like this unordered_map, *Foo>, what is the syntax for defining and passing a hash and equal functions to this map? I've tried passing to it this object: class pairHash{ public: long…
MoonBun
  • 4,322
  • 3
  • 37
  • 69
20
votes
2 answers

"Fastest" hash function implemented in Java, comparing part of file

I need to compare two different files of the instance "File" in Java and want to do this with a fast hash function. Idea: - Hashing the 20 first lines in File 1 - Hashing the 20 first lines in File 2 - Compare the two hashes and return true if those…
carloscloud
  • 351
  • 1
  • 3
  • 14
20
votes
4 answers

How to specialize std::hash for user defined types?

The Question What is a good specialization of std::hash for use in the third template parameter of std::unordered_map or std::unordered_set for a user defined type for which all member data types already have a good specialization of std::hash? For…
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
19
votes
4 answers

Hash function for a pair of long long?

I need to map a pair of long long to a double, but I'm not sure what hash function to use. Each pair may consist of any two numbers, although in practice they will usually be numbers between 0 and about 100 (but again, that's not guaranteed). Here…
Frank
15
votes
3 answers

Why does HashMap need a cryptographically secure hashing function?

I'm reading a Rust book about HashMap hashing functions, and I can't understand these two sentences. By default, HashMap uses a cryptographically secure hashing function that can provide resistance to Denial of Service (DoS) attacks. This is not…
fraillt
  • 288
  • 3
  • 8
12
votes
3 answers

A good hash function to use in interviews for integer numbers, strings?

I have come across situations in an interview where I needed to use a hash function for integer numbers or for strings. In such situations which ones should we choose ? I've been wrong in these situations because I end up choosing the ones which…
phoenix
  • 3,531
  • 9
  • 30
  • 31
12
votes
6 answers

What are the important points about cryptographic hash functions?

I was reading this question on MD5 hash values and the accepted answer confuses me. One of the main properties, as I understand it, of a cryptopgraphic hash function is that it is infeasible to find two different messages (inputs) with the same…
Rob Sobers
  • 20,737
  • 24
  • 82
  • 111
11
votes
4 answers

Hash Function For Sequence of Unique Ids (UUID)

I am storing message sequences in the database each sequence can have up to N number of messages. I want to create a hash function which will represent the message sequence and enable to check faster if message sequence exists. Each message has a…
Anwar Shaikh
  • 1,591
  • 3
  • 22
  • 43
1
2 3
17 18