tags:

views:

55

answers:

4

Let's say there is a file called myfile.txt with the following contents:

one two three

Another file called yourfile.txt with following contents:

two three one

Will the SHA-1 hash be the same for both of these files, because the content are same but in different order?

+3  A: 

No, it'll be different. Most good general purpose hashing algorithms take order into account. About the only commonly used hash functions that don't are simple checksums.

Laurence Gonsalves
+2  A: 

No, it will be different. Hashes typically work iteratively over a series of bytes.

Of course, you could just try it ;)

WhirlWind
Yeah, I could try it, but I am on a customer's machine now and a sudden doubt popped up in parallel :)
Nirmal
+1  A: 

Depends on the hash algorithm. I can create one right now that returns two equal hashes for both files.

But since you specifically asked about SHA-1 then yes, they will be completely different.

    SHA1("one two three") = a10600b129253b1aaaa860778bef2043ee40c715
    SHA1("two three one") = 5b836799b259835e762c93964a68b958eb19461a
Andreas Bonini
For a "secure" hash, it would have to be different, or it wouldn't be too secure. For any old hash, you could create whatever you wanted.
WhirlWind
If I create one now by definition it can't be "old", can it? :) Anyways yes of course, any serious hash algorithm will produce different results.
Andreas Bonini
My main reason for asking this is to prevent users from uploading the same file twice at any point. But suddenly wondered what would happen if the contents just swap. Now with the results, my Sunday is saved!
Nirmal
+1  A: 

'Cryptographic' hashes are designed to detect changes like transpositions, otherwise someone could take an electronic payment message and change the $1900.00 amount to $9100.00 without detection (by the hash anyway), which would kind of defeat the purpose.

Michael Burr