Best Python code snippet using avocado_python
vinegere.py
Source:vinegere.py
...72 b3 = bytes_str[i*2:i*3+1]73 b4 = bytes_str[i*3:i*4+1]74 # Average the distances from the 4 blocks of bytes75 distances.append(76 (compute_hamming_distance(b1, b2) / i) +77 (compute_hamming_distance(b1, b3) / i) +78 (compute_hamming_distance(b1, b4) / i) +79 (compute_hamming_distance(b2, b3) / i) +80 (compute_hamming_distance(b2, b4) / i) +81 (compute_hamming_distance(b3, b4) / i) / 682 )83 key_sizes.append(i)84 return key_sizes[distances.index(min(distances))]85def compute_hamming_distance(b1, b2):86 distance_result_str = bytes_to_long(b1) ^ bytes_to_long(b2)87 binary_rep = bin(distance_result_str)[2:]88 distance = 089 for char in binary_rep:90 if char == '1':91 distance += 192 return distance93def detect_single_byte_xor(cipher_strings):94 results_list = list()95 score_list = list()96 for cipher_str in cipher_strings:97 result, key, score = single_byte_xor_solver(cipher_str)98 results_list.append(result)99 score_list.append(score)...
c16.py
Source:c16.py
1import base642import math3import c124import c135def compute_hamming_distance(b1: bytes, b2: bytes) -> int:6 """Return the number of differing bits"""7 assert len(b1) == len(b2)8 dist = 09 for i, j in zip(b1, b2):10 x = i ^ j11 dist += format(x, 'b').count('1')12 return dist13def break_repeating_key_xor(cipher: bytes) -> tuple:14 # Guess the key size in bytes15 ham_avg = []16 max_key_size = 4017 for ks in range(2, max_key_size + 1):18 # Try with 4 KEYSIZE blocks give us better avg Hamming distance then 2 KEYSIZE blocks19 dist = compute_hamming_distance(cipher[:2*ks], cipher[2*ks:4 * ks])20 ham_avg.append({'ks': ks, 'ham': dist / (2*ks)})21 ham_avg.sort(key=lambda h1: h1['ham'])22 # Try 3 keysize with smallest avg Hamming distance23 max_try = 424 plain_bytes = None25 key_bytes = None26 min_score = math.inf27 for d in ham_avg[:max_try]:28 print(f'Try key size: {d["ks"]}, Avg Hamming Distance: {d["ham"]}')29 keysize = d['ks']30 num_repeat = len(cipher) // keysize31 num_remain = len(cipher) % keysize32 # Split cipher into list of separated single-byte XOR33 single_byte_xor = []34 for i in range(0, keysize):35 cipher_i = [cipher[i + j * keysize] for j in range(0, num_repeat)]36 # Check out of index37 if num_remain and i < num_remain:38 cipher_i.append(cipher[i + num_repeat * keysize])39 single_byte_xor.append(bytes(cipher_i))40 # print(len(cipher_i), single_byte_xor[i].hex())41 single_xor_msg = []42 for i in range(0, keysize):43 decrypt_msg, score = c13.break_single_byte_xor(single_byte_xor[i])44 if decrypt_msg:45 single_xor_msg.append(decrypt_msg)46 if len(single_xor_msg) == keysize:47 # Merge keysize strings into one string48 print('Found a message')49 msg = ''50 for m in zip(*single_xor_msg):51 msg += ''.join(m)52 # Handle the remain characters53 remain = ''.join(m[len(m) - 1] for m in single_xor_msg if len(m) > num_repeat)54 msg += remain55 guess_key = c12.xor_bytes(cipher[:keysize], msg[:keysize].encode())56 if c13.compute_score(msg) < min_score:57 print('Update the new message as best guess message')58 plain_bytes = msg.encode()59 key_bytes = guess_key60 return plain_bytes, key_bytes61if __name__ == '__main__':62 s1 = 'this is a test'63 s2 = 'wokka wokka!!!'64 result = compute_hamming_distance(s1.encode(), s2.encode())65 assert result == 3766 test = compute_hamming_distance(b'B', b'A')67 assert test == 268 with open('6.txt', 'r') as f:69 data = f.read().replace('\n', '')70 cipher = base64.b64decode(data)71 plain, key = break_repeating_key_xor(cipher)72 print(f'Decrypted Message:\n{plain.decode()}')...
SkewArray.py
Source:SkewArray.py
...19 return positions20#We say that position i in k-mers p and q is a mismatch if the symbols at position i21# of the two strings are not the same. The total number of mismatches between strings p and q22# is called the Hamming distance between these strings. 23def compute_hamming_distance(p,q):24 num_of_mismatches = 025 for i in range(len(p)):26 if p[i] != q[i]:27 num_of_mismatches += 128 return num_of_mismatches29#We say that a k-mer Pattern appears as a substring of Text with at most d mismatches 30# f there is some k-mer substring Pattern' of Text having d or fewer mismatches with Pattern; 31# that is, HammingDistance(Pattern, Pattern') ⤠d. Our observation that a DnaA box may appear 32# with slight variations leads to the following generalization of the Pattern Matching Problem.33def compute_approximate_pattern_matching(Text, Pattern, d):34 positions = [] #Initializing list of positions35 for i in range(len(Text) - len(Pattern) + 1):36 if compute_hamming_distance(Text[i:i+len(Pattern)], Pattern) <= d:37 positions.append(i)38 return positions39#This function computes the number of occurrences of Pattern in Text with at most d mismatches.40def compute_approximate_pattern_count(Text, Pattern, d):41 count = 042 for i in range(len(Text) - len(Pattern) + 1):43 if compute_hamming_distance(Text[i:i+len(Pattern)], Pattern) <= d:44 count += 145 return count46text = 'AGCGTGCCGAAATATGCCGCCAGACCTGCTGCGGTGGCCTCGCCGACTTCACGGATGCCAAGTGCATAGAGGAAGCGAGCAAAGGTGGTTTCTTTCGCTTTATCCAGCGCGTTAACCACGTTCTGTGCCGACTTT'...
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!!