Compare Two Files By Hash
NodeJS
Hard
4 views
Problem Description
Compare two files by sha256 and print SAME or DIFF.
Input Format
stdin: pathA pathB.
Output Format
Print one word.
Constraints
Use crypto.createHash.
Official Solution
const fs = require('fs');
const crypto = require('crypto');
const parts = fs.readFileSync(0, 'utf8').trim().split(/\\s+/);
if (parts.length < 2) process.exit(0);
function hashFile(p) {
const h = crypto.createHash('sha256');
h.update(fs.readFileSync(p));
return h.digest('hex');
}
try {
const a = hashFile(parts[0]);
const b = hashFile(parts[1]);
console.log(a === b ? 'SAME' : 'DIFF');
} catch (e) {
console.log('DIFF');
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!