Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与php base64_encode相当的Python_Php_Python_Encoding_Base64 - Fatal编程技术网

与php base64_encode相当的Python

与php base64_encode相当的Python,php,python,encoding,base64,Php,Python,Encoding,Base64,PHP <?php $string = base64_encode(sha1( 'ABCD' , true ) ); echo sha1('ABCD'); echo $string; ?> 输出: “fb2f85c88567f3c8ce9b799c7c54642d0c7b41f6” ZMIYZJG1YZG4Nty3ZJNjongnlowi3OTLJN2m1Ndy0MmqWYZdindfmng== PHP和Python SHA1都可以正常工作,但是Python中的base64.en

PHP

<?php

$string = base64_encode(sha1( 'ABCD' , true ) );
echo sha1('ABCD');
echo $string;
?>
输出:

“fb2f85c88567f3c8ce9b799c7c54642d0c7b41f6” ZMIYZJG1YZG4Nty3ZJNjongnlowi3OTLJN2m1Ndy0MmqWYZdindfmng==

PHP和Python SHA1都可以正常工作,但是Python中的
base64.encodestring()
返回的值与PHP中的
base64\u encode()
不同


Python中PHP
base64\u encode
的等效方法是什么?

您在PHP和Python中编码不同的sha1结果

在PHP中:

// The second argument (true) to sha1 will make it return the raw output
// which means that you're encoding the raw output.
$string = base64_encode(sha1( 'ABCD' , true ) );

// Here you print the non-raw output
echo sha1('ABCD');
在Python中:

s = hashlib.sha1()  
s.update('ABCD')

// Here you're converting the raw output to hex
myhash = s.hexdigest()
print myhash

// Here you're actually encoding the hex version instead of the raw 
// (which was the one you encoded in PHP)
print base64.encodestring(myhash)   
如果对原始和非原始输出进行base64编码,将得到不同的结果。

编码哪种并不重要,只要是一致的。

在PHP和Python中编码不同的sha1结果

在PHP中:

// The second argument (true) to sha1 will make it return the raw output
// which means that you're encoding the raw output.
$string = base64_encode(sha1( 'ABCD' , true ) );

// Here you print the non-raw output
echo sha1('ABCD');
在Python中:

s = hashlib.sha1()  
s.update('ABCD')

// Here you're converting the raw output to hex
myhash = s.hexdigest()
print myhash

// Here you're actually encoding the hex version instead of the raw 
// (which was the one you encoded in PHP)
print base64.encodestring(myhash)   
如果对原始和非原始输出进行base64编码,将得到不同的结果。
编码哪种并不重要,只要是一致的。

使用
sha1.digest()
而不是
sha1.hexdigest()

base64.encodestring
在您为字符串指定十六进制表示形式时,希望使用该字符串。

使用
sha1.digest()
而不是
sha1.hexdigest()


base64.encodestring
在您为其提供十六进制表示形式时,希望该字符串是正确的。

base64.b64encode(s.digest())
具有正确的响应

base64.b64encode(s.digest())
有正确的响应

那么如何获得python中的原始输出?我认为从
sha1('ABC',true)
中删除
true
比较容易。这就是我要说的。如果删除
true
,PHP和Python中都应该得到相同的结果。那么如何获得Python中的原始输出呢?我认为从
sha1('ABC',true)
中删除
true
更容易。这就是我要说的。如果您删除了
true
,那么在PHP和Python中都会得到相同的结果。