日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

Python CRC32 文件校验

發(fā)布時(shí)間:2023/11/27 生活经验 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python CRC32 文件校验 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

binascii.crc32(s [,crc]) 返回CRC32校驗(yàn)。參數(shù)'crc'指定初始值用于循環(huán)。例如:

?

Code
>>>?import?binascii
>>>?crc?=?binascii.crc32('spam')
>>>?binascii.crc32('?and?eggs',?crc)
739139840
>>>?binascii.crc32('spam?and?eggs')
739139840

?

?

Code
import?binascii?

def?getFileCRC(_path):?
????
try:?
????????blocksize?
=?1024?*?64?
????????f?
=?open(_path,"rb")?
????????str?
=?f.read(blocksize)?
????????crc?
=?0?
????????
while(len(str)?!=?0):?
????????????crc?
=?binascii.crc32(str,?crc)?
????????????str?
=?f.read(blocksize)?
????????f.close()?
????
except:?
????????
print?'get?file?crc?error!'?
????????
return?0?
????
return?crc??

?

?

Code
python?2.X的crc32實(shí)作上跟一般的C實(shí)作上在整數(shù)有號(hào)無(wú)號(hào)的處理上略有不同,?所以使用python?2.X與一般C實(shí)作算出的crc32(如sfv)比對(duì)時(shí),通常需要特別的方法,

這邊列出一個(gè)透過(guò)zlib.crc32快速得到所需要結(jié)果的方法:


import?zlib

def?crc32(st):
????crc?
=?zlib.crc32(st)
????
if?crc?>?0:
??????
return?"%x"?%?(crc)
????
else:
??????
return?"%x"?%?(~crc?^?0xffffffff)

ex1?
=?"12345"
ex2?
=?"1kcaseztsa12345azy"

print?"%x"?%?zlib.crc32(ex1)
print?crc32(ex1)
print?"%x"?%?zlib.crc32(ex2)
print?crc32(ex2)


或如果你有ctypes的話:
import?zlib
import?ctypes

def?crc32_c(st):
????
return?"%x"?%?ctypes.c_uint32(zlib.crc32(st)).value

ex1?
=?"12345"
ex2?
=?"1kcaseztsa12345azy"

print?"%x"?%?zlib.crc32(ex1)
print?crc32_c(ex1)
print?"%x"?%?zlib.crc32(ex2)
print?crc32_c(ex2)



註:?python?
3.0以上沒(méi)有這個(gè)問(wèn)題.

?

?

Code
from?ctypes?import?*?
import?binascii?

def?getFileCRC(_path):?
try:?
blocksize?
=?1024?*?64?
f?
=?open(_path,"rb")?
str?
=?f.read(blocksize)?
crc?
=?0?
while(len(str)?!=?0):?
crc?
=?binascii.crc32(str,?crc)?
str?
=?f.read(blocksize)?
f.close()?
except:?
klog.error(
"get?file?crc?error!")?
return?0?
return?c_uint(crc).value

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/leavingme/archive/2009/06/14/1503120.html

總結(jié)

以上是生活随笔為你收集整理的Python CRC32 文件校验的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。