nodejs中的文件夹哈希

Hash of folders in nodejs

在我的项目中,我想计算文件夹的哈希值。 比如有10个文件夹,这些文件夹下有很多子文件。我知道很多获取文件哈希值的方法,但是有什么方法可以获取每个文件夹的哈希值吗?

我这样做的目的是为了了解文件夹中的文件是否发生了变化。

我乐于接受建议和不同的想法,我需要你的帮助。提前致谢。

这实际上取决于您希望修改检测的可靠性。最可靠的方法是遍历每个文件夹中的每个文件,并通过读取每个文件的每个字节来计算实际文件内容的哈希值。

除此之外,您还可以检查文件元数据,例如文件名、修改日期、文件大小。其中任何一个的变化都表明内容发生了变化。但是,其中任何一个没有变化并不能最终表明文件内容没有改变。可以修改文件内容,保持相同的文件名,保持相同的文件大小并手动将修改日期设置回原来的日期 - 从而欺骗仅对元数据的检查。

但是,如果您愿意接受它可以通过操纵被愚弄,但通常会检测到更改,那么您可以迭代文件夹的所有文件并计算使用元数据的组合哈希:文件名、文件大小和文件修改日期,并为该文件夹提供一个散列。根据您的目的,这可能是充分的,也可能是不充分的 - 您必须进行调用。

除此之外,您将不得不读取每个文件的每个字节并计算实际文件内容的哈希值。

下面是一些元数据哈希算法的演示代码:

const fsp = require("fs/promises");
const { createHash } = require("crypto");
const path = require('path');

// -----------------------------------------------------
// Returns a buffer with a computed hash of all file's metadata:
//    full path, modification time and filesize
// If you pass inputHash, it must be a Hash object from the crypto library
//   and you must then call .digest() on it yourself when you're done
// If you don't pass inputHash, then one will be created automatically
//   and the digest will be returned to you in a Buffer object
// -----------------------------------------------------

async function computeMetaHash(folder, inputHash = null) {
    const hash = inputHash ? inputHash : createHash('sha256');
    const info = await fsp.readdir(folder, { withFileTypes: true });
    // construct a string from the modification date, the filename and the filesize
    for (let item of info) {
        const fullPath = path.join(folder, item.name);
        if (item.isFile()) {
            const statInfo = await fsp.stat(fullPath);
            // compute hash string name:size:mtime
            const fileInfo = `${fullPath}:${statInfo.size}:${statInfo.mtimeMs}`;
            hash.update(fileInfo);
        } else if (item.isDirectory()) {
            // recursively walk sub-folders
            await computeMetaHash(fullPath, hash);
        }
    }
    // if not being called recursively, get the digest and return it as the hash result
    if (!inputHash) {
        return hash.digest();
    }
}

computeMetaHash(__dirname).then(result => {
    console.log(result);
}).catch(err => {
    console.log(err);
});