def test_steganogrified_name():
    """Data will be hidden in a file with steganogrified at the end, when no output file name is provided."""
    clean_message_image = copy2(
        CLEAN_PNG_LOCATION,
        CLEAN_PNG_LOCATION[:-4] + "_test_message_steganogrified_name.png")
    clean_file_image = copy2(
        CLEAN_PNG_LOCATION,
        CLEAN_PNG_LOCATION[:-4] + "_test_file_steganogrified_name.png")
    hidden_message = "Hidden text from test_steganogrified_name."
    file_to_hide = "tests/FileToHide.zip"

    stegs = Steganographer()
    hidden_message_fname = stegs.steganographer_hide(clean_message_image,
                                                     hidden_message)
    steganogrified_message_fname = clean_message_image[:-4] + "Steganogrified.png"
    hidden_file_fname = stegs.steganographer_hide_file(clean_file_image,
                                                       file_to_hide)
    steganogrified_file_fname = clean_file_image[:-4] + "Steganogrified.png"

    assert hidden_message_fname == steganogrified_message_fname
    assert os.path.isfile(steganogrified_message_fname)
    assert hidden_file_fname == steganogrified_file_fname
    assert os.path.isfile(steganogrified_file_fname)

    os.remove(clean_message_image)
    os.remove(hidden_message_fname)
    os.remove(clean_file_image)
    os.remove(hidden_file_fname)
def test_unicode_inverse():
    """Unicode characters are hidden and revealed."""
    message = "test_unicode hidden message. Some random unicode characters: ­ЊЂѕ рЙе нЁ н╣ п╗ яЌ ▀џ ЯцЎ рЃџ рїЕ рЈю"

    stegs = Steganographer()
    assert message == stegs.steganographer_reveal(
        stegs.steganographer_hide(CLEAN_PNG_LOCATION, message,
                                  "tests/dirtyImage.png"))[0].decode('utf-8')
def test_steganographer_inverse(hidden_message):
    """Steganographer_reveal reveals what was hidden by steganographer_hide."""
    clean_image = CLEAN_PNG_LOCATION
    dirty_image = "tests/dirtyImage_test_steganographer_inverse.png"

    stegs = Steganographer()
    revealed_message = stegs.steganographer_reveal(
        stegs.steganographer_hide(clean_image, hidden_message,
                                  dirty_image))[0].decode('utf-8')
    assert revealed_message == hidden_message

    os.remove(dirty_image)
def test_steganographer_hide_name():
    """The image a string is hidden in is the correct one."""
    clean_image = CLEAN_PNG_LOCATION
    dirty_image = "tests/dirtyImage_test_steganographer_hide_name.png"
    hidden_message = "Hidden text from test_steganographer_hide_name."

    stegs = Steganographer()
    hidden_fname = stegs.steganographer_hide(clean_image, hidden_message,
                                             dirty_image)

    assert hidden_fname == dirty_image

    os.remove(dirty_image)
def test_stegs_hide_string_nonsense():
    """A random string, that can cause a decode error, will correctly be hidden in a new image."""
    clean_image = CLEAN_PNG_LOCATION
    dirty_image = "tests/dirtyImage_test_steganographer_hide_string_nonsense.png"
    hidden_message = "─ю­љАЉ─ю─ю─ю─ю─ю─ю─ю─ю─юнг─ю\U000fc423─ю─ю─ю─юнг─ю─ю─юнгнг─юнг\U000fc423─юнг\U000fc423нгнг─ю\U000fc423нг─юнг­љАЋ­љАЋ­љАЉ­љАЋ­љАЋ­љАЋ­љАЉ­љАЋ­љАЉ­љАЋ­љАЉ"

    stegs = Steganographer()
    hidden_fname = stegs.steganographer_hide(clean_image, hidden_message,
                                             dirty_image)

    with open(clean_image, 'rb') as clean, open(hidden_fname, 'rb') as dirty:
        assert clean.read() != dirty.read()
    assert compare_images(clean_image, hidden_fname) < 650
    try:
        Image.open(hidden_fname)
    except OSError:
        pytest.fail("Image is corrupt " + hidden_fname)

    os.remove(dirty_image)
def test_steganographer_hide_string():
    """A string will correctly be hidden in a new image."""
    clean_image = CLEAN_PNG_LOCATION
    dirty_image = "tests/dirtyImage_test_steganographer_hide_string.png"
    hidden_message = "Hidden text from test_steganographer_hide_string."

    stegs = Steganographer()
    hidden_fname = stegs.steganographer_hide(clean_image, hidden_message,
                                             dirty_image)

    with open(clean_image, 'rb') as clean, open(hidden_fname, 'rb') as dirty:
        assert clean.read() != dirty.read()
    assert compare_images(clean_image, hidden_fname) < 500
    try:
        Image.open(hidden_fname)
    except OSError:
        pytest.fail("Image is corrupt " + hidden_fname)

    os.remove(dirty_image)
Example #7
0
def main():
    """Given an image and a message or file steganographer will hide the message or file in the bits of the image."""
    parser = argparse.ArgumentParser(
        description=
        "hides a message in a file or returns a message hidden in a file")
    parser.add_argument(
        "input",
        help="file to hide a message in or file to reveal a message from")
    parser.add_argument("-m",
                        "--message",
                        help="message to be hidden in the input file")
    parser.add_argument(
        "-o",
        "--output",
        help=
        "name of output file to hide message in or to write revealed message",
        default='')
    parser.add_argument("-f",
                        "--file",
                        help="file to be hidden in the input file")
    parser.add_argument("-r",
                        "--reveal",
                        action='store_true',
                        help="a file will be revealed")
    parser.add_argument("-v",
                        "--version",
                        action='version',
                        version="steganographer {}".format(
                            pkg_resources.get_distribution('pip').version),
                        help="show version and exit")
    args = parser.parse_args()

    stegs = Steganographer()

    # There is a message to hide.
    if args.message:
        hidden_fname = stegs.steganographer_hide(args.input, args.message,
                                                 args.output)
        print("The message has been hidden in " + hidden_fname)
    # There is a file to hide.
    elif args.file:
        hidden_fname = stegs.steganographer_hide_file(args.input, args.file,
                                                      args.output)
        print("The file " + args.file + " has been hidden in " + hidden_fname)
    # Revealing a file.
    elif args.reveal:
        revealed_data, file_name = stegs.steganographer_reveal(args.input)

        if args.output:
            file_name = args.output

        with open(file_name, 'wb') as rev_file:
            rev_file.write(revealed_data)

        print("The hidden file was revealed in " + file_name)
    # Revealing a message.
    else:
        hidden_message = stegs.steganographer_reveal(
            args.input)[0].decode('utf-8')

        if args.output:
            open(args.output, 'w', encoding='utf-8').write(hidden_message)
            print("The hidden message was written to " + args.output)
        else:
            try:
                print("The hidden message was...\n" + hidden_message)
            except UnicodeEncodeError:  # pragma: no cover
                output_name = args.input.split('.')[0] + '_message.txt'

                print(
                    "The hidden message contains unsupported unicode characters and cannot be fully displayed "
                    + "here. The correct message has been written to",
                    output_name)
                print(hidden_message.encode('utf-8'))
                open(output_name, 'w', encoding='utf-8').write(hidden_message)