首页 > 解决方案 > 如何从节点中的文件缓冲区创建十六进制

问题描述

使用 Typescript / Node / Crypto 创建哈希。

const fileBuffer = readFileSync(filePath);

const hashedFileName = crypto
      .createHash("md5")
      .update(fileBuffer)
      .digest("hex");

但是得到一个...

Argument of type 'ArrayBuffer' is not assignable to parameter of type 'BinaryLike'.
  Type 'ArrayBuffer' is missing the following properties from type 'Float64Array': BYTES_PER_ELEMENT, buffer, byteOffset, copyWithin, and 23 more.

有没有办法使这项工作?什么是 BinaryLike?

标签: node.jscryptography

解决方案


AnArrayBuffer不是有效参数,因为.update您必须将其转换为BufferorUint8Array

const hashedFileName = crypto
      .createHash("md5")
      .update(Uint8Array.from(fileBuffer))
      .digest("hex");

推荐阅读