Nodejs 中文件夹的哈希值

rid*_*der 3 javascript hash hashcode node.js

在我的项目中,我想计算文件夹的哈希值。例如有10个文件夹,这些文件夹有很多子文件。我知道很多方法来获取文件的哈希值,但是有没有办法获取每个文件夹的哈希值?

我这样做的目的是了解文件夹中的文件是否已更改。

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

jfr*_*d00 7

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

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

但是,如果您愿意接受它可以通过操作来欺骗,但通常会检测更改,那么您可以迭代文件夹的所有文件并计算使用元数据的组合哈希:文件名、文件大小和文件修改日期并为该文件夹提供一个哈希值。根据您的目的,这可能是充分的,也可能是不充分的——您必须拨打该电话。

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

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

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);
});
Run Code Online (Sandbox Code Playgroud)