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
182
votes
9 answers

Hashing a file in Python

I want python to read to the EOF so I can get an appropriate hash, whether it is sha1 or md5. Please help. Here is what I have so far: import hashlib inputFile = raw_input("Enter the name of the file:") openedFile = open(inputFile) readFile =…
user3358300
  • 1,919
  • 2
  • 13
  • 6
181
votes
4 answers

How to hash a string into 8 digits?

Is there anyway that I can hash a random string into a 8 digit number without implementing any algorithms myself?
Bob Fang
  • 6,963
  • 10
  • 39
  • 72
180
votes
5 answers

Best Practices: Salting & peppering passwords?

I came across a discussion in which I learned that what I'd been doing wasn't in fact salting passwords but peppering them, and I've since begun doing both with a function like: hash_function($salt.hash_function($pepper.$password)) [multiple…
Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
179
votes
11 answers

What type of hash does WordPress use?

What type of hash does WordPress use? Here is an example of a WordPress hash: $P$Bp.ZDNMM98mGNxCtHSkc1DqdRPXeoR.
Amanda Kumar
176
votes
13 answers

Is it worth hashing passwords on the client side

When I want to put a login system in place, I always compare the MD5 of the given password with its value in the users table on the server side. However, a friend of mine told me that a "clear" password could be sniffed by a network software. So my…
Zakaria
  • 14,892
  • 22
  • 84
  • 125
175
votes
20 answers

Is there hash code function accepting any object type?

Basically, I'm trying to create an object of unique objects, a set. I had the brilliant idea of just using a JavaScript object with objects for the property names. Such as, set[obj] = true; This works, up to a point. It works great with string and…
Boog
  • 3,744
  • 4
  • 28
  • 25
175
votes
9 answers

Hashing a string with SHA256

I try to hash a string using SHA256, I'm using the following code: using System; using System.Security.Cryptography; using System.Text; public class Hash { public static string getHashSha256(string text) { byte[] bytes =…
Nattfrosten
  • 1,999
  • 4
  • 16
  • 21
174
votes
9 answers

What is an elegant way in Ruby to tell if a variable is a Hash or an Array?

To check what @some_var is, I am doing a if @some_var.class.to_s == 'Hash' I am sure that there is a more elegant way to check if @some_var is a Hash or an Array.
drhyde
  • 1,851
  • 2
  • 11
  • 6
172
votes
11 answers

hash function for string

I'm working on hash table in C language and I'm testing hash function for string. The first function I've tried is to add ascii code and use modulo (% 100) but i've got poor results with the first test of data: 40 collisions for 130 words. The final…
lilawood
  • 2,263
  • 5
  • 22
  • 27
168
votes
8 answers

How many random elements before MD5 produces collisions?

I've got an image library on Amazon S3. For each image, I md5 the source URL on my server plus a timestamp to get a unique filename. Since S3 can't have subdirectories, I need to store all of these images in a single flat folder. Do I need to worry…
Ben Throop
  • 4,772
  • 5
  • 23
  • 20
168
votes
10 answers

Are there any SHA-256 javascript implementations that are generally considered trustworthy?

I am writing a login for a forum, and need to hash the password client side in javascript before sending it on to the server. I'm having trouble figuring out which SHA-256 implementation I can actually trust. I was expecting there to be some kind of…
jono
  • 1,782
  • 2
  • 13
  • 6
167
votes
2 answers

Why is '397' used for ReSharper GetHashCode override?

Like many of you, I use ReSharper to speed up the development process. When you use it to override the equality members of a class, the code-gen it produces for GetHashCode() looks like: public override int GetHashCode() { unchecked …
programmer
  • 4,342
  • 4
  • 24
  • 21
166
votes
8 answers

Salting Your Password: Best Practices?

I've always been curious... Which is better when salting a password for hashing: prefix, or postfix? Why? Or does it matter, so long as you salt? To explain: We all (hopefully) know by now that we should salt a password before we hash it for storage…
Randolpho
  • 55,384
  • 17
  • 145
  • 179
164
votes
17 answers

Is there an IDictionary implementation that, on missing key, returns the default value instead of throwing?

The indexer into Dictionary throws an exception if the key is missing. Is there an implementation of IDictionary that instead will return default(T)? I know about the TryGetValue() method, but that's impossible to use with LINQ. Would this…
TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
164
votes
4 answers

How do I create a SHA1 hash in ruby?

SHA Hash functions
quackingduck
  • 5,845
  • 5
  • 29
  • 22