Best Python code snippet using localstack_python
_mac_wrapper_test.py
Source:_mac_wrapper_test.py
...29class MacWrapperTest(parameterized.TestCase):30 @parameterized.parameters([MAC_TEMPLATE,31 RAW_MAC_TEMPLATE,32 LEGACY_MAC_TEMPLATE])33 def test_compute_verify_mac(self, template):34 keyset_handle = tink.new_keyset_handle(template)35 primitive = keyset_handle.primitive(mac.Mac)36 tag = primitive.compute_mac(b'data')37 # No exception raised, no return value.38 self.assertIsNone(primitive.verify_mac(tag, b'data'))39 @parameterized.parameters([MAC_TEMPLATE,40 RAW_MAC_TEMPLATE,41 LEGACY_MAC_TEMPLATE])42 def test_verify_unknown_mac_fails(self, template):43 unknown_handle = tink.new_keyset_handle(template)44 unknown_primitive = unknown_handle.primitive(mac.Mac)45 unknown_tag = unknown_primitive.compute_mac(b'data')46 keyset_handle = tink.new_keyset_handle(template)47 primitive = keyset_handle.primitive(mac.Mac)48 with self.assertRaises(tink.TinkError):49 primitive.verify_mac(unknown_tag, b'data')50 @parameterized.parameters([MAC_TEMPLATE,51 RAW_MAC_TEMPLATE,52 LEGACY_MAC_TEMPLATE])53 def test_verify_short_mac_fails(self, template):54 keyset_handle = tink.new_keyset_handle(template)55 primitive = keyset_handle.primitive(mac.Mac)56 with self.assertRaises(tink.TinkError):57 primitive.verify_mac(b'', b'data')58 with self.assertRaises(tink.TinkError):59 primitive.verify_mac(b'tag', b'data')60 @parameterized.parameters(61 [(MAC_TEMPLATE, MAC_TEMPLATE),62 (MAC_TEMPLATE, RAW_MAC_TEMPLATE),63 (MAC_TEMPLATE, LEGACY_MAC_TEMPLATE),64 (RAW_MAC_TEMPLATE, MAC_TEMPLATE),65 (RAW_MAC_TEMPLATE, RAW_MAC_TEMPLATE),66 (RAW_MAC_TEMPLATE, LEGACY_MAC_TEMPLATE),67 (LEGACY_MAC_TEMPLATE, MAC_TEMPLATE),68 (LEGACY_MAC_TEMPLATE, RAW_MAC_TEMPLATE),69 (LEGACY_MAC_TEMPLATE, LEGACY_MAC_TEMPLATE)])70 def test_key_rotation(self, old_key_tmpl, new_key_tmpl):71 builder = keyset_builder.new_keyset_builder()72 older_key_id = builder.add_new_key(old_key_tmpl)73 builder.set_primary_key(older_key_id)74 mac1 = builder.keyset_handle().primitive(mac.Mac)75 newer_key_id = builder.add_new_key(new_key_tmpl)76 mac2 = builder.keyset_handle().primitive(mac.Mac)77 builder.set_primary_key(newer_key_id)78 mac3 = builder.keyset_handle().primitive(mac.Mac)79 builder.disable_key(older_key_id)80 mac4 = builder.keyset_handle().primitive(mac.Mac)81 self.assertNotEqual(older_key_id, newer_key_id)82 # 1 uses the older key. So 1, 2 and 3 can verify the mac, but not 4.83 mac_value1 = mac1.compute_mac(b'plaintext')84 mac1.verify_mac(mac_value1, b'plaintext')85 mac2.verify_mac(mac_value1, b'plaintext')86 mac3.verify_mac(mac_value1, b'plaintext')87 with self.assertRaises(tink.TinkError):88 mac4.verify_mac(mac_value1, b'plaintext')89 # 2 uses the older key. So 1, 2 and 3 can verify the mac, but not 4.90 mac_value2 = mac2.compute_mac(b'plaintext')91 mac1.verify_mac(mac_value2, b'plaintext')92 mac2.verify_mac(mac_value2, b'plaintext')93 mac3.verify_mac(mac_value2, b'plaintext')94 with self.assertRaises(tink.TinkError):95 mac4.verify_mac(mac_value2, b'plaintext')96 # 3 uses the newer key. So 2, 3 and 4 can verify the mac, but not 1.97 mac_value3 = mac3.compute_mac(b'plaintext')98 with self.assertRaises(tink.TinkError):99 mac1.verify_mac(mac_value3, b'plaintext')100 mac2.verify_mac(mac_value3, b'plaintext')101 mac3.verify_mac(mac_value3, b'plaintext')102 mac4.verify_mac(mac_value3, b'plaintext')103 # 4 uses the newer key. So 2, 3 and 4 can verify the mac, but not 1.104 mac_value4 = mac4.compute_mac(b'plaintext')105 with self.assertRaises(tink.TinkError):106 mac1.verify_mac(mac_value4, b'plaintext')107 mac2.verify_mac(mac_value4, b'plaintext')108 mac3.verify_mac(mac_value4, b'plaintext')109 mac4.verify_mac(mac_value4, b'plaintext')110if __name__ == '__main__':...
dec.py
Source:dec.py
...20 cipher = Cipher(algorithms.ChaCha20(key, nonce), mode=None)21 plaintext = cipher.decryptor().update(ciphertext)22 return plaintext23# Verify the validation of the mac to the text24def verify_mac(msg_bytes, mac_bytes, key):25 h = hmac.HMAC(key, hashes.SHA256())26 h.update(msg_bytes)27 h.verify(mac_bytes)28######## Decryption Schemes ########29# Encrypt Then MAC30def etm():31 data = rff("dados-etm.dat")32 mac = data["encrypted"][-32:]33 ciphertext = data["encrypted"][:-32]34 try:35 verify_mac(ciphertext, mac, HMAC_KEY)36 plaintext = dec_bytes(ciphertext, KEY, data["nonce"])37 print(plaintext.decode('utf-8'))38 except:39 print('Could not verify data integrity')40# Encrypt And MAC41def eam():42 data = rff("dados-eam.dat")43 mac = data["encrypted"][-32:]44 ciphertext = data["encrypted"][:-32]45 plaintext = dec_bytes(ciphertext, KEY, data["nonce"])46 try:47 verify_mac(plaintext, mac, HMAC_KEY)48 print(plaintext.decode('utf-8'))49 except:50 print('Could not verify data integrity')51# MAC Then Encrypt 52def mte():53 data = rff("dados-mte.dat")54 plaintext_mac = dec_bytes(data["encrypted"], KEY, data["nonce"])55 mac = plaintext_mac[-32:]56 plaintext = plaintext_mac[:-32]57 try:58 verify_mac(plaintext, mac, HMAC_KEY)59 print(plaintext.decode('utf-8'))60 except:61 print('Could not verify data integrity')62####################################63def main():64 if len(sys.argv) != 2:65 print("Please provide one of: eam, etm, mte")66 elif sys.argv[1] == "eam":67 eam()68 elif sys.argv[1] == "etm":69 etm()70 elif sys.argv[1] == "mte":71 mte()72 else:...
hack_mac_hash.py
Source:hack_mac_hash.py
1from time import time2def verify_mac(x, y, n):3 for i in range(n):4 if x[i] != y[i]:5 return False6 return True7MAC1 = '0123456789abcdef'8MAC2 = '01X3456789abcdef'9TRIALS = 10000010# пÑи каждом обÑаÑении к verify_mac() пÑоÑмаÑÑиваÑÑÑÑ Ð²Ñе воÑÐµÐ¼Ñ Ð±Ð°Ð¹Ñ11start = time()12for i in range(TRIALS):13 verify_mac(MAC1, MAC1, len(MAC1))14end = time()15print('%0.5f' % (end-start))16# пÑи каждом обÑаÑении к verify_mac() пÑоÑмаÑÑиваÑÑÑÑ ÑолÑко ÑÑи байÑа17start = time()18for i in range(TRIALS):19 verify_mac(MAC1, MAC2, len(MAC1))20end = time()...
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!!