Will identical files in different locations have different sha1 hashes?

1.3k views Asked by At
var crypto = require('crypto');
var fs = require('fs');
var file1 = process.argv[2];
var file2 = process.argv[3];

var sha1sum = function(input){
    return crypto.createHash('sha1').update(JSON.stringify(input)).digest('hex')
};

var first = sha1sum(file1);
var second = sha1sum(file2);

console.log(first + '  ' + file1);
console.log(second + '  ' + file2);
if (first == second) {
    console.log("the two hashes are equal");
} else {
    console.log("the two hashes aren't equal");
}

The above is the current code I'm using. It takes in two file inputs and compares their hashes. However, when passing the same file from two different locations as arguments, they have different sha1 hashes. Is this supposed to happen, or is my code incorrect?

1

There are 1 answers

0
AudioBubble On BEST ANSWER

So, it comes down to what you're hashing.

As was pointed out, I was only hashing the name. If you're only hashing the file contents, the sha1 values will be the same. However, if you combine the address value and the sha1 value, you will have distinct sha1s when comparing the same file in two different locations. Whether or not the sha1 value is location agnostic depends entirely on what you're passing into the sha1 value.