Advertisement
Guest User

Steganography performance

a guest
Oct 2nd, 2015
113
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import itertools
  2.  
  3. def from_bytes(message):
  4.     for byte in map(ord, message):
  5.         for _ in range(8):
  6.             yield (byte & 128) >> 7
  7.             byte <<= 1
  8.     while True:
  9.         yield 0
  10.  
  11. def make_splitter(amount, message):
  12.     def get():
  13.         total = 0
  14.         for _ in range(amount):
  15.             total <<= 1
  16.             total += next(message)
  17.         return total
  18.     return get
  19.  
  20. def encode(message, image, bits):
  21.     not_bits = ~((1 << bits) - 1)
  22.     get = make_splitter(bits, from_bytes(message))
  23.     return (
  24.         (byte & not_bits) + get()
  25.         for byte in image
  26.     )
  27.  
  28. def alt_from_bytes(message, bits):
  29.     bit = 0
  30.     total = 0
  31.     for byte in map(ord, message):
  32.         for _ in range(8):
  33.             bit += 1
  34.             total <<= 1
  35.             total += (byte & 128) >> 7
  36.             if bit == bits:
  37.                 yield total
  38.                 bit = 0
  39.                 total = 0
  40.             byte <<= 1
  41.     while True:
  42.         yield 0
  43.  
  44. def alt_encode(message, image, bits):
  45.     NOT_BITS = ~((1 << bits) - 1)
  46.     return (
  47.         (byte & NOT_BITS) + data
  48.         for byte, data in zip(image, alt_from_bytes(message, bits))
  49.         #for byte, data in itertools.izip(image, alt_from_bytes(message, bits)) Python2
  50.     )
  51.  
  52. def get_chunk(image, bits):
  53.     bits = (1 << bits) - 1
  54.     for byte in image:
  55.         yield byte & bits
  56.  
  57. # From chunk to bits.
  58. def to_bits(amount, chunks):
  59.     AND = 1 << (amount - 1)
  60.     SHIFT = amount - 1
  61.     for chunk in chunks:
  62.         for _ in range(amount):
  63.             yield (chunk & AND) >> SHIFT
  64.             chunk <<= 1
  65.  
  66. # From bit to byte
  67. def to_bytes(bits):
  68.     index = 0
  69.     total = 0
  70.     for bit in bits:
  71.         index += 1
  72.         total <<= 1
  73.         total += bit
  74.         if index == 8:
  75.             yield total
  76.             index = 0
  77.             total = 0
  78.  
  79. # Decode an `image`:list
  80. def decode(image, bits):
  81.     return to_bytes(to_bits(bits, get_chunk(image, bits)))
  82.  
  83. def alt_decode(image, bits):
  84.     AND = 1 << (bits - 1)
  85.     SHIFT = bits - 1
  86.     AND_BITS = (1 << bits) - 1
  87.     index = 0
  88.     total = 0
  89.     for byte in image:
  90.         chunk = byte & AND_BITS
  91.         for _ in range(bits):
  92.             bit = (chunk & AND) >> SHIFT
  93.             index += 1
  94.             total <<= 1
  95.             total += bit
  96.             if index == 8:
  97.                 yield total
  98.                 index = 0
  99.                 total = 0
  100.             chunk <<= 1
  101.  
  102. def zero_padding(data, bits):
  103.     for i in data:
  104.         yield i
  105.     padding = '0' * bits
  106.     while True:
  107.         yield padding
  108.  
  109. def encode_original(message, image, bits):
  110.     joined_binary_data = ''.join(
  111.         str(bin(x))[2:].zfill(8)
  112.         for x in map(ord, message)
  113.     )
  114.  
  115.     split_binary_data = zero_padding(
  116.         (
  117.             joined_binary_data[i:i + bits]
  118.             for i in range(0, len(joined_binary_data), bits)
  119.         ),
  120.         bits
  121.     )
  122.  
  123.     reduced_image = [
  124.         str(bin(i))[2:].zfill(8)[:-bits]
  125.         for i in image
  126.     ]
  127.    
  128.     return (
  129.         int(reduced_image[i] + next(split_binary_data), 2)
  130.         for i in range(len(reduced_image))
  131.     )
  132.  
  133.  
  134. def decode_original(image, bits):
  135.     image_data_parts = [
  136.         str(bin(i))[2:].zfill(8)[8 - bits:]
  137.         for i in image
  138.     ]
  139.     image_data_binary = ''.join(image_data_parts)
  140.     image_data_split = [
  141.         image_data_binary[i:i + 8]
  142.         for i in range(0, len(image_data_binary), 8)
  143.     ]
  144.     return (
  145.         int(i, 2)
  146.         for i in image_data_split
  147.     )
  148.  
  149. bits = 4
  150. print(list(alt_encode('Hi', [255, 0, 255, 0, 255, 0, 255, 0], bits)))
  151. print(''.join(map(chr, decode(alt_encode('Hi', [255, 0, 255, 0, 255, 0, 255, 0], bits), bits))))
  152. # Hi
  153.  
  154. import time
  155.  
  156. def time_it_maker(input_, iterations=1000):
  157.     def time_it(fn, name):
  158.         start = time.time()
  159.         for _ in range(iterations):
  160.             fn(*input_)
  161.         stop = time.time()
  162.         print(name, '=', stop - start)
  163.     return time_it
  164.  
  165. def list_(fn):
  166.     def inner(*args, **kwargs):
  167.         return list(fn(*args, **kwargs))
  168.     return inner
  169.  
  170. def first(fn):
  171.     def inner(*args, **kwargs):
  172.         return next(fn(*args, **kwargs))
  173.     return inner
  174.  
  175. def loop(fn):
  176.     def inner(*args, **kwargs):
  177.         for _ in fn(*args, **kwargs):
  178.             pass
  179.     return inner
  180.  
  181. def char(fn):
  182.     def inner(*args, **kwargs):
  183.         return ''.join(map(chr, fn(*args, **kwargs)))
  184.     return inner
  185.  
  186. def time_encode(time_it):
  187.     #time_it(encode_original, 'encode_original')
  188.     #time_it(encode, 'encode')
  189.     #time_it(alt_encode, 'alt_encode')
  190.     #time_it(first(encode_original), 'next(encode_original)')
  191.     #time_it(first(encode), 'next(encode)')
  192.     #time_it(first(alt_encode), 'next(alt_encode)')
  193.     time_it(list_(encode_original), 'list(encode_original)')
  194.     time_it(list_(encode), 'list(encode)')
  195.     time_it(list_(alt_encode), 'list(alt_encode)')
  196.     time_it(loop(encode_original), 'loop(encode_original)')
  197.     time_it(loop(encode), 'loop(encode)')
  198.     time_it(loop(alt_encode), 'loop(alt_encode)')
  199.    
  200. def time_decode(time_it):
  201.     #time_it(decode_original, 'decode_original')
  202.     #time_it(decode, 'decode')
  203.     #time_it(alt_decode, 'alt_decode')
  204.     #time_it(first(decode_original), 'next(decode_original)')
  205.     #time_it(first(decode), 'next(decode)')
  206.     #time_it(first(alt_decode), 'next(alt_decode)')
  207.     time_it(char(decode_original), 'chr(decode_original)')
  208.     time_it(char(decode), 'chr(decode)')
  209.     time_it(char(alt_decode), 'chr(alt_decode)')
  210.  
  211. time_encode(time_it_maker(['Hi', [255, 0, 255, 0, 255, 0, 255, 0], bits], iterations=1000000))
  212. print('\n')
  213. decode_input = list(encode('Hi', [255, 0, 255, 0, 255, 0, 255, 0], bits))
  214. time_decode(time_it_maker([decode_input, bits], iterations=1000000))
  215.  
  216. print('\nLong\n')
  217. time_encode(time_it_maker(['a'*50000, list(itertools.islice(itertools.cycle([255, 0]), 50000)), bits], 1000))
  218. print('\n')
  219. decode_input = list(encode('a'*50000, itertools.islice(itertools.cycle([255, 0]), 50000), bits))
  220. time_decode(time_it_maker([decode_input, bits], iterations=1000))
  221.  
  222. """
  223. list(encode_original) = 40.6009998322
  224. list(encode) = 43.9160001278
  225. list(alt_encode) = 25.635999918
  226. loop(encode_original) = 41.4110000134
  227. loop(encode) = 39.7620000839
  228. loop(alt_encode) = 20.6430001259
  229.  
  230.  
  231. chr(decode_original) = 22.8190000057
  232. chr(decode) = 35.7300000191
  233. chr(alt_decode) = 31.3589999676
  234.  
  235. Long
  236.  
  237. list(encode_original) = 196.507999897
  238. list(encode) = 230.309000015
  239. list(alt_encode) = 156.924999952
  240. loop(encode_original) = 194.916000128
  241. loop(encode) = 231.868999958
  242. loop(alt_encode) = 163.7900002
  243.  
  244.  
  245. chr(decode_original) = 92.3320000172
  246. chr(decode) = 188.205000162
  247. chr(alt_decode) = 172.137000084
  248. """
Advertisement
RAW Paste Data Copied
Advertisement