Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- def from_bytes(message):
- for byte in map(ord, message):
- for _ in range(8):
- yield (byte & 128) >> 7
- byte <<= 1
- while True:
- yield 0
- def make_splitter(amount, message):
- def get():
- total = 0
- for _ in range(amount):
- total <<= 1
- total += next(message)
- return total
- return get
- def encode(message, image, bits):
- not_bits = ~((1 << bits) - 1)
- get = make_splitter(bits, from_bytes(message))
- return (
- (byte & not_bits) + get()
- for byte in image
- )
- def alt_from_bytes(message, bits):
- bit = 0
- total = 0
- for byte in map(ord, message):
- for _ in range(8):
- bit += 1
- total <<= 1
- total += (byte & 128) >> 7
- if bit == bits:
- yield total
- bit = 0
- total = 0
- byte <<= 1
- while True:
- yield 0
- def alt_encode(message, image, bits):
- NOT_BITS = ~((1 << bits) - 1)
- return (
- (byte & NOT_BITS) + data
- for byte, data in zip(image, alt_from_bytes(message, bits))
- #for byte, data in itertools.izip(image, alt_from_bytes(message, bits)) Python2
- )
- def get_chunk(image, bits):
- bits = (1 << bits) - 1
- for byte in image:
- yield byte & bits
- # From chunk to bits.
- def to_bits(amount, chunks):
- AND = 1 << (amount - 1)
- SHIFT = amount - 1
- for chunk in chunks:
- for _ in range(amount):
- yield (chunk & AND) >> SHIFT
- chunk <<= 1
- # From bit to byte
- def to_bytes(bits):
- index = 0
- total = 0
- for bit in bits:
- index += 1
- total <<= 1
- total += bit
- if index == 8:
- yield total
- index = 0
- total = 0
- # Decode an `image`:list
- def decode(image, bits):
- return to_bytes(to_bits(bits, get_chunk(image, bits)))
- def alt_decode(image, bits):
- AND = 1 << (bits - 1)
- SHIFT = bits - 1
- AND_BITS = (1 << bits) - 1
- index = 0
- total = 0
- for byte in image:
- chunk = byte & AND_BITS
- for _ in range(bits):
- bit = (chunk & AND) >> SHIFT
- index += 1
- total <<= 1
- total += bit
- if index == 8:
- yield total
- index = 0
- total = 0
- chunk <<= 1
- def zero_padding(data, bits):
- for i in data:
- yield i
- padding = '0' * bits
- while True:
- yield padding
- def encode_original(message, image, bits):
- joined_binary_data = ''.join(
- str(bin(x))[2:].zfill(8)
- for x in map(ord, message)
- )
- split_binary_data = zero_padding(
- (
- joined_binary_data[i:i + bits]
- for i in range(0, len(joined_binary_data), bits)
- ),
- bits
- )
- reduced_image = [
- str(bin(i))[2:].zfill(8)[:-bits]
- for i in image
- ]
- return (
- int(reduced_image[i] + next(split_binary_data), 2)
- for i in range(len(reduced_image))
- )
- def decode_original(image, bits):
- image_data_parts = [
- str(bin(i))[2:].zfill(8)[8 - bits:]
- for i in image
- ]
- image_data_binary = ''.join(image_data_parts)
- image_data_split = [
- image_data_binary[i:i + 8]
- for i in range(0, len(image_data_binary), 8)
- ]
- return (
- int(i, 2)
- for i in image_data_split
- )
- bits = 4
- print(list(alt_encode('Hi', [255, 0, 255, 0, 255, 0, 255, 0], bits)))
- print(''.join(map(chr, decode(alt_encode('Hi', [255, 0, 255, 0, 255, 0, 255, 0], bits), bits))))
- # Hi
- import time
- def time_it_maker(input_, iterations=1000):
- def time_it(fn, name):
- start = time.time()
- for _ in range(iterations):
- fn(*input_)
- stop = time.time()
- print(name, '=', stop - start)
- return time_it
- def list_(fn):
- def inner(*args, **kwargs):
- return list(fn(*args, **kwargs))
- return inner
- def first(fn):
- def inner(*args, **kwargs):
- return next(fn(*args, **kwargs))
- return inner
- def loop(fn):
- def inner(*args, **kwargs):
- for _ in fn(*args, **kwargs):
- pass
- return inner
- def char(fn):
- def inner(*args, **kwargs):
- return ''.join(map(chr, fn(*args, **kwargs)))
- return inner
- def time_encode(time_it):
- #time_it(encode_original, 'encode_original')
- #time_it(encode, 'encode')
- #time_it(alt_encode, 'alt_encode')
- #time_it(first(encode_original), 'next(encode_original)')
- #time_it(first(encode), 'next(encode)')
- #time_it(first(alt_encode), 'next(alt_encode)')
- time_it(list_(encode_original), 'list(encode_original)')
- time_it(list_(encode), 'list(encode)')
- time_it(list_(alt_encode), 'list(alt_encode)')
- time_it(loop(encode_original), 'loop(encode_original)')
- time_it(loop(encode), 'loop(encode)')
- time_it(loop(alt_encode), 'loop(alt_encode)')
- def time_decode(time_it):
- #time_it(decode_original, 'decode_original')
- #time_it(decode, 'decode')
- #time_it(alt_decode, 'alt_decode')
- #time_it(first(decode_original), 'next(decode_original)')
- #time_it(first(decode), 'next(decode)')
- #time_it(first(alt_decode), 'next(alt_decode)')
- time_it(char(decode_original), 'chr(decode_original)')
- time_it(char(decode), 'chr(decode)')
- time_it(char(alt_decode), 'chr(alt_decode)')
- time_encode(time_it_maker(['Hi', [255, 0, 255, 0, 255, 0, 255, 0], bits], iterations=1000000))
- print('\n')
- decode_input = list(encode('Hi', [255, 0, 255, 0, 255, 0, 255, 0], bits))
- time_decode(time_it_maker([decode_input, bits], iterations=1000000))
- print('\nLong\n')
- time_encode(time_it_maker(['a'*50000, list(itertools.islice(itertools.cycle([255, 0]), 50000)), bits], 1000))
- print('\n')
- decode_input = list(encode('a'*50000, itertools.islice(itertools.cycle([255, 0]), 50000), bits))
- time_decode(time_it_maker([decode_input, bits], iterations=1000))
- """
- list(encode_original) = 40.6009998322
- list(encode) = 43.9160001278
- list(alt_encode) = 25.635999918
- loop(encode_original) = 41.4110000134
- loop(encode) = 39.7620000839
- loop(alt_encode) = 20.6430001259
- chr(decode_original) = 22.8190000057
- chr(decode) = 35.7300000191
- chr(alt_decode) = 31.3589999676
- Long
- list(encode_original) = 196.507999897
- list(encode) = 230.309000015
- list(alt_encode) = 156.924999952
- loop(encode_original) = 194.916000128
- loop(encode) = 231.868999958
- loop(alt_encode) = 163.7900002
- chr(decode_original) = 92.3320000172
- chr(decode) = 188.205000162
- chr(alt_decode) = 172.137000084
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement