-
Notifications
You must be signed in to change notification settings - Fork 69
/
lsb.py
90 lines (71 loc) · 2.54 KB
/
lsb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# Stegano - Stegano is a pure 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.7 $"
__date__ = "$Date: 2016/03/13 $"
__revision__ = "$Date: 2019/05/31 $"
__license__ = "GPLv3"
from typing import IO, Iterator, Union
from stegano import tools
from .generators import identity
def hide(
image: Union[str, IO[bytes]],
message: str,
generator: Union[None, Iterator[int]] = None,
shift: int = 0,
encoding: str = "UTF-8",
auto_convert_rgb: bool = False,
):
"""Hide a message (string) in an image with the
LSB (Least Significant Bit) technique.
"""
hider = tools.Hider(image, message, encoding, auto_convert_rgb)
width = hider.encoded_image.width
if not generator:
generator = identity()
while shift != 0:
next(generator)
shift -= 1
while hider.encode_another_pixel():
generated_number = next(generator)
col = generated_number % width
row = int(generated_number / width)
hider.encode_pixel((col, row))
return hider.encoded_image
def reveal(
encoded_image: Union[str, IO[bytes]],
generator: Union[None, Iterator[int]] = None,
shift: int = 0,
encoding: str = "UTF-8",
close_file: bool = True,
):
"""Find a message in an image (with the LSB technique)."""
revealer = tools.Revealer(encoded_image, encoding, close_file)
width = revealer.encoded_image.width
if not generator:
generator = identity()
while shift != 0:
next(generator)
shift -= 1
while True:
generated_number = next(generator)
col = generated_number % width
row = int(generated_number / width)
if revealer.decode_pixel((col, row)):
return revealer.secret_message