代码介绍
以循环右移为例子 总共就分为下类三个函数:
rorCustomByte 自定义定制字节移位
rorCustomBit 自定义Bit移位
ror8bit 默认按照8位数值 进行移位
代码实现
__author__ = 'IBinary blob https://www.cnblogs.com/ibinary/'
class RorObj():
def __init__(self):
pass
#字节循环移位
#参数1 要移动的值
#参数2 值是多少个字节
#参数3 要移动的字节位数
def rorCustomByte(self,v, Byte, shiftByte):
shiftByte = (shiftByte * 4) & (Byte * 4 - 1) # 按照bit值 来进行设置移动位数
if shiftByte == 0:
return v
a1 = (v >> (shiftByte)) # 右移shift位 空出高位shift位
a2 = (v << ((Byte * 4) - shiftByte)) # 计算出剩下要移动的位数
_ = '0x' + 'F' * Byte
FfValue = int(_, 16)
value = (a2 | a1) & FfValue
return value
# 循环右移n位
# 参数1 要移动的值
# 参数2 你的这个是是多少位的. 比如 0x12345678 就是32位
# 参数3 你要移动多少位 比如 4位 那么移动之后就是 0x81234567
def rorCustomBit(self, v, Bytebit, shift):
shift &= (Bytebit - 1) # 按照bit值 来进行设置移动位数
if shift == 0:
return v
a1 = (v >> shift) # 右移shift位 空出高位shift位
a2 = (v << ((Bytebit) - shift)) # 计算出剩下要移动的位数
l = [x for x in range(4, Bytebit + 1) if x % 4 == 0]
LCount = len(l)
_ = '0x' + 'F' * LCount
FfValue = int(_, 16)
value = (a2 | a1) & FfValue
return value
def ror4Bit(self, v, shift):
shift &= 3
if shift == 0:
return v
return ((v >> shift) | (v << (4 - shift))) & 0xF
def ror8Bit(self,v, shift):
shift &= 7
if shift == 0:
return v
return ((v >> shift) | (v << (8 - shift))) & 0xFF
def ror12Bit(self,v, shift):
shift &= 11
if shift == 0:
return v
return ((v >> shift) | (v << (12 - shift))) & 0xFFF
def ror16Bit(self,v, shift):
shift &= 15
if shift == 0:
return v # value 右移shift位
return ((v >> shift) | (v << (16 - shift))) & 0xFFFF
def ror20Bit(self,v, shift):
if shift == 0:
return v # value 右移shift位
return ((v >> shift) | (v << (20 - shift))) & 0xFFFFF
def ror24Bit(self,v, shift):
shift &= 23
if shift == 0:
return v # value 右移shift位
return ((v >> shift) | (v << (24 - shift))) & 0xFFFFFF
def ror28Bit(self,v, shift):
if shift == 0:
return v # value 右移shift位
return ((v >> shift) | (v << (28 - shift))) & 0xFFFFFFF
def ror32Bit(self,v, shift):
shift &= 0x1F # 设置要移动的位数
if shift == 0:
return v # value 右移shift位
return ((v >> shift) | (v << (32 - shift))) & 0xFFFFFFFF
class RolObl():
def __init__(self):
pass
# 循环左移原理
# 拿一个32位的数(4个字节)来说 进行移动八位
# 如:
# 0x12345678 rol 8 之后 = 0x34567812
# 其原理如下:
# 1.首先左移八位得到 0x345678
# 2.然后右移24位得到 0x12
# 最后 0x345678 | 0x12 = 0x34567812
# 鉴于Python的特殊性.我们只需要32bit数即可. 也就是最后要 & 0xFFFFFFFF
# 其它移位同理
def rolCustomBit(self,v,bit,shift):
shift &= (bit-1)
if shift == 0:
return v
HightBit = v >> (bit - shift)
LowBit = v << shift
l = [x for x in range(4, bit + 1) if x % 4 == 0]
LCount = len(l)
_ = '0x' + 'F' * LCount
FfValue = int(_, 16)
Value = (HightBit | LowBit) & FfValue
return Value
#按照字节移位
def rolCustomByte(self,v,Byte,shiftByte):
shiftByte = (shiftByte * 4) & (Byte * 4 - 1) # 按照bit值 来进行设置移动位数
if shiftByte == 0:
return v
Low = (v << (shiftByte)) #左移shift位
Hight = (v >> ((Byte * 4) - shiftByte)) # 计算出剩下要移动的位数
_ = '0x' + 'F' * Byte
FfValue = int(_, 16)
value = (Hight | Low) & FfValue
return value
def rol4Bit(self,v,shift):
shift &= 3
if shift == 0:
return v
HightBit = v >> (4 - shift)
LowBit = v << shift
Value = (HightBit | LowBit) & 0xF
return Value
def rol8Bit(self,v,shift):
shift &= 7
if shift == 0:
return v
HightBit = v >> (8 - shift)
LowBit = v << shift
Value = (HightBit | LowBit) & 0xFF
return Value
def rol12Bit(self, v, shift):
shift &= 11
if shift == 0:
return v
HightBit = v >> (12 - shift)
LowBit = v << shift
Value = (HightBit | LowBit) & 0xFFF
return Value
def rol16Bit(self, v, shift):
shift &= 15
if shift == 0:
return v
HightBit = v >> (16 - shift)
LowBit = v << shift
Value = (HightBit | LowBit) & 0xFFFF
return Value
def rol20Bit(self, v, shift):
if shift == 0:
return v
HightBit = v >> (20 - shift)
LowBit = v << shift
Value = (HightBit | LowBit) & 0xFFFFF
return Value
def rol24Bit(self, v, shift):
shift &= 23
if shift == 0:
return v
HightBit = v >> (24 - shift)
LowBit = v << shift
Value = (HightBit | LowBit) & 0xFFFFFF
return Value
def rol28Bit(self, v, shift):
if shift == 0:
return v
HightBit = v >> (28 - shift)
LowBit = v << shift
Value = (HightBit | LowBit) & 0xFFFFFFF
return Value
def rol32bit(self,v,shift):
shift &= 0x1F
if shift == 0:
return v
HightBit = v >> (32 - shift)
LowBit = v << shift
value = (HightBit | LowBit) & 0xFFFFFFFF
return value
a=RolObl()
print(hex(RolObl.rol32bit(RolObl(),0xB696B936,13)))
print(hex(RolObl.rolCustomBit(RolObl(),0xB696B936,32,13)))
print(hex(a.rolCustomBit(0xB696B936,32,13)))
介绍一下self类的调用
Python中定义类的方式如下:
class 类名([父类名[,父类名[,...]]]):
pass
省略父类名表示该类直接继承自object,类的实例化如下:
class Person():
def __init__(self):
pass
person=Person()
init()是类的构造函数,在实例化时调用,它的参数self表示类实例。
参考https://www.jianshu.com/p/8922a77151ee