读取并计算指定文件的MD5哈希值
/*** 读取并计算指定文件的MD5哈希值* @param {string} file - 文件路径* @returns {void} 不返回任何值,但会打印出文件的MD5哈希值*/
const fs = require('fs'); // 引入文件系统模块
const crypto = require('crypto'); // 引入加密模块
const path = require('path'); // 引入路径处理模块// 解析当前目录下的文件路径
const file = path.resolve(__dirname, './20220461.tif') ;// 异步读取文件内容
fs.readFile(file, (err, data) => {if (err) throw err; // 如果读取文件时发生错误,抛出异常// 创建一个MD5哈希对象,并使用文件数据更新哈希值const hash = crypto.createHash('md5');hash.update(data);// 计算并获取哈希值的十六进制表示const md5 = hash.digest('hex'); // 打印文件的MD5哈希值console.log(`The MD5 of ${file} is: ${md5}`);
});