当前位置: 首页>>代码示例>>Python>>正文


Python BitArray.cut方法代码示例

本文整理汇总了Python中bitstring.BitArray.cut方法的典型用法代码示例。如果您正苦于以下问题:Python BitArray.cut方法的具体用法?Python BitArray.cut怎么用?Python BitArray.cut使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bitstring.BitArray的用法示例。


在下文中一共展示了BitArray.cut方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: command

# 需要导入模块: from bitstring import BitArray [as 别名]
# 或者: from bitstring.BitArray import cut [as 别名]
 def command(self):
     """The bytes of the command that should update the data"""
     command = BitArray("0b100101")  # magic WRITE code
     # make sure the values are single bits
     command += [BitArray(bool=bit) for bit in (self.outtmg, self.extgck, self.tmgrst, self.dsprpt, self.blank)]
     for b in self.brightness:
         command += BitArray(uint=b, length=7)
     for rgb in self.pixels:
         for color in rgb:
             command += BitArray(uint=color, length=16)
     assert len(command) == 224
     return tuple([ba.uint for ba in command.cut(8)])
开发者ID:WanderingStar,项目名称:rpi,代码行数:14,代码来源:tlc59711.py

示例2: double_DES

# 需要导入模块: from bitstring import BitArray [as 别名]
# 或者: from bitstring.BitArray import cut [as 别名]
def double_DES(key, plain):
	keybits = BitArray(hex = key)
	plainbits= BitArray(hex = plain)


	if len(keybits) != 112:
		print "length of double DES key is not 112 bits!"
		return
	if len(plainbits) != 64:
		print "length of double DES plain text is not 64 bits!"
		return

	rev_keybytes = ''
	for k in keybits.cut(7):
		rev_keybytes += (odd_parity[k.bin])

	obj1 = DES.new(rev_keybytes[0:8], DES.MODE_ECB)
	midbytes = obj1.encrypt(plainbits.tobytes())
	obj2 = DES.new(rev_keybytes[8:16], DES.MODE_ECB)
	cipherbytes = obj2.encrypt(midbytes)
	return BitArray(bytes = cipherbytes).hex
开发者ID:twinkleMing,项目名称:CIS753_PA1,代码行数:23,代码来源:funcs.py

示例3: BitArray

# 需要导入模块: from bitstring import BitArray [as 别名]
# 或者: from bitstring.BitArray import cut [as 别名]

#.........这里部分代码省略.........
                raise InvalidDUKPTArguments("Must have either ipek or bdk")
            if len(bdk) != self.BDK_LEN:
                raise InvalidDUKPTArguments("BDK must have a length of %d" % self.BDK_LEN)
            self._bdk = BitArray(bytes=bdk)
        
    def derive_key(self, ipek, ksn):
        """Derive a unique key given the ipek and ksn

        Keyword arguments:
        ipek (BitArray) -- Initial Pin Encryption Key
        ksn (BitArray)  -- Key Serial Number
        """
        c_mask       = BitArray(hex='0xc0c0c0c000000000c0c0c0c000000000')
        ksn_offset   = 2
        ctr_offset   = -3
        right_offset = 8

        # Registers taken from documentation
        curkey = ipek
        ksnr   = BitArray(bytes=ksn.bytes[ksn_offset:])
        r3     = self.copy_counter(ksnr)
        r8     = self.reset_counter(ksnr.bytes)
        sr     = BitArray(hex='0x100000')
       
        while (sr.bytes[0] != '\x00') or (sr.bytes[1] != '\x00') or (sr.bytes[2] != '\x00'):
            tmp = self.copy_counter(sr)
            tmp = tmp & r3
            if (tmp.bytes[0] != '\x00') or (tmp.bytes[1] != '\x00') or (tmp.bytes[2] != '\x00'): 
                # Step 2
                n_ctr = BitArray(bytes=r8.bytes[ctr_offset:]) | sr
                r8    = BitArray(bytes=r8.bytes[:ctr_offset]+n_ctr.bytes)
                
                # Step 3
                r8a   = r8 ^ BitArray(bytes=curkey.bytes[right_offset:])
                
                # Step 4
                cipher = DES.new(curkey.bytes[:DES.key_size], DES.MODE_ECB)
                r8a    = BitArray(bytes=cipher.encrypt(r8a.bytes))
                
                # Step 5
                r8a = BitArray(bytes=curkey.bytes[right_offset:]) ^ r8a

                # Step 6
                curkey = curkey ^ c_mask
                
                # Step 7
                r8b = BitArray(bytes=curkey.bytes[right_offset:]) ^ r8
                
                # Step 8
                cipher = DES.new(curkey.bytes[:DES.key_size], DES.MODE_ECB)
                r8b    = BitArray(bytes=cipher.encrypt(r8b.bytes))
                
                # Step 9
                r8b = BitArray(bytes=curkey.bytes[right_offset:]) ^ r8b

                # Step 10 / 11
                curkey = BitArray(bytes=r8b.bytes+r8a.bytes)

            sr >>= 1
        self._cur_key = curkey
        return curkey

    def reset_counter(self, data):
        """Reset the counter to zero

        Keyword arguments:
        data (raw or BitArray) -- Must be at least 3 bytes
        
        Return:
        BitArray of the data passed in
        """
        if isinstance(data, BitArray):
            data = data.bytes
        if len(data) < 3:
            return None
        mask = BitArray(hex='0xe00000')
        ctr  = BitArray(bytes=data[len(data)-3:])
        return BitArray(bytes=data[:-3] + (mask & ctr).bytes)

    def copy_counter(self, data):
        """Copy only the counter bytes from a given string or BitArray

        Keyword arguments:
        data (raw or BitArray) -- Must be at least 3 bytes

        Return:
        BitArray of only the counter bytes
        """
        mask = BitArray(hex='0x1fffff')
        if len(data.bytes) > 3:
            ctr = BitArray(bytes=data.bytes[-3:])
        else:
            ctr = data

        return mask & ctr

    def increase_counter(self):
        """Increase the counter bytes of the stored ksn by one"""
        ctr = self._ksn.cut(21, start=59).next().int + 1
        self._ksn.overwrite('0b'+BitArray(int=ctr, length=21).bin, 59)
开发者ID:PaulSec,项目名称:DET,代码行数:104,代码来源:dukpt.py


注:本文中的bitstring.BitArray.cut方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。