本文整理汇总了Python中zlib.crc32方法的典型用法代码示例。如果您正苦于以下问题:Python zlib.crc32方法的具体用法?Python zlib.crc32怎么用?Python zlib.crc32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zlib
的用法示例。
在下文中一共展示了zlib.crc32方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_img_cdn_hard_rewrite
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def test_img_cdn_hard_rewrite(self):
"""测试重写html中CDN的链接 https://httpbin.org/"""
# 第一次请求, 没有使用CDN
self.rv = self.client.get(
self.url("/image/jpeg"),
environ_base=env(),
headers=headers()
) # type: Response
# 由于flaks的惰性, 需要先实际获取一次结果, 缓存才能实际被存储生效
self.assertEqual("image/jpeg", self.rv.content_type, msg=self.dump())
self.assertEqual(200, self.rv.status_code, msg=self.dump())
self.assertEqual(0x97ca823f, crc32(self.rv.data), msg=self.dump())
with self.app.test_client() as c:
# 请求包含 https://httpbin.org/image/jpeg 的页面, 其中这张图片的链接会被重写成CDN
self.rv2 = c.get(
self.url("/base64/PGltZyBzcmM9Imh0dHBzOi8vaHR0cGJpbi5vcmcvaW1hZ2UvanBlZyI+Cg=="),
environ_base=env(),
headers=headers()
) # type: Response
self.assertIn(b"cdn2.zmirror-unittest.com/image/jpeg", self.rv2.data, msg=self.dump())
示例2: __init__
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def __init__(self, seed):
if not seed:
seed = "%.1f" % time.time()
if hasattr(seed, "encode"):
seed = seed.encode('ascii')
# A note on hashfunctions.
# We don't need cryptographic quality, so we won't use hashlib -
# that'd be way to slow. The zlib module contains two hash
# functions. Adler32 is fast, but not very uniform for short
# strings. Crc32 is slower, but has better bit distribution.
# So, we use crc32 whenever the hash is converted into an
# exportable number, but we use adler32 when we're producing
# intermediate values.
self.seed = zlib.adler32(seed)
self.text_seed = seed
# Default, typically overridden
self.size = 1024 + 786j
示例3: uniform
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def uniform(self, key, min_value=0., max_value=1.):
"""Returns a random number between min_value and max_value"""
return min_value + self._random(key) * (max_value - min_value)
# def complex(self, key):
# """Returns a random complex number.
# Both real and imaginary component are between 0 and 1 (inclusive)"""
# if hasattr(key, "encode"):
# key.encode('ascii')
# value1 = zlib.crc32(key, self.seed) & MAX_VALUE
# value2 = zlib.crc32(key, value1) & MAX_VALUE
# return (float(value1) + 1j * float(value2)) * INV_MAX_VALUE
示例4: _get_crc32
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def _get_crc32(self, filename):
"""
Calculates and compares the CRC32 and returns the raw buffer.
The CRC32 is added to `files_crc32` dictionary, if not present.
:param filename: filename inside the zipfile
:rtype: bytes
"""
buffer = self.zip.read(filename)
if filename not in self.files_crc32:
self.files_crc32[filename] = crc32(buffer)
if self.files_crc32[filename] != self.zip.getinfo(filename).CRC:
log.error("File '{}' has different CRC32 after unpacking! "
"Declared: {:08x}, Calculated: {:08x}".format(filename,
self.zip.getinfo(filename).CRC,
self.files_crc32[filename]))
return buffer
示例5: annotation_stream
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def annotation_stream(self, with_checksum=False):
# create a large temporary file
f = tempfile.TemporaryFile()
for _ in range(5000):
f.write(b"1234567890!" * 1000)
filesize = f.tell()
f.seek(os.SEEK_SET, 0)
# return the file data via annotation stream (remote iterator)
annotation_size = 500000
print("transmitting file via annotations stream (%d bytes in chunks of %d)..." % (filesize, annotation_size))
with f:
while True:
chunk = f.read(annotation_size)
if not chunk:
break
# store the file data chunk in the FDAT response annotation,
# and return the current file position and checksum (if asked).
current_context.response_annotations = {"FDAT": chunk}
yield f.tell(), zlib.crc32(chunk) if with_checksum else 0
示例6: via_annotation_stream
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def via_annotation_stream(uri):
name = threading.currentThread().name
start = time.time()
total_size = 0
print("thread {0} downloading via annotation stream...".format(name))
with Proxy(uri) as p:
perform_checksum = False
for progress, checksum in p.annotation_stream(perform_checksum):
chunk = current_context.response_annotations["FDAT"]
if perform_checksum and zlib.crc32(chunk) != checksum:
raise ValueError("checksum error")
total_size += len(chunk)
assert progress == total_size
current_context.response_annotations.clear() # clean them up once we're done with them
duration = time.time() - start
print("thread {0} done, {1:.2f} Mb/sec.".format(name, total_size/1024.0/1024.0/duration))
示例7: computehash
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def computehash((filedir, filename, extrahashes, sha256sum)):
resolved_path = os.path.join(filedir, filename)
filehashes = {}
filehashes['sha256'] = sha256sum
scanfile = open(resolved_path, 'r')
data = scanfile.read()
scanfile.close()
for i in extrahashes:
if i == 'crc32':
filehashes[i] = zlib.crc32(data) & 0xffffffff
elif i == 'tlsh':
if os.stat(resolved_path).st_size >= 256:
tlshhash = tlsh.hash(data)
filehashes[i] = tlshhash
else:
filehashes[i] = None
else:
h = hashlib.new(i)
h.update(data)
filehashes[i] = h.hexdigest()
filehashes['sha256'] = sha256sum
return (filedir, filename, filehashes)
示例8: write
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def write(self,data):
self._check_closed()
if self.mode != WRITE:
import errno
raise IOError(errno.EBADF, "write() on read-only GzipFile object")
if self.fileobj is None:
raise ValueError, "write() on closed GzipFile object"
# Convert data type if called by io.BufferedWriter.
if isinstance(data, memoryview):
data = data.tobytes()
if len(data) > 0:
self.size = self.size + len(data)
self.crc = zlib.crc32(data, self.crc) & 0xffffffffL
self.fileobj.write( self.compress.compress(data) )
self.offset += len(data)
return len(data)
示例9: _read_eof
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def _read_eof(self):
# We've read to the end of the file, so we have to rewind in order
# to reread the 8 bytes containing the CRC and the file size.
# We check the that the computed CRC and size of the
# uncompressed data matches the stored values. Note that the size
# stored is the true file size mod 2**32.
self.fileobj.seek(-8, 1)
crc32 = read32(self.fileobj)
isize = read32(self.fileobj) # may exceed 2GB
if crc32 != self.crc:
raise IOError("CRC check failed %s != %s" % (hex(crc32),
hex(self.crc)))
elif isize != (self.size & 0xffffffffL):
raise IOError, "Incorrect length of data produced"
# Gzip files can be padded with zeroes and still have archives.
# Consume all zero bytes and set the file position to the first
# non-zero byte. See http://www.gzip.org/#faq8
c = "\x00"
while c == "\x00":
c = self.fileobj.read(1)
if c:
self.fileobj.seek(-1, 1)
示例10: gzip_app_iter
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def gzip_app_iter(app_iter):
size = 0
crc = zlib.crc32(b"") & 0xffffffff
compress = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL, 0)
yield _gzip_header
for item in app_iter:
size += len(item)
crc = zlib.crc32(item, crc) & 0xffffffff
# The compress function may return zero length bytes if the input is
# small enough; it buffers the input for the next iteration or for a
# flush.
result = compress.compress(item)
if result:
yield result
# Similarly, flush may also not yield a value.
result = compress.flush()
if result:
yield result
yield struct.pack("<2L", crc, size & 0xffffffff)
示例11: get_files_types
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def get_files_types(self):
"""
Return the files inside the APK with their associated types (by using python-magic)
:rtype: a dictionnary
"""
if self._files == {}:
# Generate File Types / CRC List
for i in self.get_files():
buffer = self.zip.read(i)
self.files_crc32[i] = crc32(buffer)
# FIXME why not use the crc from the zipfile?
# should be validated as well.
# crc = self.zip.getinfo(i).CRC
self._files[i] = self._get_file_magic_name(buffer)
return self._files
示例12: write
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def write(self,data):
self._check_closed()
if self.mode != WRITE:
import errno
raise IOError(errno.EBADF, "write() on read-only GzipFile object")
if self.fileobj is None:
raise ValueError, "write() on closed GzipFile object"
# Convert data type if called by io.BufferedWriter.
if isinstance(data, memoryview):
data = data.tobytes()
if len(data) > 0:
self.fileobj.write(self.compress.compress(data))
self.size += len(data)
self.crc = zlib.crc32(data, self.crc) & 0xffffffffL
self.offset += len(data)
return len(data)
示例13: __init__
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def __init__(self, *args):
super().__init__(*args)
self.packsize = None # uint32|64 packed size
self.origsize = None # uint32|64 original size
self.datetime = None # uint32 ctime
self.attribs = None # uint32 file attributes
self.crc32 = None # uint32 checksum over compressed file
self.comptype = None # uint8 compression type
self.compqual = None # uint8 compression quality
self.params = None # uint16 decompression parameters
self.reserved1 = None # uint16
self.filename = None # [uint16]
self.comment = b'' # [uint16] optional, compressed
self.ntsecurity = b'' # [uint16] optional
self.reserved2 = None # ?
self.dataoffset = None # position of data after hdr
示例14: fix_png
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def fix_png(data):
"""
Fix the signature and checksums on a fuzzed PNG image.
"""
out = [b"\x89PNG\r\n\x1A\n"]
data = bytes(data[8:])
chunk = 0
while len(data) >= 8:
chunklen = data[:4]
out.append(chunklen)
chunklen = struct.unpack("!I", chunklen)[0]
if chunk == 0:
chunkname = b"IHDR" # make sure the first tag is correct
else:
chunkname = data[4:8]
#chunkname = bytes(_coerce_ascii(c) for c in data[4:8])
out.append(chunkname)
data = data[8:]
if len(data) < chunklen:
break
else:
chunkdata = data[:chunklen]
chunkcrc = zlib.crc32(chunkname) & 0xFFFFFFFF
chunkcrc = zlib.crc32(chunkdata, chunkcrc) & 0xFFFFFFFF
out.append(chunkdata)
out.append(struct.pack("!I", chunkcrc))
data = data[chunklen+4:] # skip the old crc
chunk += 1
out.append(data)
return b"".join(out)
示例15: compress
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import crc32 [as 别名]
def compress(body, compress_level):
"""Compress 'body' at the given compress_level."""
import zlib
# See http://www.gzip.org/zlib/rfc-gzip.html
yield b'\x1f\x8b' # ID1 and ID2: gzip marker
yield b'\x08' # CM: compression method
yield b'\x00' # FLG: none set
# MTIME: 4 bytes
yield struct.pack('<L', int(time.time()) & int('FFFFFFFF', 16))
yield b'\x02' # XFL: max compression, slowest algo
yield b'\xff' # OS: unknown
crc = zlib.crc32(b'')
size = 0
zobj = zlib.compressobj(compress_level,
zlib.DEFLATED, -zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL, 0)
for line in body:
size += len(line)
crc = zlib.crc32(line, crc)
yield zobj.compress(line)
yield zobj.flush()
# CRC32: 4 bytes
yield struct.pack('<L', crc & int('FFFFFFFF', 16))
# ISIZE: 4 bytes
yield struct.pack('<L', size & int('FFFFFFFF', 16))