def gdd_hamming_74_decompress(bases, deviations): reconstructed_bytes = [] for (idx, base) in enumerate(bases): # If the current base is a "pointer" to a full base, load it full_base = base if isinstance(base, list) else bases[base] # This deviation belongs to the current base dev = deviations[idx] # Cut off the last bit, which is the carry-over last bit of the original input byte carryover_bit = dev[3] dev = dev[0:3] # GDD decode = Hamming encode bitlist = hamming_encode(full_base) # Apply the syndrome to recover the original bitlist # (same process as the second half of Hamming decode) bitlist = hamming_fix_with_syndrome(bitlist, dev) # Append the carry-over bit bitlist.append(carryover_bit) #print("Reconstructed bitlist: %s" % bitlist) # Convert back from bitlist to byte current_byte = _bitlist_to_byte(bitlist) #print("Byte: %d" % reconstructed_byte) reconstructed_bytes.append(current_byte) # Done reconstructing the whole data return bytes(reconstructed_bytes)
def main() -> None: """Main function""" string = "Hello world" block_length = 16 assert not block_length % 8, "Block length must be a multiple of 8." print("Input string:", string) print("Block length:", block_length) encoded = hm.hamming_encode(string, block_length) print(f"Encoded string: {encoded}") decoded = hm.hamming_decode(encoded, block_length) print(f"Decoded string: {decoded}")
def gdd_hamming_1511_decompress(bases, deviations, is_padded): reconstructed_bytes = [] for (idx, base) in enumerate(bases): # If the current base is a "pointer" to a full base, load it full_base = base if isinstance(base, list) else bases[base] # This deviation belongs to the current base dev = deviations[idx] # Cut off the last bit, which is the carry-over last bit of the original input byte carryover_bit = dev[4] dev = dev[0:4] assert len(dev) == 4, "Deviation should be 4 bits, instead found {} bits".format(len(dev)) # GDD decode = Hamming encode bitlist = hamming_encode(full_base) assert len(bitlist) == 15 # Apply the syndrome to recover the original bitlist # (same process as the second half of Hamming decode) bitlist = hamming_fix_with_syndrome(bitlist, dev) # Append the carry-over bit bitlist.append(carryover_bit) assert len(bitlist) == 16 # Convert back from bitlist to byte byte1 = _bitlist_to_byte(bitlist[:8]) reconstructed_bytes.append(byte1) byte2 = _bitlist_to_byte(bitlist[8:]) reconstructed_bytes.append(byte2) # Done reconstructing the whole data if is_padded: # Cut off the last byte, which is a padding reconstructed_bytes = reconstructed_bytes[:-1] return bytes(reconstructed_bytes)
#print ('responsebit', responsebit) ''' responsebit = '0000000011111111' n = 4 splitresponse = [responsebit[i:i + n] for i in range(0, len(responsebit), n)] #intresponse = int (splitresponse,2) #byteresponse = int_to_bytes(intresponse) print('splitresonse', splitresponse) hammingsplit = [] i = 0 N = 63148583107154283585608284940392418734726981382069355868003882013090711003369 e = 65537 d = 51783437651169347215431900289402465246791119526563008636281618633074802696377 while (i < len(splitresponse)): hammingsplit.append(hamming_encode(splitresponse[i], 1)) i = i + 1 #print ("hammingsplit", hammingsplit) #P bit is leftmost hammingsplitint = int(''.join(hammingsplit), 2) #print ('hammingsplitint',hammingsplitint) sendoff = encrypt_rsa(hammingsplitint, N, e) try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print("Socket successfully created") except socket.error as err: print("socket creation failed with error %s" % (err)) #sending data to noise function s.connect((HOST, PORT)) s.send((sendoff)) print(' sendoff', bytes_to_bits(sendoff)) ready = select.select([s], [], [], 10)
from hamming import hamming_encode # a = hamming_encode([int(i) for i in '10011001010000001011011']) a = hamming_encode( [0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0]) print(a)
from huffman import HuffmanCoding from hamming import hamming_encode, hamming_correct from noise import Noisy from pathlib import Path from scipy.stats import entropy from entropy import calculateEntropy from correction import encode_hamming, detect_error path = "uncompressedFile" h = HuffmanCoding(path) output_path = h.compress() #encode('1compressed.bin') #hammtest #encode_hamming('1compressed.bin') #correction hamming_encode('1compressed.bin') #addnoise = Noisy('2encrypted.bin') #addnoise.volume_it_up('2encrypted.bin') #correct('2encrypted.bin') #detect_error('2encrypted.bin') hamming_correct('2encrypted.bin') #detect_error('3noise.bin') decompressed = h.decompress('4decrypted.bin') print("*** file info ***") print("BEFORE COMPRESSION : File length = " + str(Path(path).stat().st_size))