mirror of
https://github.com/cedricbonhomme/Stegano.git
synced 2025-05-12 17:18:30 +02:00
Updated tests of the exifHeader module and some improvements for this module.
This commit is contained in:
parent
064c87146c
commit
f9b33b33ef
2 changed files with 31 additions and 10 deletions
|
@ -27,8 +27,9 @@ __license__ = "GPLv3"
|
|||
from PIL import Image
|
||||
import piexif
|
||||
|
||||
def hide(img, img_enc, secret_message = None, secret_file = None):
|
||||
def hide(input_image_file, img_enc, secret_message = None, secret_file = None):
|
||||
"""
|
||||
Hide a message (string) in an image.
|
||||
"""
|
||||
from zlib import compress
|
||||
from base64 import b64encode
|
||||
|
@ -36,12 +37,11 @@ def hide(img, img_enc, secret_message = None, secret_file = None):
|
|||
if secret_file != None:
|
||||
with open(secret_file, "r") as f:
|
||||
secret_file_content = f.read()
|
||||
if secret_file != None:
|
||||
text = compress(b64encode(bytes(secret_file_content, "utf-8")))
|
||||
else:
|
||||
text = compress(b64encode(bytes(secret_message, "utf-8")))
|
||||
|
||||
img = Image.open(img)
|
||||
img = Image.open(input_image_file)
|
||||
if "exif" in img.info:
|
||||
exif_dict = piexif.load(img.info["exif"])
|
||||
else:
|
||||
|
@ -51,15 +51,17 @@ def hide(img, img_enc, secret_message = None, secret_file = None):
|
|||
exif_bytes = piexif.dump(exif_dict)
|
||||
img.save(img_enc, exif=exif_bytes)
|
||||
img.close()
|
||||
return img
|
||||
|
||||
|
||||
def reveal(img):
|
||||
def reveal(input_image_file):
|
||||
"""
|
||||
Find a message in an image.
|
||||
"""
|
||||
from base64 import b64decode
|
||||
from zlib import decompress
|
||||
|
||||
exif_dict = piexif.load(img)
|
||||
exif_dict = piexif.load(input_image_file)
|
||||
encoded_message = exif_dict["0th"][piexif.ImageIFD.ImageDescription]
|
||||
return b64decode(decompress(encoded_message))
|
||||
|
||||
|
@ -95,11 +97,13 @@ if __name__ == "__main__":
|
|||
|
||||
if options.hide:
|
||||
if options.secret_message != "" and options.secret_file == "":
|
||||
hide(img=options.input_image_file, img_enc=options.output_image_file, \
|
||||
hide(input_image_file=options.input_image_file, \
|
||||
img_enc=options.output_image_file, \
|
||||
secret_message=options.secret_message)
|
||||
elif options.secret_message == "" and options.secret_file != "":
|
||||
hide(img=options.input_image_file, img_enc=options.output_image_file, \
|
||||
hide(input_image_file=options.input_image_file, \
|
||||
img_enc=options.output_image_file, \
|
||||
secret_file=options.secret_file)
|
||||
|
||||
elif options.reveal:
|
||||
reveal(img=options.input_image_file)
|
||||
reveal(input_image_file=options.input_image_file)
|
||||
|
|
|
@ -35,7 +35,7 @@ class TestEXIFHeader(unittest.TestCase):
|
|||
"""
|
||||
Test hiding the empty string.
|
||||
"""
|
||||
secret = exifHeader.hide("./examples/pictures/Elisha-Cuthbert.jpg",
|
||||
secret = exifHeader.hide("./examples/pictures/20160505T130442.jpg",
|
||||
"./image.jpg", secret_message="")
|
||||
#secret.save(""./image.png"")
|
||||
|
||||
|
@ -47,13 +47,30 @@ class TestEXIFHeader(unittest.TestCase):
|
|||
messages_to_hide = ["a", "foo", "Hello World!", ":Python:"]
|
||||
|
||||
for message in messages_to_hide:
|
||||
secret = exifHeader.hide("./examples/pictures/Elisha-Cuthbert.jpg",
|
||||
secret = exifHeader.hide("./examples/pictures/20160505T130442.jpg",
|
||||
"./image.jpg", secret_message=message)
|
||||
|
||||
clear_message = exifHeader.reveal("./image.jpg")
|
||||
|
||||
self.assertEqual(message, message)
|
||||
|
||||
def test_with_image_without_exif_data(self):
|
||||
secret = exifHeader.hide("./examples/pictures/Elisha-Cuthbert.jpg",
|
||||
"./image.jpg", secret_message="")
|
||||
#secret.save(""./image.png"")
|
||||
|
||||
clear_message = exifHeader.reveal("./image.jpg")
|
||||
|
||||
self.assertEqual(b"", clear_message)
|
||||
|
||||
def test_with_png_image(self):
|
||||
secret = exifHeader.hide("./examples/pictures/Lenna.png",
|
||||
"./image.png", secret_message="Secret")
|
||||
#secret.save(""./image.png"")
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
clear_message = exifHeader.reveal("./image.png")
|
||||
|
||||
def tearDown(self):
|
||||
try:
|
||||
os.unlink("./image.jpg")
|
||||
|
|
Loading…
Add table
Reference in a new issue