我试图将PHP
代码片段转换为Python3
代码,但是print
和{
你可以在第一步看到。在
你知道问题出在哪里吗?我也附加了输入数组,但我认为它们是相等的。在
�W2+ vs ee7523b2
编辑
当我将raw从TRUE切换到FALSE时,第一步的输出是相同的。$d = strrev(hash("crc32b", $d, FALSE)) . $d
但问题是我必须把PHP转换成Python,而不是相反,因为在第2步中我需要有相同的输出。在
PHP输出(CMD)
^{pr2}$PYTHON输出
^{3}$PHP
<?php
$suma = "100";
$datum = "20190101";
$varsym = "11111111";
$konsym = "";
$specsym = "";
$poznamka = "Faktúra";
$iban = "SK6807200002891987426353";
$swift = "";
$d = implode("\t", array(
0 => '',
1 => '1',
2 => implode("\t", array(
true,
$suma, // SUMA
'EUR', // JEDNOTKA
$datum, // DATUM
$varsym, // VARIABILNY SYMBOL
$konsym, // KONSTANTNY SYMBOL
$specsym, // SPECIFICKY SYMBOL
'',
$poznamka, // POZNAMKA
'1',
$iban, // IBAN
$swift, // SWIFT
'0',
'0'
))
));
// 0
echo "0 -> ".$d."\n";
$d = strrev(hash("crc32b", $d, TRUE)) . $d;
// 1
echo "1 -> ".$d."\n";
$x = proc_open("/usr/bin/xz '--format=raw' '--lzma1=lc=3,lp=0,pb=2,dict=128KiB' '-c' '-'", [0 => ["pipe", "r"], 1 => ["pipe", "w"]], $p);
fwrite($p[0], $d);
fclose($p[0]);
$o = stream_get_contents($p[1]);
fclose($p[1]);
proc_close($x);
$d = bin2hex("\x00\x00" . pack("v", strlen($d)) . $o);
// 2
echo "2 -> ".$d."\n";
?>
PYTHON
def crc32b(x):
h = zlib.crc32(x)
x='%08X' % (h & 0xffffffff,)
return x.lower()
t = "\t"
gen = t.join(["1",
"100", # SAME VARIABLES
"EUR",
"20190101",
"11111111",
"",
"",
"",
"Faktúra",
"1",
"SK6807200002891987426353",
"",
"0",
"0"]
)
d = t.join([
"", "1", gen])
# 0
print(f"0 -> {d}")
hashD = crc32b(d.encode()) # OK
hashD = hashD[::-1]
# hashD = str(binascii.unhexlify(hashD))
d = hashD + d
# 1
print(f"1 -> {d}")
args = shlex.split("xz '--format=raw' '--lzma1=lc=3,lp=0,pb=2,dict=128KiB' -c -")
process = subprocess.Popen(args, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output = process.communicate(d.encode())
pack = "\x00\x00" + str(struct.pack("H", len(d))) + str(output[0])
d = binascii.hexlify(pack.encode())
# 2
print(f"2 -> {d}")
无法与PHP一起工作。
根据[Python 3]: zlib.crc32(data[, value])(强调是我的):
你在混淆:
注意事项:
'î'
)看起来不同,但只是一个表示问题(在我的控制台vs您的控制台中)将其集成到代码中,您需要修改crc32b函数(并删除对hashD的任何进一步处理)以:
有关此一般主题的详细信息,请检查[SO]: Python struct.pack() behavior (@CristiFati's answer)。在
@EDIT0:
添加从十六进制表示形式开始的版本:
您只需删除函数
hash()
的第三个参数如果将此参数设置为true,hash将返回原始二进制数据,php将尝试将其解析为文本字符串,而您希望得到十六进制结果
相关问题 更多 >
编程相关推荐