简体   繁体   English

文件报告的SHA-1 base32返回字符串

[英]SHA-1 base32 of a file report back to a string

I'm having troubles figuring out how to implement a program that I can generate a base32 sha-1 value of a file. 我在弄清楚如何实现可以生成文件的base32 sha-1值的程序时遇到了麻烦。 I know it can't be too difficult to figure out as to generate a standard sha1 file is fairly easy. 我知道要弄清楚,生成标准的sha1文件非常容易,所以这不是很难。

$file1 = sha1_file('main.jpg');

Any help would be appreciated. 任何帮助,将不胜感激。

If you want to encode the SHA1 value as Base32, you gonna have to either write that yourself or find a library. 如果要将SHA1值编码为Base32,则必须自己编写或找到一个库。 PHP does not have it built-in, like it does with base64_encode . PHP没有内置的功能,就像base64_encode

A while ago, I needed a base32_encode function, so I wrote one. 前一阵子,我需要一个base32_encode函数,所以我写了一个。 I don't know how efficient it is, and I'm sure better ones exist out there, but it does work. 我不知道它的效率如何,我敢肯定还有更好的方法,但是它确实有效。 It's located here: https://github.com/NTICompass/PHP-Base32 它位于这里: https : //github.com/NTICompass/PHP-Base32

Using that you can do: 使用它,您可以执行以下操作:

<?php
include 'Base32.php';

$base32 = new Base32;

$file1 = sha1_file('main.jpg');
echo $base32->base32_encode($file1);

I have a PHP class, Base2n , which can encode Base32 per RFC 4648. It's actually more flexible than that, allowing you to parametrically define many standard and non-standard encoding schemes with a 2 n base. 我有一个PHP类Base2n ,它可以根据RFC 4648编码Base32。实际上,它比以前更加灵活,允许您以2 n为基数以参数方式定义许多标准和非标准编码方案。

https://github.com/ademarre/binary-to-text-php https://github.com/ademarre/binary-to-text-php

Base32 is demonstrated in the first example of the README file. 在README文件的第一个示例中演示了Base32。

Here's what your "main.jpg" example would look like: 您的“ main.jpg”示例如下所示:

// include, require, or autoload Base2n

$file1 = sha1_file('main.jpg', TRUE);

$base32 = new Base2n(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', FALSE, TRUE, TRUE);
$base32FileHash = $base32->encode($file1);

Notice that I also set $raw_output=TRUE in the call to sha1_file() . 注意,我在对sha1_file()的调用中还设置了$raw_output=TRUE I assume this is what you want because otherwise the Base32 output would be done on the hexadecimal representation of the SHA-1 hash digest, not the raw 160-bit digest itself. 我假设这就是您想要的,因为否则Base32输出将在SHA-1哈希摘要的十六进制表示形式上进行,而不是在原始的160位摘要本身上进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM