forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheraser.py
More file actions
24 lines (18 loc) · 684 Bytes
/
Copy patheraser.py
File metadata and controls
24 lines (18 loc) · 684 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
Secret file eraser.
"""
from itertools import islice
from random import random
from .bitmap import Bitmap
def erase(bitmap: Bitmap) -> None:
"""Scramble a previously hidden data."""
if bitmap.reserved_field > 0:
for byte_offset in islice(bitmap.byte_offsets, bitmap.reserved_field):
bitmap[byte_offset] = randomize_lsb(bitmap[byte_offset])
bitmap.reserved_field = 0
print("Erased a secret file from the bitmap")
else:
print("Secret file not found in the bitmap")
def randomize_lsb(value: int) -> int:
"""Set a random bit on the least-significant position."""
return value & ~1 if random() < 0.5 else value | 1