尝试将Perl转换为PHP [英] Trying to convert Perl to PHP

查看:94
本文介绍了尝试将Perl转换为PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将以下代码从Perl转换为PHP.特别是,我在本节中遇到了麻烦,因为我不知道这里发生了什么:

I'm having trouble converting the following code from Perl to PHP. In particular, I'm having trouble with this section, as I have no clue what is happening here:

sub md5sum($) {
    my @ib = unpack("C*", shift);
    my $encstr = join("", map {sprintf('\%3.3o', $_)} @ib);
    my ($out) = split(/ /, `printf '$encstr' | md5sum`);
    return $out;
}

我正在寻求帮助的要点是"C*", shift在unpack语句中的作用,join部分中的情况以及| md5sum在<末尾的作用c3>

The main point I'm looking for help on is what does the "C*", shift do in the unpack statement, what's going on in the join part, and what does the | md5sum do at the end of the split

对于下面的这一部分,我相信我也遇到了麻烦.我的主要问题是使用php包,我指定H32还是仅指定H?

For this section below, I believe I'm also having trouble. My main question here is for the php pack, do I specify the H32, or just the H?

$hexchal = pack "H32", $challenge;
$newchal = pack "H32", md5sum($hexchal . $uamsecret);
my $passvar = "";
$passvar = md5sum("\0" . $password . $newchal);

这是我在上一节中尝试过的方法,但是$ hexchal的值与之不符

Here is what I've tried for the section above, but the $hexchal's don't match up

$hexchal = pack("H32", $challenge);
$newchal = md5($hexchal.$uamsecret);
$passvar = md5("\0".$password.$newchal);

推荐答案

代码对我来说有些混乱,但这是我的解释:

Code looks a little confusing to me but here's my interpretation:

<1> sub md5sum($) {
<2>   my @ib = unpack("C*", shift);
<3>   my $encstr = join("", map {sprintf('\%3.3o', $_)} @ib);
<4>   my ($out) = split(/ /, `printf '$encstr' | md5sum`);
<5>   return $out;
    }

< 2>上的"C*", shift将字符串转换为数字数组,然后通过对每个数字进行sprintf格式化将它们连接在一起以形成新的字符串.然后,在执行命令行printf '$encstring' | md5sum时,将所得字符串用作参数,并捕获并返回返回值.有效地,它返回md5sum(由shell命令返回).

the "C*", shift on <2> converts the string into an array of numbers which is then joined together to form a new string by doing the sprintf formatting on each number. The resultant string is then used a aparameter when executing the command line printf '$encstring' | md5sum and the return is captured and returned. Effectively this returns with the md5sum ( as returned by the shell command ).

为例,以用户名"作为参数调用该函数将导致命令行构造为printf '\165\163\145\162\156\141\155\145' | md5sum,结果为14c4b06b824ec593239362517f538b29 -,但是如果我们删除管道的md5sum并仅在一个外壳,然后我们返回原始的'username'字符串.

as an example calling the function with 'username' as a parameter will result in the command line being constructed as printf '\165\163\145\162\156\141\155\145' | md5sum which gives a result of 14c4b06b824ec593239362517f538b29 - however if we drop the piped md5sum and just execute printf '\165\163\145\162\156\141\155\145' in a shell then we get the original string of 'username' returned.

在我看来,拆包,连接和打印的唯一原因是要提供更多的安全性,以便在有人入侵计算机并监视进程等情况下,进程不包含原始字符串.

It looks to me as though the only reason for the unpack,join and printf is to provide a little more security so processes do not contain the original string should somebody hack the machine and watch the processes etc.

在空格上分割似乎是用来省略命令行调用返回的尾随-字符,因此您只能通过将命令行中的返回字符串分割成2个值来获取md5sum,而只能将第一个值捕获到返回的值中$ out标量变量.

The split on whitespace appears to be used to ommit the trailing - character returned by the command line call so that you only get the md5sum by splitting the retruned string from the command line into 2 values but only capturing the first into the returned $out scalar variable.

我认为代码的编写者不能使用CPAN MD5模块,因此回退到使用命令行md5sum命令,但希望混淆和调用命令行.

I assume that the author of the code could not use the CPAN MD5 module and so fell back to using the command line md5sum command but wanted to obfuscate and command line calls.

您应该能够使用仅以字符串作为参数的php md5sum函数调用来替换整个函数( http://php.net/manual/en/function.md5.php )

You should be able to replace the entire function with a php md5sum function call with just the string as the parameter ( http://php.net/manual/en/function.md5.php )

这篇关于尝试将Perl转换为PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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