目录中使用klaw递归函数的NodeJS哈希文件

时间:2019-06-11 10:12:05

标签: node.js hash

我正在尝试哈希目录中的每个文件,然后将输出打印到nodeJS上的.txt文件中。但是我面临的问题是哈希值打印在.txt文件中的错误位置。

要访问目录中的每个文件,我使用了Klaw API https://dustinpfister.github.io/2018/07/19/nodejs-klaw/?。我指的是NodeJS hash files recursively in a directory用于递归哈希每个文件。我怀疑问题是因为console.log尚未标准化,尤其是在调用函数console.log() async or sync??时。我在网络上找到了一些解决方案,例如setTimeout(但是我不知道如何在函数发生抛出异常的情况下应用它),如果能提供帮助,我将不胜感激!

这是我在NodeJS上的代码

let klaw = require('klaw'),
path = require('path'),
// the dir to walk
dir_walk = process.argv[2] || process.cwd();

var crypto = require('crypto');
var fs = require('fs');

// walking dir_walk with the following options
klaw(dir_walk, {

    // default to full recursion, if now depth is given
    depthLimit: process.argv[3] || -1

}).on('data', function (item) {

    if (!item.stats.isDirectory()) {
        //hash function
        generateHash(item.path, function (e, hash) {
       if (e) done(e);
        console.log('Hash : ' + hash);
      });
        console.log('\nType : File');
        console.log('Name : ' + path.basename(item.path));
        console.log('Path: ' + item.path); // the full absolute path of of the item
        console.log('Mtime: ' + item.stats.mtime); 
        console.log('Ctime: ' + item.stats.ctime);// the stats of the item 
    }
    else{
        console.log('\nType : Folder');
        console.log('Name : ' + path.basename(item.path));
        console.log('Path: ' + item.path);
        console.log('Mtime: ' + item.stats.mtime); 
        console.log('Ctime: ' + item.stats.ctime);
    }

})
//Function for generating hashes

 function generateHash (filename, callback) {
    var algorithm = 'sha256';
    var hashTable = new Array();

    var hash = crypto.createHash(algorithm);
    var fileStream = fs.ReadStream(filename);

    fileStream.on('data', function(data) {
        hash.update(data);      
    });
    fileStream.on('end', function() {
        var digest = hash.digest('hex');
        callback(null, digest);
    });
}

我的目录的输出是

Type : Folder
Name : TESTING
Path: /Users/*/Documents/TESTING
Mtime: Tue Jun 11 2019 16:09:29 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 16:09:29 GMT+0800 (Singapore Standard Time)


Type : File
Name : .DS_Store
Path: /Users/*/Documents/TESTING/.DS_Store
Mtime: Tue Jun 11 2019 15:44:03 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 15:44:03 GMT+0800 (Singapore Standard Time)


Type : File
Name : basic.js
Path: /Users/*/Documents/TESTING/basic.js
Mtime: Tue Jun 11 2019 16:09:08 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 16:09:08 GMT+0800 (Singapore Standard Time)


Type : File
Name : basicv1.js
Path: /Users/*/Documents/TESTING/basicv1.js
Mtime: Tue Jun 11 2019 17:37:02 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 17:37:02 GMT+0800 (Singapore Standard Time)
Hash : 1ebe514c6032f1bcb6c50a0e07fde487d8a38ca2a2a67948198bf48bc8877951
Hash : 56d7fadb2f8d37eda3986c73726fecb4469e84ac1fbe0b1108f2d141bb8a509f

Type : Folder
Name : folder
Path: /Users/*/Documents/TESTING/folder
Mtime: Sat Jun 08 2019 15:36:35 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 15:43:20 GMT+0800 (Singapore Standard Time)
Hash : 038789863d605a4f6219bbd0c087a32876e633ff4e678d1744d423e9abca1260

但是我应该期望的实际输出是

Type : Folder
Name : TESTING
Path: /Users/*/Documents/TESTING
Mtime: Tue Jun 11 2019 16:09:29 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 16:09:29 GMT+0800 (Singapore Standard Time)


Type : File
Name : .DS_Store
Path: /Users/*/Documents/TESTING/.DS_Store
Mtime: Tue Jun 11 2019 15:44:03 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 15:44:03 GMT+0800 (Singapore Standard Time)
Hash : 1ebe514c6032f1bcb6c50a0e07fde487d8a38ca2a2a67948198bf48bc8877951


Type : File
Name : basic.js
Path: /Users/*/Documents/TESTING/basic.js
Mtime: Tue Jun 11 2019 16:09:08 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 16:09:08 GMT+0800 (Singapore Standard Time)
Hash : 56d7fadb2f8d37eda3986c73726fecb4469e84ac1fbe0b1108f2d141bb8a509f

Type : File
Name : basicv1.js
Path: /Users/*/Documents/TESTING/basicv1.js
Mtime: Tue Jun 11 2019 17:37:02 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 17:37:02 GMT+0800 (Singapore Standard Time)
Hash : 038789863d605a4f6219bbd0c087a32876e633ff4e678d1744d423e9abca1260

Type : Folder
Name : folder
Path: /Users/*/Documents/TESTING/folder
Mtime: Sat Jun 08 2019 15:36:35 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 15:43:20 GMT+0800 (Singapore Standard Time)

1 个答案:

答案 0 :(得分:1)

generateHash是异步的。您必须等待结果,并在打印后像这样

// walking dir_walk with the following options
klaw(dir_walk, {

    // default to full recursion, if now depth is given
    depthLimit: process.argv[3] || -1

}).on('data', function (item) {

    if (!item.stats.isDirectory()) {
        //hash function
        generateHash(item.path, function (e, hash) {
            if (e) done(e);
            console.log('\nType : File');
            console.log('Name : ' + path.basename(item.path));
            console.log('Path: ' + item.path); // the full absolute path of of the item
            console.log('Mtime: ' + item.stats.mtime);
            console.log('Ctime: ' + item.stats.ctime); // the stats of the item 
            console.log('Hash : ' + hash);
        });
    } else {
        console.log('\nType : Folder');
        console.log('Name : ' + path.basename(item.path));
        console.log('Path: ' + item.path);
        console.log('Mtime: ' + item.stats.mtime);
        console.log('Ctime: ' + item.stats.ctime);
    }

})