为文件(或文件集)生成 base64 编码的 SHA512 摘要

发布于 2024-10-19 02:48:53 字数 984 浏览 4 评论 0原文

适用于 Tablet OS 的 BlackBerry WebWorks SDK 构建工具会生成一个存档,其中包含文件和一个 MANIFEST.MF,其中包含每个文件的条目和一个 Base64 编码的 SHA512 摘要。但是有一个bug,构建中文件过多会导致构建失败。我正在尝试弄清楚如何自己生成相同的信息。

下面的示例来自 MANIFEST.MF,它包含文件名和摘要。但是,base64 变体使用 _ 代替 /,使用 - 代替 +,并且没有尾随 == 用于填充。

Archive-Asset-Name: air/XX/IMG_0999.jpg
Archive-Asset-SHA-512-Digest: YI_KXWjpJwwsi5MDQPeBQc9SVi-bH6zYq5PgBD3jQiqHu-r-5Hv8A0yh_y5j2T9MpYZ5TVMW4JXHSXNYmpV1tA

我运行 Windows 7,但安装了 GIT,因此我将 MINGW32 作为 bash shell。我发现了 openssl 和 tr 命令的管道组合,但它似乎有点混乱。

openssl dgst -sha512 -binary IMG_0999.gif | openssl enc -base64 | tr '+' '-' | tr '/' '_' | tr -d '=' | tr -d '\n'

有没有更好的方法来生成这个摘要?

最终,我将不得不对目录树中的所有图像进行处理。我希望有一个脚本解决方案,而不必编写程序。第二个问题是如何为目录中的所有文件生成清单语法(递归地)。这是包含上述命令的 bash shell 脚本吗?由于我以前没有编写过 shell 脚本,所以这里的任何指针都会受到赞赏。

The BlackBerry WebWorks SDK for Tablet OS build tool generates an archive that contains the files and a MANIFEST.MF with entry for each file and a base64 encoded SHA512 digest. However, there is a bug that too many files in the build will cause a build failure. I am trying to figure out how to generate that same information myself.

The example below comes from the MANIFEST.MF and it contains the filename and digest. However, the base64 variant uses _ instead of / and - instead of + and has no trailing == for padding.

Archive-Asset-Name: air/XX/IMG_0999.jpg
Archive-Asset-SHA-512-Digest: YI_KXWjpJwwsi5MDQPeBQc9SVi-bH6zYq5PgBD3jQiqHu-r-5Hv8A0yh_y5j2T9MpYZ5TVMW4JXHSXNYmpV1tA

I running Windows 7, but have GIT installed so I have MINGW32 as a bash shell. I found this piped combination of openssl and tr commands but it seems a bit of a kludge.

openssl dgst -sha512 -binary IMG_0999.gif | openssl enc -base64 | tr '+' '-' | tr '/' '_' | tr -d '=' | tr -d '\n'

Is there a better way to generate this digest?

Eventually, I will have to do for all the images in a directory tree. I was hoping for a script solution instead of having to write a program. A second question is how to generate that manifest syntax for all files in a directory (recursively). Would this be a bash shell script encompassing the above command? Any pointers here are appreciated since I haven't written a shell script before.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您的好友蓝忘机已上羡 2024-10-26 02:48:53

您可以通过组合一些内容来缩短命令:

openssl dgst -sha512 -binary IMG_0999.gif | openssl enc -base64 | tr '+/' '-_' | tr -d '=\n'

对于树中的所有文件,例如:

find . -type f -name '*.gif' -print0 | xargs -0 -I % sh -c 'openssl dgst -sha512 -binary "$(basename "$1")" | openssl enc -base64 | tr "+/" "-_" | tr -d "=\n" > "$1.sum"' _ $1

You can shorten your command by combining some things:

openssl dgst -sha512 -binary IMG_0999.gif | openssl enc -base64 | tr '+/' '-_' | tr -d '=\n'

For all files in a tree, something like:

find . -type f -name '*.gif' -print0 | xargs -0 -I % sh -c 'openssl dgst -sha512 -binary "$(basename "$1")" | openssl enc -base64 | tr "+/" "-_" | tr -d "=\n" > "$1.sum"' _ $1
半世晨晓 2024-10-26 02:48:53

Dennis Williamson 的解决方案让我继续前进,但是安装的 tr 版本 (2.0) 确实喜欢多字符语法。此外,xargs 版本 (4.1) 不喜欢 -I 选项。然而,它让我开始了,这才是重要的。这就是我最终得到的结果:

find . -type f -name '*.gif' -print0 | xargs -0 --replace=% sh -c 'echo -e "Archive-Asset-Name: %\nArchive-Asset-SHA-512-Digest: `openssl dgst -sha512 -binary % | openssl enc -base64 | tr "+" "-" | tr "/" "_" | tr -d "=" | tr -d "\n"`\n"'

并生成以下内容(更改图像名称以保护无辜者)

Archive-Asset-Name: ./IMG_0001.gif
Archive-Asset-SHA-512-Digest: AsgQnXYxj07qy-DTlzqYEv3v1UNqbr1sp5kVz0PE8FYwD-q_0VM20Wyci3xunJwdqy59MbyIX1GGtSmhuETUHQ

Archive-Asset-Name: ./IMG_0032.gif
Archive-Asset-SHA-512-Digest: JE3lmvXxOjSpDoUjyvAp92E1T4b8AQGGJBCcBz_1AG2eoBoSRH76LHpWPEUAeXFx_kOqEaDMRGPrpYnlfcnz9g

Archive-Asset-Name: ./IMG_0003.gif
Archive-Asset-SHA-512-Digest: bJJQ-j-s9630FzUh3hP50h7gWYqJQ6o0FP37nUuSeSUmerFxBwR2X0VeTYn-h4cf7szUr4fqDfAKQLOK0gt7zA

我仍然需要调整名称,使其不带有 ./ 前缀,但那是另一天的事了。

Dennis Williamson's solution got me going but the tr version (2.0) that is installed did like that multiple character syntax. Also, the xargs version (4.1) didn't like the -I option. However, it got me started and that is what matters. Here is what I ended up with:

find . -type f -name '*.gif' -print0 | xargs -0 --replace=% sh -c 'echo -e "Archive-Asset-Name: %\nArchive-Asset-SHA-512-Digest: `openssl dgst -sha512 -binary % | openssl enc -base64 | tr "+" "-" | tr "/" "_" | tr -d "=" | tr -d "\n"`\n"'

and that generates the following (images names changed to protect the innocent)

Archive-Asset-Name: ./IMG_0001.gif
Archive-Asset-SHA-512-Digest: AsgQnXYxj07qy-DTlzqYEv3v1UNqbr1sp5kVz0PE8FYwD-q_0VM20Wyci3xunJwdqy59MbyIX1GGtSmhuETUHQ

Archive-Asset-Name: ./IMG_0032.gif
Archive-Asset-SHA-512-Digest: JE3lmvXxOjSpDoUjyvAp92E1T4b8AQGGJBCcBz_1AG2eoBoSRH76LHpWPEUAeXFx_kOqEaDMRGPrpYnlfcnz9g

Archive-Asset-Name: ./IMG_0003.gif
Archive-Asset-SHA-512-Digest: bJJQ-j-s9630FzUh3hP50h7gWYqJQ6o0FP37nUuSeSUmerFxBwR2X0VeTYn-h4cf7szUr4fqDfAKQLOK0gt7zA

I still need to tweak the name to not be prefixed with ./ but that is for another day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文