Reading zip file content for later compute sha256 checksum fails.

huangapple go评论71阅读模式
英文:

Reading zip file content for later compute sha256 checksum fails

问题

我有一个包含一些常规文件的zip文件。这个文件已上传到文件服务器。
现在我正在尝试计算zip文件的sha256校验和,然后将校验和写入一个*.sha256sum文件,并将其上传到文件服务器。

然后,当有人从文件服务器下载zip文件和校验和文件(.sha256sum)时,他/她会再次计算zip文件的sha256并将其与存储为文本的校验和文件(.sha256sum)中刚下载的校验和进行比较。

当我尝试计算zip文件的sha256校验和时,出现错误。

with open(filename) as f:
    data = f.read()
    hash_sha256 = hashlib.sha256(data).hexdigest()

错误如下,并且在行data = f.read()中抛出:

in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap'编解码器无法解码位置44处的字节0x90字符映射到<undefined>
英文:

I have a zip file which contains some regular files. This file is uploaded to a fileserver.
Now I am trying to compute the sha256 checksum for the zip file, then write the checksum into a *.sha256sum file and upload to the fileserver as well.

Then when one downloads the zip file and the checksum file (.sha256sum) from the fileserver, he/she computes again the sha256 of the zip file and compare it with the one stored as text in the checksum file (.sha256sum) just downloaded.

When I try to compute the sha256 checksum of the zip file i get an error.

with open(filename) as f:
    data = f.read()
    hash_sha256 = hashlib.sha256(data).hexdigest()

The error is the following and it is thrown in line data = f.read():

in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: &#39;charmap&#39; codec can&#39;t decode byte 0x90 in position 44: character maps to &lt;undefined&gt;

答案1

得分: 2

你必须以二进制模式打开文件:

with open(filename, 'rb') as f:
    data = f.read()
    hash_sha256 = hashlib.sha256(data).hexdigest()

根据读写文件文档:

通常情况下,文件以文本模式打开,这意味着您从文件中读取和写入字符串,这些字符串是以特定编码编码的。

因此,在底层进行了一些操作,以使其成为可用的文本,而您并不希望这样。

在模式后附加 'b' 将文件以二进制模式打开。二进制模式数据以字节对象读取和写入。

英文:

You must open the file in binary mode:

with open(filename, &#39;rb&#39;) as f:
    data = f.read()
    hash_sha256 = hashlib.sha256(data).hexdigest()

Per Reading and Writing files:

> Normally, files are opened in text mode, that means, you read and write strings from and to the file, which are encoded in a specific encoding.

So, there's something going on under the hood to make it usable text, which you don't want.

> Appending a 'b' to the mode opens the file in binary mode. Binary mode data is read and written as bytes objects.

huangapple
  • 本文由 发表于 2023年5月17日 07:09:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定