把檔案利用 sha256 來計算 hash 值 利用 js 或 node.js

2017-10-12 00:08:10 +08:00
 ricky321u

想不到有什麼相關的 library 可以做到,如果是 CryptoJS 可以做到的話也請告知一下 因為我目前只會把 string 換成 hash 希望可以做到新增一個 pdf 或其他檔案 會把其轉成 hASH 值 http://www.atool.org/file_hash.php 感謝各位

1789 次点击
所在节点    JavaScript
2 条回复
cctrv
2017-10-16 03:56:28 +08:00
node 走 shell 去獲得 sha256

Google 兩套關鍵字「 node shell command 」和「 sha256 terminal 」

https://stackoverflow.com/questions/3358420/generating-a-sha256-from-the-linux-command-line

https://www.npmjs.com/package/shelljs
cctrv
2017-10-16 04:00:41 +08:00
或者用這個

https://www.hacksparrow.com/how-to-generate-md5-sha1-sha512-sha256-checksum-hashes-in-node-js.html

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

// change the algo to sha1, sha256 etc according to your requirements
var algo = 'md5';
var shasum = crypto.createHash(algo);

var file = './kitten.jpg';
var s = fs.ReadStream(file);
s.on('data', function(d) { shasum.update(d); });
s.on('end', function() {
var d = shasum.digest('hex');
console.log(d);
});

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/396939

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX