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
145
votes
12 answers

How to change Hash values?

I'd like to replace each value in a hash with value.some_method. For example, for given a simple hash: {"a" => "b", "c" => "d"}` every value should be .upcased, so it looks like: {"a" => "B", "c" => "D"} I tried #collect and #map but always just…
Adam Nonymous
144
votes
5 answers

Choosing between std::map and std::unordered_map

Now that std has a real hash map in unordered_map, why (or when) would I still want to use the good old map over unordered_map on systems where it actually exists? Are there any obvious situations that I cannot immediately see?
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
143
votes
7 answers

Getting URL hash location, and using it in jQuery

I'd like to get the value after a hash in the URL of the current page and then be able to apply this in a new function... eg. The URL could be www.example.com/index.html#foo And I would like to use this in conjunction with the following piece of…
Robbie White
  • 1,558
  • 2
  • 11
  • 8
141
votes
5 answers

Is there any difference between the `:key => "value"` and `key: "value"` hash notations?

Is there any difference between :key => "value" (hashrocket) and key: "value" (Ruby 1.9) notations? If not, then I would like to use key: "value" notation. Is there a gem that helps me to convert from :x => to x: notations?
AdamNYC
  • 19,887
  • 29
  • 98
  • 154
141
votes
6 answers

How does Git compute file hashes?

The SHA1 hashes stored in the tree objects (as returned by git ls-tree) do not match the SHA1 hashes of the file content (as returned by sha1sum): $ git cat-file blob 4716ca912495c805b94a88ef6dc3fb4aff46bf3c |…
netvope
  • 7,647
  • 7
  • 32
  • 42
141
votes
5 answers

What is the optimal length for user password salt?

Any salt at all will obviously help when salting and hashing a user's password. Are there any best practices for how long the salt should be? I'll be storing the salt in my user table, so I would like the best tradeoff between storage size and…
David
  • 7,487
  • 6
  • 32
  • 25
141
votes
4 answers

How to convert ActiveRecord results into an array of hashes

I have an ActiveRecord result of a find operation: tasks_records = TaskStoreStatus.find( :all, :select => "task_id, store_name, store_region", :conditions => ["task_status = ? and store_id = ?", "f", store_id] ) Now I want to convert this…
Avinash Mb
  • 1,451
  • 2
  • 10
  • 5
140
votes
10 answers

Random hash in Python

What is the easiest way to generate a random hash (MD5) in Python?
mistero
  • 5,139
  • 8
  • 29
  • 27
140
votes
13 answers

Hash Code and Checksum - what's the difference?

My understanding is that a hash code and checksum are similar things - a numeric value, computed for a block of data, that is relatively unique. i.e. The probability of two blocks of data yielding the same numeric hash/checksum value is low enough…
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
139
votes
3 answers

How can I make a python dataclass hashable?

Say a I have a dataclass in python3. I want to be able to hash and order these objects. I only want them ordered/hashed on id. I see in the docs that I can just implement _hash_ and all that but I'd like to get datacalsses to do the work for me…
Brian C.
  • 6,455
  • 3
  • 32
  • 42
139
votes
2 answers

Is BCrypt a good hashing algorithm to use in C#? Where can I find it?

I have read that when hashing a password, many programmers recommend using the BCrypt algorithm. I am programming in C# and is wondering if anyone knows of a good implementation for BCrypt? I found this page, but I don't really know if it is bogus…
Svish
  • 152,914
  • 173
  • 462
  • 620
138
votes
4 answers

How do I sort an array of hashes by a value in the hash?

This Ruby code is not behaving as I would expect: # create an array of hashes sort_me = [] sort_me.push({"value"=>1, "name"=>"a"}) sort_me.push({"value"=>3, "name"=>"c"}) sort_me.push({"value"=>2, "name"=>"b"}) # sort sort_me.sort_by { |k|…
Ollie Glass
  • 19,455
  • 21
  • 76
  • 107
138
votes
14 answers

PHP best way to MD5 multi-dimensional array?

What is the best way to generate an MD5 (or any other hash) of a multi-dimensional array? I could easily write a loop which would traverse through each level of the array, concatenating each value into a string, and simply performing the MD5 on the…
Peter John
  • 1,859
  • 4
  • 15
  • 14
137
votes
8 answers

Git Tag list, display commit sha1 hashes

so the git tag command lists the current git tags tag1 tag2 git tag -n prints tag's message tag1 blah blah tag2 blah blah What's the best way to get the hash of tag1 & tag2 ?
Quang Van
  • 11,475
  • 12
  • 57
  • 61
137
votes
8 answers

Print Current Mercurial Revision Hash?

Is there a better way extract the current revision hash in Mercurial than hg log -l1|grep changeset|cut -d: -f3 ? Part of my webapp deployment script "tags" the uploaded app tarball with its unique revision hash.
rentzsch
  • 3,548
  • 4
  • 27
  • 39