有效的解决方案:base32编码在php [英] effective solution: base32 encoding in php

查看:277
本文介绍了有效的解决方案:base32编码在php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个base32函数/类为php。我发现的不同的类和功能都是非常无效的。我运行了一个基准,并得出以下结果:



10000个解码:



base32:2.3273秒<我使用的base32类是:



http://www.php.net /manual/en/function.base-convert.php#102232



有什么办法更简单吗?



为什么我要使用base32的原因是它不区分大小写,因此我对于某些系统(例如电子邮件系统)上的url参数总是转换为小写字母没有任何问题



如果你有一个更好的小编编码方式,我也很乐意听到他们: - )。



Best,



Freddy

解决方案

对于Base32在PHP中,你可以在这里尝试我的实现:



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



从README文件中的Base32示例复制:

  // RFC 4648 base32 alphabet;不区分大小写
$ base32 = new Base2n(5,'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',FALSE,TRUE,TRUE);
$ encoded = $ base32-> encode('encode this');
// MVXGG33EMUQHI2DJOM ======

它不慢,它可能或可能不会比您基准的类更快,但它不会像内置的PHP函数(如code> base64_encode())那么快。如果这对你很重要,而且你并不关心Base32编码,那么你应该只使用十六进制。您可以使用本机PHP函数对十六进制进行编码,并且不区分大小写。

  $ encoded = bin2hex('encode this') ; // 656e636f64652074686973 
$ decoded = pack('H *',$ encoded); //编码这个

//或者,从PHP 5.4 ...
$ decoded = hex2bin($ encoded); //编码这个

十六进制的缺点是与Base32相比有更多的数据通货膨胀。十六进制将数据充满100%,而Base32将资料膨胀约60%。


I am looking for a base32 function/class for php. the different classes and function that i found are all very ineffective. I ran a benchmark and came to the following result:

10000 decodings:

base32: 2.3273 seconds

base64: 0.0062 seconds

The base32 class which I have used is:

http://www.php.net/manual/en/function.base-convert.php#102232

Is there any way which is more simpler?

The reason why I want to use base32 is that it is not case sensitive and as a result I have no problems any more regarding url parameters which on some system (e.g. email systems) are always converted to lowercase letters.

If you have a better alternative for lowercase encoding, I would also love to hear them :-).

Best,

Freddy

解决方案

For Base32 in PHP, you can try my implementation here:

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

Copied from the Base32 example in the README file:

// RFC 4648 base32 alphabet; case-insensitive
$base32 = new Base2n(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', FALSE, TRUE, TRUE);
$encoded = $base32->encode('encode this');
// MVXGG33EMUQHI2DJOM======

It's not slow, and it may or may not be faster than the class you benchmarked, but it will not be as fast as a built-in PHP function like base64_encode(). If that is very important to you, and you don't really care about Base32 encoding, then you should just use hexadecimal. You can encode hexadecimal with native PHP functions, and it is case-insensitive.

$encoded = bin2hex('encode this'); // 656e636f64652074686973
$decoded = pack('H*', $encoded);   // encode this

// Alternatively, as of PHP 5.4...
$decoded = hex2bin($encoded);      // encode this

The downside to hexadecimal is that there is more data inflation compared to Base32. Hexadecimal inflates the data 100%, while Base32 inflates the data about 60%.

这篇关于有效的解决方案:base32编码在php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆