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
121
votes
7 answers

Is there an MD5 Fixed Point where md5(x) == x?

Is there a fixed point in the MD5 transformation, i.e. does there exist x such that md5(x) == x?
Jack
119
votes
7 answers

How is a JavaScript hash map implemented?

I currently work with OpenLayers and have a huge set of data to draw into a vector layer (greater than 100000 vectors). I'm now trying to put all these vectors into a JavaScript hash map to analyze the performance. I want to know how is the hash…
Patrick Hillert
  • 2,309
  • 4
  • 22
  • 37
119
votes
4 answers

Java Equivalent to Python Dictionaries

I am a long time user of Python and really like the way that the dictionaries are used. They are very intuitive and easy to use. Is there a good Java equivalent to python's dictionaries? I have heard of people using hashmaps and hashtables. Could…
slimbo
  • 2,699
  • 4
  • 25
  • 36
118
votes
4 answers

Strange, unexpected behavior (disappearing/changing values) when using Hash default value, e.g. Hash.new([])

Consider this code: h = Hash.new(0) # New hash pairs will by default have 0 as values h[1] += 1 #=> {1=>1} h[2] += 2 #=> {2=>2} That’s all fine, but: h = Hash.new([]) # Empty array as default value h[1] <<= 1 #=> {1=>[1]} ←…
Valentin V
  • 24,971
  • 33
  • 103
  • 152
115
votes
3 answers

Magic number in boost::hash_combine

The boost::hash_combine template function takes a reference to a hash (called seed) and an object v. According to the docs, it combines seed with the hash of v by seed ^= hash_value(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); I can see that this…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
115
votes
12 answers

Compile time string hashing

I have read in few different places that using C++11's new string literals it might be possible to compute a string's hash at compile time. However, no one seems to be ready to come out and say that it will be possible or how it would be done. Is…
deft_code
  • 57,255
  • 29
  • 141
  • 224
115
votes
12 answers

How to generate an MD5 file hash in JavaScript/Node.js?

How to write functionToGenerateMD5hash for this code? I already have fileVideo and I need to send the corresponding md5 hash to the server by clicking on the button. $("#someButton").click(function() { var fr = new FileReader(); fr.onload =…
Taras Kravets
  • 1,443
  • 4
  • 14
  • 15
113
votes
13 answers

Efficiently generate a 16-character, alphanumeric string

I'm looking for a very quick way to generate an alphanumeric unique id for a primary key in a table. Would something like this work? def genKey(): hash = hashlib.md5(RANDOM_NUMBER).digest().encode("base64") alnum_hash =…
ensnare
  • 40,069
  • 64
  • 158
  • 224
112
votes
5 answers

Hash table runtime complexity (insert, search and delete)

Why do I keep seeing different runtime complexities for these functions on a hash table? On wiki, search and delete are O(n) (I thought the point of hash tables was to have constant lookup so what's the point if search is O(n)). In some course…
user1136342
  • 4,731
  • 10
  • 30
  • 40
111
votes
13 answers

How to generate short uid like "aX4j9Z" (in JS)

For my web application (in JavaScript) I want to generate short guids (for different objects - that are actually different types - strings and arrays of strings) I want something like "aX4j9Z" for my uids (guids). So these uids should be lightweight…
WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181
111
votes
10 answers

The necessity of hiding the salt for a hash

At work we have two competing theories for salts. The products I work on use something like a user name or phone number to salt the hash. Essentially something that is different for each user but is readily available to us. The other product…
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
111
votes
7 answers

SHA-256 or MD5 for file integrity

I know that SHA-256 is favored over MD5 for security, etc., but, if I am to use a method to only check file integrity (that is, nothing to do with password encryption, etc.), is there any advantage of using SHA-256? Since MD5 is 128-bit and SHA-256…
Dave
  • 8,163
  • 11
  • 67
  • 103
109
votes
17 answers

How do I extract a sub-hash from a hash?

I have a hash: h1 = {:a => :A, :b => :B, :c => :C, :d => :D} What is the best way to extract a sub-hash like this? h1.extract_subhash(:b, :d, :e, :f) # => {:b => :B, :d => :D} h1 #=> {:a => :A, :c => :C}
sawa
  • 165,429
  • 45
  • 277
  • 381
109
votes
7 answers

How to add to an existing hash in Ruby

In regards to adding an key => value pair to an existing populated hash in Ruby, I'm in the process of working through Apress' Beginning Ruby and have just finished the hashes chapter. I am trying to find the simplest way to achieve the same…
Tom
  • 2,065
  • 5
  • 20
  • 21
108
votes
8 answers

How do I combine hash values in C++0x?

C++0x adds hash<...>(...). I could not find a hash_combine function though, as presented in boost. What is the cleanest way to implement something like this? Perhaps, using C++0x xor_combine?
Neil G
  • 32,138
  • 39
  • 156
  • 257