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
228
votes
6 answers

How to get a specific output iterating a hash in Ruby?

I want to get a specific output iterating a Ruby Hash. This is the Hash I want to iterate over: hash = { 1 => ['a', 'b'], 2 => ['c'], 3 => ['d', 'e', 'f', 'g'], 4 => ['h'] } This is the output I would like to…
sts
  • 2,317
  • 3
  • 16
  • 8
226
votes
9 answers

Hash collision in git

What would actually happen if I had a hash collision while using git? E.g. I manage to commit two files with the same sha1 checksum, would git notice it or corrupt one of the files? Could git be improved to live with that, or would I have to change…
Sec
  • 7,059
  • 6
  • 31
  • 58
215
votes
7 answers

How to add new item to hash

I don't know how to add new item to already existing hash. For example, first I construct hash: hash = {item1: 1} After that, I want to add item2, so after this I have hash like this: {item1: 1, item2: 2} I don't know what method to do on hash.…
Иван Бишевац
  • 13,811
  • 21
  • 66
  • 93
205
votes
17 answers

Hash and salt passwords in C#

I was just going through one of DavidHayden's articles on Hashing User Passwords. Really I can't get what he is trying to achieve. Here is his code: private static string CreateSalt(int size) { //Generate a cryptographic random number. …
ACP
  • 34,682
  • 100
  • 231
  • 371
203
votes
3 answers

hash function in Python 3.3 returns different results between sessions

I've implemented a BloomFilter in python 3.3, and got different results every session. Drilling down this weird behavior got me to the internal hash() function - it returns different hash values for the same string every session. Example: >>>…
redlus
  • 2,301
  • 2
  • 12
  • 16
202
votes
11 answers

Changing every value in a hash in Ruby

I want to change every value in a hash so as to add '%' before and after the value so { :a=>'a' , :b=>'b' } must be changed to { :a=>'%a%' , :b=>'%b%' } What's the best way to do this?
theReverseFlick
  • 5,894
  • 8
  • 32
  • 33
194
votes
5 answers

Simple (non-secure) hash function for JavaScript?

Possible Duplicate: Generate a Hash from string in Javascript/jQuery Can anyone suggest a simple (i.e. tens of lines of code, not hundreds of lines) hash function written in (browser-compatible) JavaScript? Ideally I'd like something that, when…
mjs
  • 63,493
  • 27
  • 91
  • 122
193
votes
9 answers

How can I update window.location.hash without jumping the document?

I have a sliding panel set up on my website. When it finished animating, I set the hash like so function() { window.location.hash = id; } (this is a callback, and the id is assigned earlier). This works good, to allow the user to bookmark the…
alex
  • 479,566
  • 201
  • 878
  • 984
192
votes
14 answers

Good Hash Function for Strings

I'm trying to think up a good hash function for strings. And I was thinking it might be a good idea to sum up the unicode values for the first five characters in the string (assuming it has five, otherwise stop where it ends). Would that be a good…
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
191
votes
7 answers

Default implementation for Object.GetHashCode()

How does the default implementation for GetHashCode() work? And does it handle structures, classes, arrays, etc. efficiently and well enough? I am trying to decide in what cases I should pack my own and in what cases I can safely rely on the default…
Fung
  • 7,530
  • 7
  • 53
  • 68
190
votes
9 answers

Why is XOR the default way to combine hashes?

Say you have two hashes H(A) and H(B) and you want to combine them. I've read that a good way to combine two hashes is to XOR them, e.g. XOR( H(A), H(B) ). The best explanation I've found is touched briefly here on these hash function…
Nate Murray
  • 3,841
  • 5
  • 32
  • 33
189
votes
11 answers

How can I print the contents of a hash in Perl?

I keep printing my hash as # of buckets / # allocated. How do I print the contents of my hash? Without using a while loop would be most preferable (for example, a one-liner would be best).
Kys
  • 2,597
  • 5
  • 20
  • 16
183
votes
12 answers

Equivalent of .try() for a hash to avoid "undefined method" errors on nil?

In Rails we can do the following in case a value doesn't exist to avoid an error: @myvar = @comment.try(:body) What is the equivalent when I'm digging deep into a hash and don't want to get an error? @myvar =…
sscirrus
  • 55,407
  • 41
  • 135
  • 228
183
votes
4 answers

How to sort a Ruby Hash by number value?

I have a counter hash that I am trying to sort by count. The problem I am running into is that the default Hash.sort function sorts numbers like strings rather than by number size. i.e. Given Hash: metrics = {"sitea.com" => 745, "siteb.com" => 9,…
Dustin M.
  • 2,884
  • 3
  • 20
  • 18
183
votes
13 answers

How to "EXPIRE" the "HSET" child key in redis?

I need to expire all keys in redis hash, which are older than 1 month.
aieven
  • 1,939
  • 3
  • 14
  • 8