我在计算谷歌云存储对象的文件哈希时遇到了问题,因为文档中记录了here。
我已经尝试过crc32($filepath)、hash_file('crc32b', $filepath)和md5等效项,但都不能与它们的功能相匹配。我也尝试过对结果进行base64编码,但我当时只是猜测而已。
如何在PHP中根据这个文件获得下面的哈希?https://storage.googleapis.com/hashing-test/0067142-0.png
crc32c:wPjVCQ==
md5Hash:SNsMU2l0FHH+BE3Fg79Vew==
发布于 2020-07-28 06:00:41
我在谷歌图书馆的源代码中找到了答案。下面返回的字符串与在对象信息中创建的字符串相同。
use Google\CRC32\CRC32;
function getCrc32($content) {
$crc32c = CRC32::create(CRC32::CASTAGNOLI);
$crc32c->update($content);
return base64_encode($crc32c->hash(true));
}
$hash = getCrc32(file_get_contents('/path/to/foo.png'));发布于 2020-07-28 06:58:44
这与使用crcmod的python相同:
import crcmod
with open("smallfile.txt", "rb") as f:
tmp = b''.join(f.readlines())
crc32_func = crcmod.mkCrcFun(0x11EDC6F41, initCrc=0, xorOut=0xFFFFFFFF)
crc32c = hex(crc32_func(tmp))
print (crc32c)https://stackoverflow.com/questions/63117031
复制相似问题