Python binascii.crc32() Examples
The following are 30
code examples of binascii.crc32().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
binascii
, or try the search function
.
Example #1
Source File: DNS.py From XFLTReaT with MIT License | 8 votes |
def cmh_autotune(self, module, message, additional_data, cm): message = message[len(self.CONTROL_AUTOTUNE)+2:] # get tune type, requested record type, length and encoding for crafting the answer (query_type, RRtype, length, encode_class) = struct.unpack("<BHHH", message[0:7]) if self.DNS_proto.get_RR_type(RRtype)[0] == None: return True # extra parameters added to be able to response in the proper way additional_data = additional_data + (True, self.download_encoding_list[encode_class], self.DNS_proto.get_RR_type(RRtype)[0]) if (query_type == 0) or (query_type == 3): # record && downstream length discovery message = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length)) if query_type == 1: # A record name length discovery message = struct.pack("<i", binascii.crc32(message[7:])) if query_type == 2: # checking download encoding, echoing back request payload message = message[7:] module.send(common.CONTROL_CHANNEL_BYTE, self.CONTROL_AUTOTUNE_CLIENT+message, additional_data) return True # tune control message handler # client sets the record type and encodings by calling this # server side
Example #2
Source File: joystick.py From derplearning with MIT License | 6 votes |
def send(self, rumble_high=0, rumble_low=0, red=0, green=0, blue=0, light_on=0, light_off=0): """Actuate the controller by setting its rumble or light color/blink""" packet = bytearray(79) packet[:5] = [0xA2, 0x11, 0x80, 0x00, 0xFF] packet[7] = int(rumble_high * 255 + 0.5) packet[8] = int(rumble_low * 255 + 0.5) packet[9] = int(red * 255 + 0.5) packet[10] = int(green * 255 + 0.5) packet[11] = int(blue * 255 + 0.5) packet[12] = int(light_on * 255 + 0.5) packet[13] = int(light_off * 255 + 0.5) crc = crc32(packet[:-4]) packet[-4] = crc & 0x000000FF packet[-3] = (crc & 0x0000FF00) >> 8 packet[-2] = (crc & 0x00FF0000) >> 16 packet[-1] = (crc & 0xFF000000) >> 24 hid = bytearray((self.__report_id,)) if self.__fd is not None: self.__fd.write(hid + packet[2:]) return True return False
Example #3
Source File: zipfile.py From ironpython2 with Apache License 2.0 | 6 votes |
def _GenerateCRCTable(): """Generate a CRC-32 table. ZIP encryption uses the CRC32 one-byte primitive for scrambling some internal keys. We noticed that a direct implementation is faster than relying on binascii.crc32(). """ poly = 0xedb88320 table = [0] * 256 for i in range(256): crc = i for j in range(8): if crc & 1: crc = ((crc >> 1) & 0x7FFFFFFF) ^ poly else: crc = ((crc >> 1) & 0x7FFFFFFF) table[i] = crc return table
Example #4
Source File: zipfile.py From meddle with MIT License | 6 votes |
def _GenerateCRCTable(): """Generate a CRC-32 table. ZIP encryption uses the CRC32 one-byte primitive for scrambling some internal keys. We noticed that a direct implementation is faster than relying on binascii.crc32(). """ poly = 0xedb88320 table = [0] * 256 for i in range(256): crc = i for j in range(8): if crc & 1: crc = ((crc >> 1) & 0x7FFFFFFF) ^ poly else: crc = ((crc >> 1) & 0x7FFFFFFF) table[i] = crc return table
Example #5
Source File: zipfile.py From BinderFilter with MIT License | 6 votes |
def _GenerateCRCTable(): """Generate a CRC-32 table. ZIP encryption uses the CRC32 one-byte primitive for scrambling some internal keys. We noticed that a direct implementation is faster than relying on binascii.crc32(). """ poly = 0xedb88320 table = [0] * 256 for i in range(256): crc = i for j in range(8): if crc & 1: crc = ((crc >> 1) & 0x7FFFFFFF) ^ poly else: crc = ((crc >> 1) & 0x7FFFFFFF) table[i] = crc return table
Example #6
Source File: standards.py From python_moztelemetry with Mozilla Public License 2.0 | 6 votes |
def sampler(dataframe, modulo, column="client_id", sample_id=42): """ Collect a sample of clients given an input column Filter dataframe based on the modulus of the CRC32 of a given string column matching a given sample_id. if dataframe has already been filtered by sample_id, then modulo should be a multiple of 100, column should be "client_id", and the given sample_id should match the value previously used, optionally plus multiples of 100. Args: dataframe: A Dataframe to be sampled modulo (int): selects a 1/modulo sampling of dataframe column (str): name of a string column to sample on sample_id (int): modulus result to select for sampling Returns: A DataFrame sampled on the given inputs. """ return dataframe \ .withColumn( "sampler", udf(lambda key: (crc32(key or "") & 0xffffffff) % modulo)(column), ).where("sampler = %s" % sample_id).drop("sampler")
Example #7
Source File: zipfile.py From oss-ftp with MIT License | 6 votes |
def _GenerateCRCTable(): """Generate a CRC-32 table. ZIP encryption uses the CRC32 one-byte primitive for scrambling some internal keys. We noticed that a direct implementation is faster than relying on binascii.crc32(). """ poly = 0xedb88320 table = [0] * 256 for i in range(256): crc = i for j in range(8): if crc & 1: crc = ((crc >> 1) & 0x7FFFFFFF) ^ poly else: crc = ((crc >> 1) & 0x7FFFFFFF) table[i] = crc return table
Example #8
Source File: ch09_listing_source.py From https---github.com-josiahcarlson-redis-in-action with MIT License | 6 votes |
def shard_key(base, key, total_elements, shard_size): #A if isinstance(key, (int, long)) or key.isdigit(): #B shard_id = int(str(key), 10) // shard_size #C else: shards = 2 * total_elements // shard_size #D shard_id = binascii.crc32(key) % shards #E return "%s:%s"%(base, shard_id) #F # <end id="calculate-shard-key"/> #A We will call the shard_key() function with a base HASH name, along with the key to be stored in the sharded HASH, the total number of expected elements, and the desired shard size #B If the value is an integer or a string that looks like an integer, we will use it directly to calculate the shard id #C For integers, we assume they are sequentially assigned ids, so we can choose a shard id based on the upper 'bits' of the numeric id itself. We also use an explicit base here (necessitating the str() call) so that a key of '010' turns into 10, and not 8 #D For non-integer keys, we first calculate the total number of shards desired, based on an expected total number of elements and desired shard size #E When we know the number of shards we want, we hash the key and find its value modulo the number of shards we want #F Finally, we combine the base key with the shard id we calculated to determine the shard key #END # <start id="sharded-hset-hget"/>
Example #9
Source File: eventcluster.py From msticpy with MIT License | 6 votes |
def crc32_hash_df(data: pd.DataFrame, column: str) -> pd.Series: """ Return the CRC32 hash of the input column. Parameters ---------- data : pd.DataFrame The DataFrame to process column : str Column name to process Returns ------- pd.Series CRC32 hash of input column """ return data.apply(lambda x: crc32(bytes(x[column].encode("utf-8"))), axis=1) # pylint: disable=too-many-arguments, too-many-statements
Example #10
Source File: eventcluster.py From msticpy with MIT License | 6 votes |
def crc32_hash(value: str) -> int: """ Return the CRC32 hash of the input column. Parameters ---------- value : str Data to process Returns ------- int CRC32 hash """ return crc32(bytes(value.encode("utf-8")))
Example #11
Source File: eventcluster.py From msticpy with MIT License | 6 votes |
def delim_hash(value: str, delim_list: str = r'[\s\-\\/\.,"\'|&:;%$()]') -> int: r""" Return a hash (CRC32) of the delimiters from input column. Parameters ---------- value : str Data to process delim_list : str, optional delimiters to use. (the default is r'[\\s\\\\-\\\\\\\\/\.,"\\\\'|&:;%$()]') Returns ------- int Hash of delimiter set in the string. """ return crc32(bytes("".join(re.findall(delim_list, value)), "utf-8"))
Example #12
Source File: wordlist.py From monero-python with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_checksum(cls, phrase): """Given a mnemonic word string, return a string of the computed checksum. :rtype: str """ phrase_split = phrase.split(" ") if len(phrase_split) < 12: raise ValueError("Invalid mnemonic phrase") if len(phrase_split) > 13: # Standard format phrase = phrase_split[:24] else: # MyMonero format phrase = phrase_split[:12] wstr = "".join(word[:cls.unique_prefix_length] for word in phrase) wstr = bytearray(wstr.encode('utf-8')) z = ((crc32(wstr) & 0xffffffff) ^ 0xffffffff ) >> 0 z2 = ((z ^ 0xffffffff) >> 0) % len(phrase) return phrase_split[z2]
Example #13
Source File: zipfile.py From jawfish with MIT License | 6 votes |
def _GenerateCRCTable(): """Generate a CRC-32 table. ZIP encryption uses the CRC32 one-byte primitive for scrambling some internal keys. We noticed that a direct implementation is faster than relying on binascii.crc32(). """ poly = 0xedb88320 table = [0] * 256 for i in range(256): crc = i for j in range(8): if crc & 1: crc = ((crc >> 1) & 0x7FFFFFFF) ^ poly else: crc = ((crc >> 1) & 0x7FFFFFFF) table[i] = crc return table
Example #14
Source File: mtprotoproxy.py From mtprotoproxy with MIT License | 5 votes |
def read(self, buf_size): msg_len_bytes = await self.upstream.readexactly(4) msg_len = int.from_bytes(msg_len_bytes, "little") # skip paddings while msg_len == 4: msg_len_bytes = await self.upstream.readexactly(4) msg_len = int.from_bytes(msg_len_bytes, "little") len_is_bad = (msg_len % len(PADDING_FILLER) != 0) if not MIN_MSG_LEN <= msg_len <= MAX_MSG_LEN or len_is_bad: print_err("msg_len is bad, closing connection", msg_len) return b"" msg_seq_bytes = await self.upstream.readexactly(4) msg_seq = int.from_bytes(msg_seq_bytes, "little", signed=True) if msg_seq != self.seq_no: print_err("unexpected seq_no") return b"" self.seq_no += 1 data = await self.upstream.readexactly(msg_len - 4 - 4 - 4) checksum_bytes = await self.upstream.readexactly(4) checksum = int.from_bytes(checksum_bytes, "little") computed_checksum = binascii.crc32(msg_len_bytes + msg_seq_bytes + data) if computed_checksum != checksum: return b"" return data
Example #15
Source File: zipfile.py From oss-ftp with MIT License | 5 votes |
def _update_crc(self, newdata, eof): # Update the CRC using the given data. if self._expected_crc is None: # No need to compute the CRC if we don't have a reference value return self._running_crc = crc32(newdata, self._running_crc) & 0xffffffff # Check the CRC if we're at the end of the file if eof and self._running_crc != self._expected_crc: raise BadZipfile("Bad CRC-32 for file %r" % self.name)
Example #16
Source File: test_zlib.py From oss-ftp with MIT License | 5 votes |
def test_crc32empty(self): self.assertEqual(zlib.crc32("", 0), 0) self.assertEqual(zlib.crc32("", 1), 1) self.assertEqual(zlib.crc32("", 432), 432)
Example #17
Source File: zipfile.py From oss-ftp with MIT License | 5 votes |
def __init__(self, fileobj, mode, zipinfo, decrypter=None, close_fileobj=False): self._fileobj = fileobj self._decrypter = decrypter self._close_fileobj = close_fileobj self._compress_type = zipinfo.compress_type self._compress_size = zipinfo.compress_size self._compress_left = zipinfo.compress_size if self._compress_type == ZIP_DEFLATED: self._decompressor = zlib.decompressobj(-15) elif self._compress_type != ZIP_STORED: descr = compressor_names.get(self._compress_type) if descr: raise NotImplementedError("compression type %d (%s)" % (self._compress_type, descr)) else: raise NotImplementedError("compression type %d" % (self._compress_type,)) self._unconsumed = '' self._readbuffer = '' self._offset = 0 self._universal = 'U' in mode self.newlines = None # Adjust read size for encrypted files since the first 12 bytes # are for the encryption/password information. if self._decrypter is not None: self._compress_left -= 12 self.mode = mode self.name = zipinfo.filename if hasattr(zipinfo, 'CRC'): self._expected_crc = zipinfo.CRC self._running_crc = crc32(b'') & 0xffffffff else: self._expected_crc = None
Example #18
Source File: test_zlib.py From oss-ftp with MIT License | 5 votes |
def test_abcdefghijklmnop(self): """test issue1202 compliance: signed crc32, adler32 in 2.x""" foo = 'abcdefghijklmnop' # explicitly test signed behavior self.assertEqual(zlib.crc32(foo), -1808088941) self.assertEqual(zlib.crc32('spam'), 1138425661) self.assertEqual(zlib.adler32(foo+foo), -721416943) self.assertEqual(zlib.adler32('spam'), 72286642)
Example #19
Source File: test_zlib.py From oss-ftp with MIT License | 5 votes |
def test_penguins(self): self.assertEqual32(zlib.crc32("penguin", 0), 0x0e5c1a120L) self.assertEqual32(zlib.crc32("penguin", 1), 0x43b6aa94) self.assertEqual32(zlib.adler32("penguin", 0), 0x0bcf02f6) self.assertEqual32(zlib.adler32("penguin", 1), 0x0bd602f7) self.assertEqual(zlib.crc32("penguin"), zlib.crc32("penguin", 0)) self.assertEqual(zlib.adler32("penguin"),zlib.adler32("penguin",1))
Example #20
Source File: isis.py From genielibs with Apache License 2.0 | 5 votes |
def system_id(self): system_id = self.parent.system_id if system_id is None: unique_int = binascii.crc32(self.device_name.encode()) system_id = IsisSystemID( 'FFFF.{:04X}.{:04X}'.format( (unique_int >> 16) & 0xFFFF, unique_int & 0xFFFF, )) return system_id
Example #21
Source File: test_zlib.py From oss-ftp with MIT License | 5 votes |
def test_negative_crc_iv_input(self): # The range of valid input values for the crc state should be # -2**31 through 2**32-1 to allow inputs artifically constrained # to a signed 32-bit integer. self.assertEqual(zlib.crc32('ham', -1), zlib.crc32('ham', 0xffffffffL)) self.assertEqual(zlib.crc32('spam', -3141593), zlib.crc32('spam', 0xffd01027L)) self.assertEqual(zlib.crc32('spam', -(2**31)), zlib.crc32('spam', (2**31)))
Example #22
Source File: isis.py From genielibs with Apache License 2.0 | 5 votes |
def area_addresses(self): area_addresses = self.parent.area_addresses if area_addresses is None: unique_int = binascii.crc32(self.device_name.encode()) area_addresses = [ IsisAreaAddress( '47.{:04X}.{:04X}'.format( (unique_int >> 16) & 0xFFFF, unique_int & 0xFFFF, ))] return frozenset(area_addresses)
Example #23
Source File: isis.py From genielibs with Apache License 2.0 | 5 votes |
def area_addresses(self): unique_int = binascii.crc32(self.pid.encode()) return frozenset([ IsisAreaAddress( '47.{:04X}.{:04X}'.format( (unique_int >> 16) & 0xFFFF, unique_int & 0xFFFF, ))])
Example #24
Source File: legacy_records.py From aiokafka with Apache License 2.0 | 5 votes |
def validate_crc(self): crc = crc32(self._buffer[self.MAGIC_OFFSET:]) return self._crc == crc
Example #25
Source File: mtprotoproxy.py From mtprotoproxy with MIT License | 5 votes |
def write(self, msg, extra={}): len_bytes = int.to_bytes(len(msg) + 4 + 4 + 4, 4, "little") seq_bytes = int.to_bytes(self.seq_no, 4, "little", signed=True) self.seq_no += 1 msg_without_checksum = len_bytes + seq_bytes + msg checksum = int.to_bytes(binascii.crc32(msg_without_checksum), 4, "little") full_msg = msg_without_checksum + checksum padding = PADDING_FILLER * ((-len(full_msg) % CBC_PADDING) // len(PADDING_FILLER)) return self.upstream.write(full_msg + padding)
Example #26
Source File: zipfile.py From BinderFilter with MIT License | 5 votes |
def __init__(self, fileobj, mode, zipinfo, decrypter=None, close_fileobj=False): self._fileobj = fileobj self._decrypter = decrypter self._close_fileobj = close_fileobj self._compress_type = zipinfo.compress_type self._compress_size = zipinfo.compress_size self._compress_left = zipinfo.compress_size if self._compress_type == ZIP_DEFLATED: self._decompressor = zlib.decompressobj(-15) elif self._compress_type != ZIP_STORED: descr = compressor_names.get(self._compress_type) if descr: raise NotImplementedError("compression type %d (%s)" % (self._compress_type, descr)) else: raise NotImplementedError("compression type %d" % (self._compress_type,)) self._unconsumed = '' self._readbuffer = '' self._offset = 0 self._universal = 'U' in mode self.newlines = None # Adjust read size for encrypted files since the first 12 bytes # are for the encryption/password information. if self._decrypter is not None: self._compress_left -= 12 self.mode = mode self.name = zipinfo.filename if hasattr(zipinfo, 'CRC'): self._expected_crc = zipinfo.CRC self._running_crc = crc32(b'') & 0xffffffff else: self._expected_crc = None
Example #27
Source File: ch10_listing_source.py From https---github.com-josiahcarlson-redis-in-action with MIT License | 5 votes |
def shard_key(base, key, total_elements, shard_size): #A if isinstance(key, (int, long)) or key.isdigit(): #B shard_id = int(str(key), 10) // shard_size #C else: shards = 2 * total_elements // shard_size #D shard_id = binascii.crc32(key) % shards #E return "%s:%s"%(base, shard_id) #F
Example #28
Source File: codesys_gateway_v3_config_modification_tra_2020_04.py From poc with BSD 3-Clause "New" or "Revised" License | 5 votes |
def layer7(svc_group, svc_num, sess_id, data): hdr = pack('<HHII',svc_group, svc_num, sess_id, len(data)) hdr += pack('<HH', 0, 0) pdu = pack('<HH',0xcd55, len(hdr)) pdu += hdr pdu += data # Layer7 body pdu = pack('<Ii', len(pdu), binascii.crc32(pdu)) + pdu return pdu
Example #29
Source File: codesys_gateway_v3_config_modification_tra_2020_04.py From poc with BSD 3-Clause "New" or "Revised" License | 5 votes |
def layer4_meta(type, data): type |= 0xC0 # Meta request from client pdu = pack('<BBH', type, 0, 0x0101) pdu = pdu + pack('<i', binascii.crc32(pdu + '\x00'*4 + data)) + data return pdu
Example #30
Source File: mbed.py From web2board with GNU Lesser General Public License v3.0 | 5 votes |
def add_mbedlib(libname, libar): if libar in env.get("LIBS"): return lib_dir = join(env.subst("$PLATFORMFW_DIR"), "libs", libname) if not isfile(join(lib_dir, "TARGET_%s" % variant, "TOOLCHAIN_GCC_ARM", "lib%s.a" % libar)): print ( "Warning: %s board doesn't have native support for '%s' library!" % (env.get("BOARD"), libname), file=sys.stderr) return env.Append( LIBPATH=[ join(env.subst("$PLATFORMFW_DIR"), "libs", libname, "TARGET_%s" % variant, "TOOLCHAIN_GCC_ARM") ], LIBS=[libar] ) sysincdirs = ( "eth", "include", "ipv4", "lwip-eth", "lwip-sys" ) for root, _, files in walk(lib_dir): if (not any(f.endswith(".h") for f in files) and basename(root) not in sysincdirs): continue var_dir = join("$BUILD_DIR", "FrameworkMbed%sInc%d" % (libname.upper(), crc32(root))) if var_dir in env.get("CPPPATH"): continue env.VariantDirWrap(var_dir, root) env.Append(CPPPATH=[var_dir])