createMD5.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import crypto from "crypto";
  2. import fs from "node:fs";
  3. import SparkMD5 from "spark-md5";
  4. import environment from "#environment";
  5. export function createMD5(filePath) {
  6. return new Promise((res, rej) => {
  7. const hash = crypto.createHash("md5");
  8. const rStream = fs.createReadStream(filePath);
  9. rStream.on("data", (data) => {
  10. hash.update(data);
  11. });
  12. rStream.on("end", () => {
  13. res(hash.digest("hex"));
  14. });
  15. });
  16. }
  17. /**
  18. * 作者:起一个可以中奖的名字
  19. * 链接:https://juejin.cn/post/7142831050052665381
  20. * 来源:稀土掘金
  21. * 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  22. * 计算文件Md5
  23. * 将文件分片逐步计算最终合并得出整个文件md5, 提升计算速度
  24. * @param {*} file
  25. */
  26. // export function computeFileMD5(file) {
  27. // return new Promise((resolve, reject) => {
  28. // let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
  29. // let chunkSize = 2097152; // 按照一片 2MB 分片
  30. // let chunks = Math.ceil(file.size / chunkSize); // 片数
  31. // let currentChunk = 0;
  32. // let spark = new SparkMD5.ArrayBuffer();
  33. // let fileReader = new FileReader();
  34. // fileReader.onload = function (e) {
  35. // console.log('read chunk nr', currentChunk + 1, 'of', chunks);
  36. // spark.append(e.target.result);
  37. // currentChunk++;
  38. // if (currentChunk < chunks) {
  39. // loadNext();
  40. // } else {
  41. // console.log('finished loading');
  42. // let md5 = spark.end(); //最终md5值
  43. // spark.destroy(); //释放缓存
  44. // resolve(md5);
  45. // }
  46. // };
  47. // fileReader.onerror = function (e) {
  48. // console.warn('oops, something went wrong.');
  49. // reject(e);
  50. // };
  51. // function loadNext() {
  52. // let start = currentChunk * chunkSize;
  53. // const end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
  54. // fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
  55. // }
  56. // loadNext();
  57. // })
  58. // }
  59. //
  60. export function computeFileMD5(file) {
  61. return new Promise((resolve, reject) => {
  62. const hash = crypto.createHash("md5");
  63. // 直接将整个 Buffer 更新到 hash
  64. hash.update(file.data);
  65. // 计算哈希值
  66. const md5 = hash.digest("hex");
  67. resolve(md5);
  68. });
  69. }
  70. // 加密
  71. export function aes_encrypt(msg) {
  72. const aesInfo = environment.aes_info();
  73. const cipher = crypto.createCipheriv("aes-256-cbc", aesInfo.key, aesInfo.iv);
  74. // input encoding: utf8
  75. // output encoding: hex
  76. let encrypted = cipher.update(msg, "utf8", "hex");
  77. encrypted += cipher.final("hex");
  78. return encrypted;
  79. }
  80. // 解谜
  81. export function aes_decrypt(encrypted) {
  82. const aesInfo = environment.aes_info();
  83. const decipher = crypto.createDecipheriv(
  84. "aes-256-cbc",
  85. aesInfo.key,
  86. aesInfo.iv,
  87. );
  88. let decrypted = decipher.update(encrypted, "hex", "utf8");
  89. decrypted += decipher.final("utf8");
  90. return decrypted;
  91. }