From 6ad140bdfb64e30a543e3c7d5c43217f2725b6ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bonhomme?= Date: Mon, 13 Mar 2017 09:34:43 +0100 Subject: [PATCH] Management of unicode. It would be perfect to manage ASCII and unicode in the same time (so 8 bits to 32 bits caracters). --- stegano/lsb/lsb.py | 4 ++-- stegano/lsbset/lsbset.py | 4 ++-- stegano/tools.py | 3 ++- tests/test_lsb.py | 12 ++++++------ 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/stegano/lsb/lsb.py b/stegano/lsb/lsb.py index a2bd745..ff64bdf 100755 --- a/stegano/lsb/lsb.py +++ b/stegano/lsb/lsb.py @@ -106,9 +106,9 @@ def reveal(input_image_file): if img.mode == 'RGBA': pixel = pixel[:3] # ignore the alpha for color in pixel: - buff += (color&1)<<(7-count) + buff += (color&1)<<(31-count) count += 1 - if count == 8: + if count == 32: bitab.append(chr(buff)) buff, count = 0, 0 if bitab[-1] == ":" and limit == None: diff --git a/stegano/lsbset/lsbset.py b/stegano/lsbset/lsbset.py index 0dd72f5..b611d38 100644 --- a/stegano/lsbset/lsbset.py +++ b/stegano/lsbset/lsbset.py @@ -104,9 +104,9 @@ def reveal(input_image_file, generator): generated_number = next(generator) # color = [r, g, b] for color in img_list[generated_number]: - buff += (color&1)<<(7-count) + buff += (color&1)<<(31-count) count += 1 - if count == 8: + if count == 32: bitab.append(chr(buff)) buff, count = 0, 0 if bitab[-1] == ":" and limit == None: diff --git a/stegano/tools.py b/stegano/tools.py index acb9693..3740b18 100755 --- a/stegano/tools.py +++ b/stegano/tools.py @@ -57,7 +57,8 @@ def a2bits_list(chars: str) -> List[str]: >>> "".join(a2bits_list("Hello World!")) '010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001' """ - return [bin(ord(x))[2:].rjust(8,"0") for x in chars] + #return [bin(ord(x))[2:].rjust(8,"0") for x in chars] + return [bin(ord(x))[2:].rjust(32,"0") for x in chars] def bs(s: int) -> str: """Converts an int to its bits representation as a string of 0's and 1's. diff --git a/tests/test_lsb.py b/tests/test_lsb.py index 939f24d..9672267 100644 --- a/tests/test_lsb.py +++ b/tests/test_lsb.py @@ -43,7 +43,7 @@ class TestLSB(unittest.TestCase): secret = lsb.hide("./tests/sample-files/Lenna.png", "") def test_hide_and_reveal(self): - messages_to_hide = ["a", "foo", "Hello World!", ":Python:"] + messages_to_hide = ['🍕', 'a', 'foo', 'Hello World!', ':Python:'] for message in messages_to_hide: secret = lsb.hide("./tests/sample-files/Lenna.png", message) secret.save("./image.png") @@ -53,7 +53,7 @@ class TestLSB(unittest.TestCase): self.assertEqual(message, clear_message) def test_with_transparent_png(self): - messages_to_hide = ["a", "foo", "Hello World!", ":Python:"] + messages_to_hide = ['🍕', 'a', 'foo', 'Hello World!', ':Python:'] for message in messages_to_hide: secret = lsb.hide("./tests/sample-files/transparent.png", message) secret.save("./image.png") @@ -64,24 +64,24 @@ class TestLSB(unittest.TestCase): @patch('builtins.input', return_value='y') def test_manual_convert_rgb(self, input): - message_to_hide = "I love 🍕 and 🍫!" + message_to_hide = 'I love 🍕 and 🍫!' secret = lsb.hide("./tests/sample-files/Lenna-grayscale.png", message_to_hide) @patch('builtins.input', return_value='n') def test_refuse_convert_rgb(self, input): - message_to_hide = "I love 🍕 and 🍫!" + message_to_hide = 'I love 🍕 and 🍫!' with self.assertRaises(Exception): secret = lsb.hide("./tests/sample-files/Lenna-grayscale.png", message_to_hide) def test_auto_convert_rgb(self): - message_to_hide = "I love 🍕 and 🍫!" + message_to_hide = 'I love 🍕 and 🍫!' secret = lsb.hide("./tests/sample-files/Lenna-grayscale.png", message_to_hide, True) def test_with_text_file(self): - text_file_to_hide = "./tests/sample-files/lorem_ipsum.txt" + text_file_to_hide = './tests/sample-files/lorem_ipsum.txt' with open(text_file_to_hide) as f: message = f.read() secret = lsb.hide("./tests/sample-files/Lenna.png", message)