Пример #1
0
 def testRor(self):
     a = BitArray('0b111000')
     a.ror(1)
     self.assertEqual(a, '0b011100')
     a = BitArray('0b111000')
     a.ror(1, start=2, end=6)
     self.assertEqual(a, '0b011100')
Пример #2
0
def encode(data, padding_at_begin=True):
    """
    bytes data to base32 encoded string
    """
    if not isinstance(data, (bytes, bytearray)):
        data = bytes(data, 'utf-8')

    bit_array = BitArray()
    for b in bytes(data):
        bit_array.append(BitArray(uint=b, length=8))

    # padding: round bit_array length to multiple of 5 bits
    penny = len(bit_array) % 5
    if penny > 0:
        bit_array.append(BitArray(uint=0, length=(5-penny)))
        if padding_at_begin:
            bit_array.ror(5-penny)

    result = ''
    a, b = 0, 5
    while a < len(bit_array):
        b32_char = bit_array[a:b]
        result += ALPTHABET[b32_char.int]
        a, b = a+5, b+5

    return result
def AHash(k, p, bstream):
    block_size = int((k - 1) * math.floor(math.log(p, 2)))
    n_block = int(math.ceil(bstream.len / float(block_size)))
    message_size = block_size * n_block
    b0 = message_size

    #reset bitstream position
    bstream.pos = 0
    for i in range(1, n_block + 1):
        try:
            b0 = roundHash(k, p, BitStream(bstream.read(block_size)), b0)
        except:
            padding_size = block_size - bstream.len % block_size
            b0 = roundHash(
                k, p,
                BitStream("0b" + bstream.read(bstream.len - bstream.pos).bin +
                          "0" * padding_size), b0)

        if (i != n_block):
            tmp_b0 = BitArray("uint:" + str(int(math.ceil(math.log(p, 2)))) +
                              "=" + str(b0))
            tmp_b0.ror(int(math.ceil(math.log(p, 2)) // 2))
            b0 = tmp_b0.uint

    return b0
Пример #4
0
    def speed_and_direction_packet(address,
                                   speed,
                                   speed_steps,
                                   direction,
                                   headlight=False):
        """
        Build a speed and direction packet.

        param address int is the dcc device address.
        param speed   int is the speed. Depending on the
                      speed steps we make a baseline packet
                      or a 128-bit packet
        param direction int is 1 for forward and 0 for backwards.
        """
        address_bin = BitArray('uint:8=%d' % address)

        if speed_steps == 128:
            # Build a 2 byte advanced operation instruction
            instruction_bin1 = BitArray('0b00111111')
            instruction_bin2 = BitArray()
            if direction:
                instruction_bin2.append('0b1')
            else:
                instruction_bin2.append('0b0')
            speed_bin = BitArray('uint:7=%d' % speed)
            instruction_bin2.append(speed_bin)

            error = address_bin ^ instruction_bin1 ^ instruction_bin2
            data = [instruction_bin1, instruction_bin2, error]

        else:
            # Build a 1 byte direction and speed baseline packet
            instruction_bin = BitArray('0b01')
            if direction:
                instruction_bin.append('0b1')
            else:
                instruction_bin.append('0b0')
            if speed_steps == 14:
                if headlight:
                    instruction_bin.append('0b1')
                else:
                    instruction_bin.append('0b0')
                speed_bin = BitArray('uint:4=%d' % speed)
            else:
                speed_bin = BitArray('uint:5=%d' % speed)
                speed_bin.ror(1)

            instruction_bin.append(speed_bin)

            error = address_bin ^ instruction_bin
            data = [instruction_bin, error]

        return DCCGeneralPacket(address_bin, data)
Пример #5
0
    def speed_and_direction_packet(address,
                                   speed,
                                   speed_steps,
                                   direction,
                                   headlight=False):
        """
        Build a speed and direction packet.

        param address int is the dcc device address.
        param speed   int is the speed. Depending on the
                      speed steps we make a baseline packet
                      or a 128-bit packet
        param direction int is 1 for forward and 0 for backwards.
        """
        address_bin = BitArray('uint:8=%d' % address)

        if speed_steps == 128:
            # Build a 2 byte advanced operation instruction
            instruction_bin1 = BitArray('0b00111111')
            instruction_bin2 = BitArray()
            if direction:
                instruction_bin2.append('0b1')
            else:
                instruction_bin2.append('0b0')
            speed_bin = BitArray('uint:7=%d' % speed)
            instruction_bin2.append(speed_bin)

            error = address_bin ^ instruction_bin1 ^ instruction_bin2
            data = [instruction_bin1, instruction_bin2, error]

        else:
            # Build a 1 byte direction and speed baseline packet
            instruction_bin = BitArray('0b01')
            if direction:
                instruction_bin.append('0b1')
            else:
                instruction_bin.append('0b0')
            if speed_steps == 14:
                if headlight:
                    instruction_bin.append('0b1')
                else:
                    instruction_bin.append('0b0')
                speed_bin = BitArray('uint:4=%d' % speed)
            else:
                speed_bin = BitArray('uint:5=%d' % speed)
                speed_bin.ror(1)

            instruction_bin.append(speed_bin)

            error = address_bin ^ instruction_bin
            data = [instruction_bin, error]

        return DCCGeneralPacket(address_bin, data)
def joshes_try_scrambler():
    """
    An alternative attempt of mine to try and more strictly interpret the
    standard, but it doesn't produce sequences that are at all close to that
    produced by other third parties nor reference implementations
    """
    # start value has least significant bit set to 1
    register = BitArray(uint=0b0000000000000001, length=16)
    while True:
        # Bit clock = XOR least two significant bits
        register[0] = BitArray(bool=(register[14] ^ register[15]))
        yield register.uint
        # shift feedback register right
        register.ror(1)
Пример #7
0
    def lowerSigmaOne(self, xInput):

        termOne = BitArray(xInput)
        termTwo = BitArray(xInput)
        termThree = xInput >> 10

        termOne.ror(17)
        termTwo.ror(19)

        #print(termOne.bin)
        #print(termTwo.bin)
        #print(termThree.bin)

        output = termOne ^ termTwo ^ termThree

        #print(output.bin)

        return output
Пример #8
0
def rrs(ba):
    if isinstance(ba, str):
        ba = BitArray(bytes(ba, "utf-8"))

    out = []
    for _ in range(ba.length):
        try:
            out.append(ba.bytes.decode('utf-8'))
        except UnicodeDecodeError:
            pass
        ba.ror(1)
    ba.reverse()
    for _ in range(ba.length):
        try:
            out.append(ba.bytes.decode('utf-8'))
        except UnicodeDecodeError:
            pass
        ba.ror(1)
    ba.reverse()

    return out
def AHash(k,p,bstream):
    block_size = int((k-1)*math.floor(math.log(p,2)))
    n_block = int(math.ceil(bstream.len/float(block_size)))
    message_size = block_size * n_block
    b0 = message_size

    #reset bitstream position
    bstream.pos = 0
    for i in range(1,n_block+1):
        try:
            b0 = roundHash(k, p, BitStream(bstream.read(block_size)), b0)
        except:
            padding_size = block_size - bstream.len%block_size
            b0 = roundHash(k, p,
                BitStream("0b" + bstream.read(bstream.len - bstream.pos).bin + "0" * padding_size),
                b0)

        if(i != n_block):
            tmp_b0 = BitArray("uint:" + str(int(math.ceil(math.log(p,2)))) + "=" + str(b0))
            tmp_b0.ror(int(math.ceil(math.log(p,2))//2))
            b0 = tmp_b0.uint

    return b0
Пример #10
0
    def upperSigmaOne(self, xInput):

        xor1 = BitArray(xInput)
        xor2 = BitArray(xInput)
        xor3 = BitArray(xInput)

        xor1.ror(6)
        xor2.ror(11)
        xor3.ror(25)

        output = xor1 ^ xor2 ^ xor3

        return output
Пример #11
0
    def upperSigmaZero(self, xInput):

        xor1 = BitArray(xInput)
        xor2 = BitArray(xInput)
        xor3 = BitArray(xInput)

        xor1.ror(2)
        xor2.ror(13)
        xor3.ror(22)

        output = xor1 ^ xor2 ^ xor3

        return output
 def testRor(self):
     s = BitArray("0b1000")
     s.ror(1)
     self.assertEqual(s, "0b0100")
Пример #13
0
 def testRor(self):
     s = BitArray('0b1000')
     s.ror(1)
     self.assertEqual(s, '0b0100')
Пример #14
0
def circular_permutation_right(m, R):  # Tested and works
    ror_m = BitArray(m)
    ror_m.ror(R)
    return (bytearray(ror_m.bytes))
Пример #15
0
 def testRor(self):
     s = BitArray('0b1000')
     s.ror(1)
     self.assertEqual(s, '0b0100')