def test_explicit(self): data = [ # little big little big ('', '', ''), ('0000', '0', '0'), ('0001', '8', '1'), ('1000', '1', '8'), ('1001', '9', '9'), ('0100', '2', '4'), ('0101', 'a', '5'), ('1100', '3', 'c'), ('1101', 'b', 'd'), ('0010', '4', '2'), ('0011', 'c', '3'), ('1010', '5', 'a'), ('1011', 'd', 'b'), ('0110', '6', '6'), ('0111', 'e', '7'), ('1110', '7', 'e'), ('1111', 'f', 'f'), ('10001100', '13', '8c'), ('100011001110', '137', '8ce'), ('1000110011101111', '137f', '8cef'), ('10100111110111001001', '5eb39', 'a7dc9'), ] for bs, hex_le, hex_be in data: a_be = bitarray(bs, 'big') a_le = bitarray(bs, 'little') self.assertEQUAL(hex2ba(hex_be, 'big'), a_be) self.assertEQUAL(hex2ba(hex_le, 'little'), a_le) self.assertEqual(ba2hex(a_be), hex_be) self.assertEqual(ba2hex(a_le), hex_le)
def test_explicit(self): for h, bs in [('', ''), ('0', '0000'), ('a', '1010'), ('f', '1111'), ('1a', '00011010'), ('2b', '00101011'), ('4c1', '010011000001'), ('a7d', '101001111101')]: a = bitarray(bs, 'big') self.assertEQUAL(hex2ba(h), a) self.assertEqual(ba2hex(a), h)
def test_binascii(self): a = urandom(800, 'big') s = binascii.hexlify(a.tobytes()).decode() self.assertEqual(ba2hex(a), s) b = bitarray(endian='big') b.frombytes(binascii.unhexlify(s)) self.assertEQUAL(hex2ba(s, 'big'), b)
def test_explicit(self): data = [ # little big ('', '', ''), ('1000', '1', '8'), ('1000 1100', '13', '8c'), ('1000 1100 1110', '137', '8ce'), ('1000 1100 1110 1111', '137f', '8cef'), ('1000 1100 1110 1111 0100', '137f2', '8cef4'), ] for bs, hex_le, hex_be in data: a_be = bitarray(bs, 'big') a_le = bitarray(bs, 'little') self.assertEQUAL(hex2ba(hex_be, 'big'), a_be) self.assertEQUAL(hex2ba(hex_le, 'little'), a_le) self.assertEqual(ba2hex(a_be), hex_be) self.assertEqual(ba2hex(a_le), hex_le)
def test_round_trip2(self): for a in self.randombitarrays(): if len(a) % 4 or a.endian() != 'big': self.assertRaises(ValueError, ba2hex, a) continue b = hex2ba(ba2hex(a)) self.assertEQUAL(b, a)
def test_hex(self): for i in range(1000): s = hex(i) self.assertEqual(s[:2], '0x') a = hex2ba(s[2:], 'big') self.assertEqual(ba2int(a), i) t = '0x%s' % ba2hex(a) self.assertEqual(t, s) self.assertEqual(eval(t), i)
def test_hexadecimal(self): a = base2ba(16, 'F61', 'big') self.assertEqual(a, bitarray('1111 0110 0001')) self.assertEqual(ba2base(16, a), 'f61') for n in range(50): s = ''.join(choice(hexdigits) for _ in range(n)) for endian in 'big', 'little': a = base2ba(16, s, endian) self.assertEQUAL(a, hex2ba(s, endian)) self.assertEqual(ba2base(16, a), ba2hex(a))
def test_round_trip(self): s = ''.join(choice(hexdigits) for _ in range(randint(20, 100))) for default_endian in 'big', 'little': _set_default_endian(default_endian) a = hex2ba(s) self.check_obj(a) self.assertEqual(len(a) % 4, 0) self.assertEqual(a.endian(), default_endian) t = ba2hex(a) self.assertEqual(t, s.lower()) b = hex2ba(t, default_endian) self.assertEQUAL(a, b)
def _get_ciphertext(self): block = self.right_sides[-1] + self.left_sides[-1] # block = self.left_sides[-1] + self.right_sides[-1] bits = self.permute(block, INVERSE_INITIAL_PERMUTATION) begin = 0 chars = [] for end in range(8, len(bits) + 1, 8): chars.append(ba2hex(bits[begin:end])) begin = end return ''.join(chars)
def test_ba2hex(self): self.assertEqual(ba2hex(bitarray(0, 'big')), '') self.assertEqual(ba2hex(bitarray('1110', 'big')), 'e') self.assertEqual(ba2hex(bitarray('1110', 'little')), '7') self.assertEqual(ba2hex(bitarray('00000001', 'big')), '01') self.assertEqual(ba2hex(bitarray('10000000', 'big')), '80') self.assertEqual(ba2hex(bitarray('00000001', 'little')), '08') self.assertEqual(ba2hex(bitarray('10000000', 'little')), '10') self.assertEqual(ba2hex(frozenbitarray('11000111', 'big')), 'c7') # length not multiple of 4 self.assertRaises(ValueError, ba2hex, bitarray('10')) self.assertRaises(TypeError, ba2hex, '101') c = ba2hex(bitarray('1101', 'big')) self.assertIsInstance(c, str) for n in range(7): a = bitarray(n * '1111', 'big') b = a.copy() self.assertEqual(ba2hex(a), n * 'f') # ensure original object wasn't altered self.assertEQUAL(a, b)
def test_round_trip(self): for i in range(100): s = ''.join(choice(hexdigits) for _ in range(randint(0, 1000))) for default_endian in 'big', 'little': _set_default_endian(default_endian) a = hex2ba(s) self.assertEqual(len(a) % 4, 0) self.assertEqual(a.endian(), default_endian) t = ba2hex(a) self.assertEqual(t, s.lower()) b = hex2ba(t, default_endian) self.assertEQUAL(a, b) # test simple encode / decode implementation self.assertEQUAL(a, self.hex2ba(t)) self.assertEqual(t, self.ba2hex(a))
def test_round_trip(self): for i in range(100): s = ''.join(choice(hexdigits) for _ in range(randint(0, 1000))) t = ba2hex(hex2ba(s)) self.assertEqual(t.decode(), s.lower())
for x in range(len(permuted_choice_1)): key_pc1[x] = bits[permuted_choice_1[x] - 1] c = key_pc1[:28] d = key_pc1[28:] c_shift_left = c[1:] + c[:1] d_shift_left = d[1:] + d[:1] whole = c_shift_left + d_shift_left key_pc2 = bitarray(48) key_pc2.setall(0) for x in range(len(permuted_choice_2)): key_pc2[x] = whole[permuted_choice_2[x] - 1] print("Round 1 Key: {}".format(util.ba2hex(key_pc2))) s1 = [[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7], [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8], [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0], [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13]] s2 = [[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10], [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5], [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15], [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9]] s3 = [[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8], [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1], [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7], [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12]] s4 = [[7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15], [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
p_addr = 0 data_len = 0 fuses_bin = bitarray() for line in f_in: fuses_match = FUSES_PATTERN.match(line) if fuses_match: addr, data = fuses_match.groups() c_addr = int(addr) d_addr = c_addr - p_addr - data_len p_addr = c_addr data_len = len(data) # empty fuse address if d_addr > 0: fuses_bin += d_addr * bitarray('1') fuses_bin += bitarray(data) # ignore PE & SIG del fuses_bin[PE[0]:PE[1]+1] del fuses_bin[SIG[0]:SIG[1]+1] # 32 bit padding padding = 32 - len(fuses_bin) % 32 fuses_bin += padding * bitarray('1') fuses_hex = ba2hex(fuses_bin) # write 8 hex per line for i in range(0, len(fuses_hex), 8): f_out.write(fuses_hex[i:i+8] + '\n')