node.js hash string?

后端 未结 11 2376
暗喜
暗喜 2020-12-02 03:49

I have a string that I want to hash. What\'s the easiest way to generate the hash in node.js?

The hash is for versioning, not security.

11条回答
  •  情书的邮戳
    2020-12-02 04:33

    Take a look at crypto.createHash(algorithm)

    var filename = process.argv[2];
    var crypto = require('crypto');
    var fs = require('fs');
    
    var md5sum = crypto.createHash('md5');
    
    var s = fs.ReadStream(filename);
    s.on('data', function(d) {
      md5sum.update(d);
    });
    
    s.on('end', function() {
      var d = md5sum.digest('hex');
      console.log(d + '  ' + filename);
    });
    

提交回复
热议问题