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 .

22309 questions
1209
votes
14 answers

Secure hash and salt for PHP passwords

It is currently said that MD5 is partially unsafe. Taking this into consideration, I'd like to know which mechanism to use for password protection. This question, Is “double hashing” a password less secure than just hashing it once? suggests that…
luiscubal
  • 23,581
  • 8
  • 51
  • 82
1042
votes
34 answers

How can I generate an MD5 hash?

Is there any method to generate MD5 hash of a string in Java?
Akshay
  • 11,303
  • 5
  • 27
  • 26
700
votes
24 answers

Generate a Hash from string in Javascript

I need to convert strings to some form of hash. Is this possible in JavaScript? I'm not utilizing a server-side language so I can't do it that way.
Freesnöw
  • 25,654
  • 28
  • 83
  • 131
682
votes
5 answers

How can bcrypt have built-in salts?

Coda Hale's article "How To Safely Store a Password" claims that: bcrypt has salts built-in to prevent rainbow table attacks. He cites this paper, which says that in OpenBSD's implementation of bcrypt: OpenBSD generates the 128-bit bcrypt salt…
Nathan Long
  • 113,812
  • 91
  • 316
  • 418
516
votes
13 answers

Fundamental difference between Hashing and Encryption algorithms

I see a lot of confusion between hashes and encryption algorithms and I would like to hear some more expert advice about: When to use hashes vs encryptions What makes a hash or encryption algorithm different (from a theoretical/mathematical…
Kenny Cason
  • 11,382
  • 9
  • 42
  • 71
515
votes
13 answers

Why does Java's hashCode() in String use 31 as a multiplier?

Per the Java documentation, the hash code for a String object is computed as: s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates…
jacobko
  • 8,090
  • 7
  • 29
  • 36
510
votes
16 answers

How does a hash table work?

I'm looking for an explanation of how a hash table works - in plain English for a simpleton like me! For example, I know it takes the key, calculates the hash (I am looking for an explanation how) and then performs some kind of modulo to work out…
Arec Barrwin
  • 58,423
  • 9
  • 27
  • 25
451
votes
12 answers

HashSet vs. List performance

It's clear that a search performance of the generic HashSet class is higher than of the generic List class. Just compare the hash-based key with the linear approach in the List class. However calculating a hash key may itself take some CPU…
Michael Damatov
  • 13,992
  • 10
  • 43
  • 70
368
votes
7 answers

Calculate MD5 checksum for a file

I'm using iTextSharp to read the text from a PDF file. However, there are times I cannot extract text, because the PDF file is only containing images. I download the same PDF files everyday, and I want to see if the PDF has been modified. If the…
broke
  • 7,508
  • 14
  • 50
  • 81
367
votes
12 answers

node.js hash string?

I have a string that I want to hash. What's the easiest way to generate the hash in node.js? The hash is for versioning, not security.
Harry
  • 47,045
  • 66
  • 163
  • 243
359
votes
16 answers

Why should hash functions use a prime number modulus?

A long time ago, I bought a data structures book off the bargain table for $1.25. In it, the explanation for a hashing function said that it should ultimately mod by a prime number because of "the nature of math". What do you expect from a $1.25…
theschmitzer
  • 10,928
  • 11
  • 37
  • 48
343
votes
4 answers

What column type/length should I use for storing a Bcrypt hashed password in a Database?

I want to store a hashed password (using BCrypt) in a database. What would be a good type for this, and which would be the correct length? Are passwords hashed with BCrypt always of same length? EDIT Example…
helpermethod
  • 51,037
  • 60
  • 165
  • 263
327
votes
6 answers

C++ unordered_map using a custom class type as the key

I am trying to use a custom class as key for an unordered_map, like the following: #include #include #include using namespace std; class node; class Solution; class Node { public: int a; int b; …
Alfred Zhong
  • 5,853
  • 9
  • 39
  • 55
309
votes
16 answers

Is "double hashing" a password less secure than just hashing it once?

Is hashing a password twice before storage any more or less secure than just hashing it once? What I'm talking about is doing this: $hashed_password = hash(hash($plaintext_password)); instead of just this: $hashed_password =…
Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
309
votes
20 answers

Best implementation for hashCode method for a collection

How do we decide on the best implementation of hashCode() method for a collection (assuming that equals method has been overridden correctly) ?
Omnipotent
  • 25,231
  • 11
  • 28
  • 33
1
2 3
99 100