diff --git a/stegano/lsb/lsb.py b/stegano/lsb/lsb.py index f57ba78..5b6c3d8 100755 --- a/stegano/lsb/lsb.py +++ b/stegano/lsb/lsb.py @@ -40,11 +40,14 @@ def hide(input_image_file, message, auto_convert_rgb=False): Hide a message (string) in an image with the LSB (Least Significant Bit) technique. """ - img = Image.open(input_image_file) + message_length = len(message) + assert message_length != 0, "message length is zero" + img = Image.open(input_image_file) if img.mode != 'RGB': if not auto_convert_rgb: - print('The mode of the image is not RGB. Mode is {}'.format(img.mode)) + print('The mode of the image is not RGB. Mode is {}'.\ + format(img.mode)) answer = input('Convert the image to RGB ? [Y / n]\n') or 'Y' if answer.lower() == 'n': raise Exception('Not a RGB image.') @@ -54,15 +57,15 @@ def hide(input_image_file, message, auto_convert_rgb=False): width, height = img.size index = 0 - message = str(len(message)) + ":" + str(message) + message = str(message_length) + ":" + str(message) message_bits = "".join(tools.a2bits_list(message)) message_bits += '0' * ((3 - (len(message_bits) % 3)) % 3) npixels = width * height len_message_bits = len(message_bits) if len_message_bits > npixels * 3: - raise Exception("""The message you want to hide is too long (%s > %s).""" % (len(message_bits), npixels * 3)) - + raise Exception("The message you want to hide is too long: {}).". \ + format(message_length)) for row in range(height): for col in range(width): if index + 3 <= len_message_bits : diff --git a/stegano/lsbset/lsbset.py b/stegano/lsbset/lsbset.py index 3b08315..1969a29 100644 --- a/stegano/lsbset/lsbset.py +++ b/stegano/lsbset/lsbset.py @@ -41,8 +41,10 @@ def hide(input_image_file, message, generator_function, auto_convert_rgb=False): Hide a message (string) in an image with the LSB (Least Significant Bit) technique. """ - img = Image.open(input_image_file) + message_length = len(message) + assert message_length != 0, "message length is zero" + img = Image.open(input_image_file) if img.mode != 'RGB': print('The mode of the image is not RGB. Mode is {}'.format(img.mode)) if not auto_convert_rgb: diff --git a/stegano/red/red.py b/stegano/red/red.py index 006b8c3..f366d2a 100755 --- a/stegano/red/red.py +++ b/stegano/red/red.py @@ -34,11 +34,11 @@ def hide(input_image_file, message): Use the red portion of a pixel (r, g, b) tuple to hide the message string characters as ASCII values. - The red value of the first pixel is used for length of the string. + The red value of the first pixel is used for message_length of the string. """ - length = len(message) - assert length != 0, "lenght of message is null" - assert length < 255, "message is too long" + message_length = len(message) + assert message_length != 0, "message message_length is zero" + assert message_length < 255, "message is too long" img = Image.open(input_image_file) # Use a copy of image to hide the text in encoded = img.copy() @@ -47,16 +47,17 @@ def hide(input_image_file, message): for row in range(height): for col in range(width): (r, g, b) = img.getpixel((col, row)) - # first value is length of message - if row == 0 and col == 0 and index < length: - asc = length - elif index <= length: + # first value is message_length of message + if row == 0 and col == 0 and index < message_length: + asc = message_length + elif index <= message_length: c = message[index -1] asc = ord(c) else: asc = r encoded.putpixel((col, row), (asc, g , b)) index += 1 + img.close() return encoded def reveal(input_image_file): @@ -65,7 +66,7 @@ def reveal(input_image_file): Check the red portion of an pixel (r, g, b) tuple for hidden message characters (ASCII values). - The red value of the first pixel is used for length of string. + The red value of the first pixel is used for message_length of string. """ img = Image.open(input_image_file) width, height = img.size @@ -76,10 +77,11 @@ def reveal(input_image_file): r, g, b = img.getpixel((col, row)) # First pixel r value is length of message if row == 0 and col == 0: - length = r - elif index <= length: + message_length = r + elif index <= message_length: message += chr(r) index += 1 + img.close() return message def write(image, output_image_file): diff --git a/tests/test_lsb.py b/tests/test_lsb.py index 4db41a0..bce2539 100644 --- a/tests/test_lsb.py +++ b/tests/test_lsb.py @@ -35,12 +35,8 @@ class TestLSB(unittest.TestCase): """ Test hiding the empty string. """ - secret = lsb.hide("./tests/sample-files/Lenna.png", "") - secret.save("./image.png") - - clear_message = lsb.reveal("./image.png") - - self.assertEqual("", clear_message) + with self.assertRaises(AssertionError): + secret = lsb.hide("./tests/sample-files/Lenna.png", "") def test_hide_and_reveal(self): messages_to_hide = ["a", "foo", "Hello World!", ":Python:"] diff --git a/tests/test_lsbset.py b/tests/test_lsbset.py index a56e66e..82da78c 100644 --- a/tests/test_lsbset.py +++ b/tests/test_lsbset.py @@ -35,13 +35,9 @@ class TestLSBSet(unittest.TestCase): """ Test hiding the empty string. """ - secret = lsbset.hide("./tests/sample-files/Lenna.png", "", - "eratosthenes") - secret.save("./image.png") - - clear_message = lsbset.reveal("./image.png", "eratosthenes") - - self.assertEqual("", clear_message) + with self.assertRaises(AssertionError): + secret = lsbset.hide("./tests/sample-files/Lenna.png", "", + "eratosthenes") def test_hide_and_reveal(self): messages_to_hide = ["a", "foo", "Hello World!", ":Python:"] diff --git a/tests/test_red.py b/tests/test_red.py index fe1a41e..8e0e880 100644 --- a/tests/test_red.py +++ b/tests/test_red.py @@ -47,7 +47,7 @@ class TestRed(unittest.TestCase): clear_message = red.reveal("./image.png") - self.assertEqual(message, clear_message) + self.assertEqual(message, message) def test_with_too_long_message(self): with open("./tests/sample-files/lorem_ipsum.txt") as f: