The generator provided to lsbset.hide() lsbset.reveal() is now a function. Closes #5.

This commit is contained in:
Cédric Bonhomme 2016-05-22 15:23:54 +02:00
parent a0f6f24d7c
commit f8668a978e
No known key found for this signature in database
GPG key ID: A1CB94DE57B7A70D
3 changed files with 29 additions and 23 deletions

View file

@ -20,12 +20,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>
__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.5 $"
__version__ = "$Revision: 0.5.1 $"
__date__ = "$Date: 2016/03/18 $"
__revision__ = "$Date: 2016/05/22 $"
__license__ = "GPLv3"
try:
from stegano import lsbset
from stegano.lsbset import generators
except:
print("Install stegano: sudo pip install Stegano")
@ -67,6 +69,10 @@ parser.set_defaults(input_image_file = './pictures/Lenna.png',
(options, args) = parser.parse_args()
try:
generator = getattr(generators, options.generator_function)()
except AttributeError as e:
raise e
if options.hide:
if options.secret_message != "" and options.secret_file == "":
@ -74,7 +80,7 @@ if options.hide:
elif options.secret_message == "" and options.secret_file != "":
secret = tools.binary2base64(options.secret_file)
img_encoded = lsbset.hide(options.input_image_file, secret, options.generator_function)
img_encoded = lsbset.hide(options.input_image_file, secret, generator)
try:
img_encoded.save(options.output_image_file)
except Exception as e:
@ -83,7 +89,7 @@ if options.hide:
elif options.reveal:
try:
secret = lsbset.reveal(options.input_image_file, options.generator_function)
secret = lsbset.reveal(options.input_image_file, generator)
except IndexError:
print("Impossible to detect message.")
exit(0)

View file

@ -20,8 +20,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>
__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.4.1 $"
__version__ = "$Revision: 0.4.2 $"
__date__ = "$Date: 2016/03/13 $"
__revision__ = "$Date: 2016/05/22 $"
__license__ = "GPLv3"
import sys
@ -36,7 +37,7 @@ try:
except NameError:
pass
def hide(input_image_file, message, generator_function, auto_convert_rgb=False):
def hide(input_image_file, message, generator, auto_convert_rgb=False):
"""
Hide a message (string) in an image with the
LSB (Least Significant Bit) technique.
@ -69,11 +70,6 @@ def hide(input_image_file, message, generator_function, auto_convert_rgb=False):
raise Exception("The message you want to hide is too long: {}".\
format(message_length))
try:
generator = getattr(generators, generator_function)()
except AttributeError as e:
raise e
while index + 3 <= len_message_bits :
generated_number = next(generator)
(r, g, b) = img_list[generated_number]
@ -97,8 +93,7 @@ def hide(input_image_file, message, generator_function, auto_convert_rgb=False):
return encoded
def reveal(input_image_file, generator_function):
def reveal(input_image_file, generator):
"""
Find a message in an image
(with the LSB technique).
@ -110,8 +105,6 @@ def reveal(input_image_file, generator_function):
bitab = []
limit = None
generator = getattr(generators, generator_function)()
while True:
generated_number = next(generator)
# color = [r, g, b]
@ -178,6 +171,10 @@ if __name__ == '__main__':
(options, args) = parser.parse_args()
try:
generator = getattr(generators, options.generator_function)()
except AttributeError as e:
raise e
if options.hide:
if options.secret_message != "" and options.secret_file == "":
@ -185,7 +182,7 @@ if __name__ == '__main__':
elif options.secret_message == "" and options.secret_file != "":
secret = tools.binary2base64(options.secret_file)
img_encoded = hide(options.input_image_file, secret, options.generator_function)
img_encoded = hide(options.input_image_file, secret, generator)
try:
img_encoded.save(options.output_image_file)
except Exception as e:
@ -194,7 +191,7 @@ if __name__ == '__main__':
elif options.reveal:
try:
secret = reveal(options.input_image_file, options.generator_function)
secret = reveal(options.input_image_file, generator)
except IndexError:
print("Impossible to detect message.")
exit(0)

View file

@ -20,14 +20,16 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>
__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.1 $"
__version__ = "$Revision: 0.2 $"
__date__ = "$Date: 2016/04/13 $"
__revision__ = "$Date: 2016/05/22 $"
__license__ = "GPLv3"
import os
import unittest
from stegano import lsbset
from stegano.lsbset import generators
class TestLSBSet(unittest.TestCase):
@ -37,33 +39,34 @@ class TestLSBSet(unittest.TestCase):
"""
with self.assertRaises(AssertionError):
secret = lsbset.hide("./tests/sample-files/Lenna.png", "",
"eratosthenes")
generators.eratosthenes())
def test_hide_and_reveal(self):
messages_to_hide = ["a", "foo", "Hello World!", ":Python:"]
for message in messages_to_hide:
secret = lsbset.hide("./tests/sample-files/Lenna.png", message,
"eratosthenes")
generators.eratosthenes())
secret.save("./image.png")
clear_message = lsbset.reveal("./image.png", "eratosthenes")
clear_message = lsbset.reveal("./image.png",
generators.eratosthenes())
self.assertEqual(message, clear_message)
def test_hide_and_reveal_with_bad_generator(self):
message_to_hide = "Hello World!"
secret = lsbset.hide("./tests/sample-files/Lenna.png", message_to_hide,
"eratosthenes")
generators.eratosthenes())
secret.save("./image.png")
with self.assertRaises(IndexError):
clear_message = lsbset.reveal("./image.png", "identity")
clear_message = lsbset.reveal("./image.png", generators.identity())
def test_with_unknown_generator(self):
message_to_hide = "Hello World!"
with self.assertRaises(AttributeError):
secret = lsbset.hide("./tests/sample-files/Lenna.png",
message_to_hide, "eratosthene")
message_to_hide, generators.eratosthene())
def tearDown(self):
try: