Best Python code snippet using avocado_python
magma.py
Source:magma.py  
...37            bits = bin(ord(char))[2:]38            bits = '0' * (8 - len(bits)) + bits39            bitlist.extend(map(int, bits))40        return bitlist41    def bitlist_to_string(self, bitlist):42        chars = []43        for i in range(len(bitlist) // 8):44            byte = bitlist[i*8:(i+1)*8]45            byte_str = ''.join(map(str, byte))46            chars.append(chr(int(byte_str, 2)))47        return ''.join(chars)48    def prepare_data(self, data):49        if len(data) % BLOCK_SIZE != 0:50            data += PAD_SYMBOL * (BLOCK_SIZE - len(data) % BLOCK_SIZE)51        return data52    def prepare_result(self, result):53        return result[:-BLOCK_SIZE] + result[-BLOCK_SIZE:].rstrip(PAD_SYMBOL)54    def synchromail_crypt(self):55        iv = self.prepare_data(self.iv)56        iv = self.string_to_bitlist(iv)57        return self.ecb_crypt(iv, False)58    def generate_gamma(self, blocks_count):59        get_bin = lambda x: '{0:032b}'.format(x)60        C1 = 1684301261        C2 = 1684300962        sync = self.synchromail_crypt()63        N4 = sync[:32]64        N3 = sync[32:]65        for i in range(blocks_count):66            N3 = int(''.join(map(str, N3)), 2)67            N3 = (N3 + C2) % 2 ** 3268            N3 = list(map(int, list(get_bin(N3))))69            N4 = int(''.join(map(str, N4)), 2)70            N4 = (N4 + C1) % 2 ** 3271            N4 = list(map(int, list(get_bin(N4))))72            self.gamma.append(self.ecb_crypt(N3 + N4, False))73    def generate_keys(self):74        key = self.string_to_bitlist(self.key)75        keys = [key[i:i+32] for i in range(0, len(key), 32)]76        self.keys.extend(keys * 3)77        self.keys.extend(reversed(keys))78    def F(self, R, key):79        rotate = lambda l, n: l[n:] + l[:n]80        get_bin = lambda x: '{0:032b}'.format(x)81        A_ = []82        r = int(''.join(map(str, R)), 2)83        k = int(''.join(map(str, key)), 2)84        A = (r + k) % 2 ** 3285        A = list(map(int, list(get_bin(A))))86        A = [A[i:i+4] for i in range(0, len(A), 4)]87        for i in range(len(A)):88            a = int(''.join(map(str, A[i])), 2)89            a_ = SBOX[i][a]90            a_ = list(map(int, list(get_bin(a_))))91            A_.extend(a_)92        return rotate(A_, 11)93    def ecb_crypt(self, block, decrypt):94        L = block[:32]95        R = block[32:]96        i = 31 if decrypt else 097        i_adj = -1 if decrypt else 198        for _ in range(self.rounds_count):99            next_L = R[:]100            f = self.F(R, self.keys[i])101            next_R = list(map(lambda x, y: x ^ y, L, f))102            L = next_L103            R = next_R104            i += i_adj105        return R + L106    def crypt_block(self, block, decrypt, i):107        if self.mode == Modes.ECB:108            return self.ecb_crypt(block, decrypt)109        elif self.mode == Modes.CBC:110            if decrypt:111                result = self.ecb_crypt(block, decrypt)112                if i == 0:113                    iv = self.prepare_data(self.iv)114                    iv = self.string_to_bitlist(iv)115                    result = list(map(lambda x, y: x ^ y, iv, result))116                else:117                    result = list(map(lambda x, y: x ^ y, self.last_result, result))118                self.last_result = block119            else:120                if i == 0:121                    iv = self.prepare_data(self.iv)122                    iv = self.string_to_bitlist(iv)123                    block = list(map(lambda x, y: x ^ y, block, iv))124                else:125                    block = list(map(lambda x, y: x ^ y, block, self.last_result))126                result = self.ecb_crypt(block, decrypt)127                self.last_result = result[:]128            return result129            130        elif self.mode == Modes.OFB:131            return list(map(lambda x, y: x ^ y, block, self.gamma[i]))132        elif self.mode == Modes.CFB:133            result = list(map(lambda x, y: x ^ y, block, self.gamma[i]))134            if decrypt:135                self.gamma.append(block)136            else:137                self.gamma.append(result)138            return result139    def crypt(self, data, decrypt=False):140        blocks = [data[i:i+BLOCK_SIZE] for i in range(0, len(data), BLOCK_SIZE)]141        crypted_data = []142        if self.mode == Modes.OFB:143            self.generate_gamma(len(blocks))144        elif self.mode == Modes.CFB:145            self.gamma = [self.synchromail_crypt()]146        for i in range(len(blocks)):147            block = self.string_to_bitlist(blocks[i])148            crypted_block = self.crypt_block(block, decrypt, i)149            crypted_data.append(self.bitlist_to_string(crypted_block))150        return ''.join(crypted_data)151    def encrypt(self, data):152        data = self.prepare_data(data)153        return self.crypt(data)154    def decrypt(self, data):155        result = self.crypt(data, decrypt=True)...inputgen.py
Source:inputgen.py  
1def String_to_BitList(data):2    """Turn the string data, into a list of bits (1, 0)'s"""3    l = len(data) * 84    result = [0] * l5    pos = 06    for ch in data:7        i = 78        while i >= 0:9            if ch & (1 << i) != 0:10                result[pos] = 111            else:12                result[pos] = 013            pos += 114            i -= 115    return result16def BitList_to_String(data):17    """Turn the list of bits -> data, into a string"""18    result = []19    pos = 020    c = 021    while pos < len(data):22        c += data[pos] << (7 - (pos % 8))23        if (pos % 8) == 7:24            result.append(c)25            c = 026        pos += 127    28    return bytes(result)29plaintext = b"besthero" 30strbit = String_to_BitList(plaintext)31plaintexts_list = [plaintext] # 5 plaintext / keys with 1 hamming distance differnce wrt plaintext (str)32for i in range(5):33    temp = strbit34    temp[i] = 1 - temp[i] # changing 1 bit in the base PT35    plaintexts_list.append(BitList_to_String(temp))36strbit = String_to_BitList(plaintext)37plaintexts_HD_list = [plaintext,] # 6 plaintext with hamming distance differnce from 0 to 5 wrt plaintext (str, bit list)38for i in range(1,6):39    temp = strbit40    for j in range(i):41        temp[j] = 1 - temp[j]  # changing 1 bit in the base PT...useful_bit_string_funcs.py
Source:useful_bit_string_funcs.py  
...7    bytes = zip(*(bi,) * 8)8    shifts = (7, 6, 5, 4, 3, 2, 1, 0)9    for byte in bytes:10        yield chr(sum(bit << s for bit, s in zip(byte, shifts)))11def bitlist_to_string(bl):12    return ''.join(bitlist_to_chars(bl))13def string_to_bitstring(s):14    l = string_to_bitlist(s)15    return ''.join([str(bit) for bit in l])...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
