如何构建wheel包的RECORD文件中的哈希?

问题描述 投票:0回答:1

我需要知道如何生成.whl包文件中的RECORD文件。特别是它如何为每个文件生成sha256 ...

例如,我们可以看到这一行:

$ cat RECORD | grep WHEEL
pkv-X.Y.Z.dist-info/WHEEL,sha256=X8kVdBCq85ICewwfaE6btv5qKsFQfVq8NYJIXUK0i1A,104

这似乎来自:

$ sha256sum <WHEEL | awk '{print $1}' | xxd -r -p | base64 | tr +/ -_ | cut -c -43
X8kVdBCq85ICewwfaE6btv5qKsFQfVq8NYJIXUK0i1A
$ wc -c <WHEEL
104

但我想知道它是如何在python中构建的,因为我对trcut -c -43变换有一点信任。 注意:在其他文件上“看起来”tr是正确的,即/ - > _+ - > -但是我想让python源代码对此负责......

在python 3.7中,我到目前为止

python3 -c "import hashlib; import base64; print(base64.b64encode(hashlib.sha256(open('WHEEL', 'rb').read()).digest()))"
b'X8kVdBCq85ICewwfaE6btv5qKsFQfVq8NYJIXUK0i1A='

注意:到目前为止,我已经看到https://github.com/pypa/setuptools内部没有任何运气...

python python-3.x base64 python-wheel
1个回答
0
投票

你应该看看https://github.com/pypa/pip/blob/c9df690f3b5bb285a855953272e6fe24f69aa08a/src/pip/_internal/wheel.py#L71-L84

def rehash(path, blocksize=1 << 20):
    # type: (str, int) -> Tuple[str, str]
    """Return (hash, length) for path using hashlib.sha256()"""
    h = hashlib.sha256()
    length = 0
    with open(path, 'rb') as f:
        for block in read_chunks(f, size=blocksize):
            length += len(block)
            h.update(block)
    digest = 'sha256=' + urlsafe_b64encode(
        h.digest()
    ).decode('latin1').rstrip('=')
    # unicode/str python2 issues
    return (digest, str(length))  # type: ignore

可以使用bash命令在manylinux2010映像中完成:

/opt/_internal/cpython-3.7.3/bin/python3 -c "\
import hashlib;\
import base64;\
print(\
 base64.urlsafe_b64encode(\
 hashlib.sha256(open('FILE_NAME', 'rb').read()).digest())\
 .decode('latin1')\
 .rstrip(b'=')\
 )"
© www.soinside.com 2019 - 2024. All rights reserved.