与php base64_encode相当的Python
PHP与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
$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中都会得到相同的结果。