wav scaffolding and tests

This commit is contained in:
Alexander Treml 2025-06-19 19:16:39 +02:00
parent 19c5dcad5c
commit 35d03bc0c6
4 changed files with 126 additions and 0 deletions

5
stegano/wav/__init__.py Normal file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env python
from .wav import hide, reveal
__all__ = ["hide", "reveal"]

59
stegano/wav/wav.py Normal file
View file

@ -0,0 +1,59 @@
#!/usr/bin/env python
# Stegano - Stéganô is a basic Python Steganography module.
# Copyright (C) 2010-2024 Cédric Bonhomme - https://www.cedricbonhomme.org
#
# For more information : https://github.com/cedricbonhomme/Stegano
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.2 $"
__date__ = "$Date: 2010/10/01 $"
__revision__ = "$Date: 2017/02/06 $"
__license__ = "GPLv3"
from typing import IO, Union
import wave
def hide(input_file: Union[str, IO[bytes]], message: str, output_file: Union[str, IO[bytes]]):
"""
Hide a message (string) in a .wav audio file.
Use the lsb of each sample to hide the message string characters as ASCII values.
The first eight bits are used for message_length of the string.
"""
message_length = len(message)
assert message_length != 0, "message message_length is zero"
# TODO messages in audio files could likely be much longer in most cases
assert message_length < 255, "message is too long"
output = wave.open(output_file, "wb")
with wave.open(input_file, "rb") as input:
pass
# TODO
def reveal(input_file: Union[str, IO[bytes]]):
"""
Find a message in an image.
Check the lsb of each sample for hidden message characters (ASCII values).
The first eight bits are used for message_length of the string.
"""
message = ""
with wave.open(input_file, "rb") as input:
pass
# TODO
return message

Binary file not shown.

62
tests/test_wav.py Normal file
View file

@ -0,0 +1,62 @@
#!/usr/bin/env python
# Stegano - Stegano is a pure Python steganography module.
# Copyright (C) 2010-2025 Cédric Bonhomme - https://www.cedricbonhomme.org
#
# For more information : https://github.com/cedricbonhomme/Stegano
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.1 $"
__date__ = "$Date: 2016/05/19 $"
__license__ = "GPLv3"
import os
import unittest
from stegano import wav
class TestWav(unittest.TestCase):
def test_hide_empty_message(self):
"""
Test hiding the empty string.
"""
with self.assertRaises(AssertionError):
wav.hide("./tests/sample-files/free-software-song.wav", "", "./audio.wav")
def test_hide_and_reveal(self):
messages_to_hide = ["a", "foo", "Hello World!", ":Python:"]
for message in messages_to_hide:
wav.hide("./tests/sample-files/free-software-song.wav", message, "./audio.wav")
clear_message = wav.reveal("./audio.wav")
self.assertEqual(message, clear_message)
def test_with_too_long_message(self):
with open("./tests/sample-files/lorem_ipsum.txt") as f:
message = f.read()
with self.assertRaises(AssertionError):
wav.hide("./tests/sample-files/free-software-song.wav", message, "./audio.wav")
def tearDown(self):
try:
os.unlink("./audio.wav")
except Exception:
pass
if __name__ == "__main__":
unittest.main()