我试图将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";
?>
PYTHONdef 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}")