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_reveal_file():
    """A file that has been hidden can be revealed."""
    original_file = "tests/FileToHide.zip"
    dirty_image = "tests/dirtyImageWFile.png"
    revealed_file_name = "tests/test_steganographer_reveal_file.zip"

    stegs = Steganographer()
    revealed_file_data, _ = stegs.steganographer_reveal(dirty_image)

    with open(revealed_file_name, 'wb') as rev_file:
        rev_file.write(revealed_file_data)

    with open(original_file, 'rb') as original, open(revealed_file_name,
                                                     'rb') as revealed:
        assert original.read() == revealed.read()

    os.remove(revealed_file_name)
Beispiel #4
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)