admin管理员组

文章数量:815041

如何从节点中的文件缓冲区创建十六进制

使用打字稿/节点/加密来创建哈希。

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?

回答如下:

ArrayBuffer不是.update的有效参数,您必须将其转换为BufferUint8Array

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

本文标签: 如何从节点中的文件缓冲区创建十六进制