php - 在 PHP 中为 Google Cloud Storage 对象计算 md5Hash 或 crc32
问题描述
我无法计算此处记录的 Google Cloud Storage 对象的文件哈希。
我试过crc32($filepath)
,hash_file('crc32b', $filepath)
和 md5 等价物,但没有什么能与他们想出的东西相匹配。我也尝试过对结果进行 base64 编码,但当时我只是在猜测。
如何根据此文件在 PHP 中获取以下哈希值?https://storage.googleapis.com/hashing-test/0067142-0.png
crc32c:wPjVCQ==
md5哈希:SNsMU2l0FHH+BE3Fg79Vew==
解决方案
我在谷歌库源代码中找到了答案。以下返回一个与对象信息中创建的字符串相同的字符串。
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'));
推荐阅读
- bash - 将具有特殊字符的变量字符串存储到bash中的数组中
- ionic-framework - 如何在 Ionic 4 上的本地存储中存储图像?(简单且最新)
- javascript - 如何在 index.js 中动态导出组件?
- mysql - 基于存在于另一个表中的条件选择值?
- java - 如何通过 JavaFX 从 Mariadb 数据库中获取数据到表中?
- c++ - 调用加入时如何修复“终止调用而没有活动异常”
- tomcat9 - 找不到位于 WEB-INF/lib jar 中 META-INF/resources 目录内的资源
- excel - 如何将两个私有子 Worksheet_Change(ByVal Target As Range) 合并到一个工作表中?
- selenium - 如何使用 mvn robotframework 运行单个测试用例?
- reactjs - React Navigation 支持 useEffect Hook?错误回调不是函数