Skip to content

Commit 1049b3e

Browse files
author
rootware
committed
added unredact function. Currently debugging file not found error
1 parent 2bbfec7 commit 1049b3e

File tree

6 files changed

+100
-13
lines changed

6 files changed

+100
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ redacted_*
66
ip_test.txt
77
multiredact
88
manhours*
9+
hashshadow*
910
.hashshadow*
1011
*.txt
1112
.vscode

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"request": "launch",
1111
"module": "pyredactkit",
1212
"justMyCode": true,
13-
"args": ["test/test.txt"]
13+
"args": ["redacted_attempts.log", "-u", ".hashshadow_attempts.log.json"]
1414
}
1515
]
1616
}

pyredactkit/pyredactkit.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
import argparse
77
from pyredactkit.redact import Redactor
8+
from pyredactkit.unredact import Unredactor
89
import os
910
import glob
10-
# lintertest
1111

1212
banner = """
1313
______ ______ _ _ _ ___ _
@@ -35,8 +35,17 @@ def main():
3535
formatter_class=argparse.ArgumentDefaultsHelpFormatter
3636
)
3737
parser.add_argument(
38-
"file", nargs="+",
39-
help="Path of a file or a directory of files"
38+
"file",
39+
nargs="+",
40+
help="""Path of a file or a directory of files"""
41+
)
42+
parser.add_argument(
43+
"-u",
44+
"--unredact",
45+
help="""
46+
Option to unredact masked data.
47+
pyredactkit [redactedfile] -u [.hashshadow.json]
48+
"""
4049
)
4150
parser.add_argument(
4251
"-t", "--redactiontype",
@@ -82,13 +91,16 @@ def main():
8291

8392
# redact file
8493
redact_obj = Redactor()
94+
unredact_obj = Unredactor()
8595

8696
for file in files:
8797
if args.redactiontype:
8898
redact_obj.process_file(file, args.redactiontype)
8999
elif args.dirout:
90100
redact_obj.process_file(file, args.redactiontype, args.dirout)
91101
redact_obj.process_report(file, args.dirout)
102+
elif args.unredact:
103+
unredact_obj.unredact(args.unredact, file)
92104
else:
93105
redact_obj.process_file(file)
94106
redact_obj.process_report(file)

pyredactkit/redact.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import sys
66
import re
77
import math
8-
import hashlib
98
import json
109
import uuid
1110

@@ -82,7 +81,15 @@ def allowed_file(self, file):
8281
return False
8382
return mimetypes.guess_type(file)[0] in self.get_allowed_files()
8483

85-
def write_hashmap(self, hash_map, filename):
84+
def write_hashmap(self, hash_map=dict, filename=str):
85+
"""Function that writes a .hashshadow_file.txt.json to os directory.
86+
Args:
87+
hash_map (dictionary): dictionary object to be written to file.
88+
filename (str): name of supplied file
89+
90+
Returns:
91+
Writes .hashshadow_file.txt.json to os directory
92+
"""
8693
with open(f".hashshadow_{os.path.basename(filename)}.json", "w", encoding="utf-8") as file:
8794
json.dump(hash_map, file)
8895
print(
@@ -106,6 +113,7 @@ def redact_specific(self, line=str, option=str, filename=str):
106113
Args:
107114
line (str) : line to be supplied to redact
108115
option (str): (optional) choice for redaction
116+
filename (str): name of supplied file
109117
110118
Returns:
111119
line (str): redacted line

pyredactkit/unredact.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import json
2+
import sys
3+
4+
5+
class Unredactor:
6+
"""Redactor class
7+
Class containing all methods to support un-redaction
8+
of masked data
9+
10+
"""
11+
12+
def __init__(self):
13+
"""
14+
Class Initialization
15+
Args:
16+
None
17+
18+
Returns:
19+
None
20+
"""
21+
22+
def replace_all(self, text, dictionary):
23+
"""Function to replace all the text from string
24+
Args:
25+
text (str): A line of text in string format
26+
dictionary (dict): A key value pair of masked data and original data
27+
28+
Returns:
29+
text (str): A line of text after replacing masked data with original data
30+
"""
31+
for k, v in dictionary.items():
32+
text = text.replace(k, v)
33+
return text
34+
35+
def unredact(self, lookup_file, redacted_file):
36+
"""Function to unredact masked data and produces original unredacted data.
37+
Args:
38+
lookup_file (str): Name of the file to look up key value map of masked data and original data.
39+
redacted_file (str): Name of the redacted file
40+
41+
Returns:
42+
Writes unredacted_file.txt with original unmasked data.
43+
"""
44+
with open(redacted_file, encoding="utf-8") as redacted_target:
45+
try:
46+
with open(lookup_file, encoding="utf-8") as lookup_target:
47+
with open(f"unredacted_{redacted_file}", "w", encoding="utf-8") as write_file:
48+
content = json.load(lookup_target)
49+
for line in redacted_target:
50+
line = self.replace_all(line, content)
51+
write_file.write(line)
52+
except FileNotFoundError:
53+
sys.exit(f"[ - ] {lookup_file} file was not found")
54+
except json.JSONDecodeError:
55+
sys.exit(f"[ - ] Issue decoding {lookup_file} file")

test/unredact.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import os
55
import sys
6+
import fileinput
67

78

89
def unredact(lookup_file, redacted_file):
@@ -11,17 +12,27 @@ def unredact(lookup_file, redacted_file):
1112
with open(lookup_file, encoding="utf-8") as lookup_target:
1213
with open("unredacted_file.txt", "w", encoding="utf-8") as write_file:
1314
content = json.load(lookup_target)
14-
for key, value in content.items():
15-
for line in redacted_target:
16-
if key in line:
17-
print(line)
18-
# print(line)
19-
# line = line.replace(key, value)
20-
# write_file.write(line)
15+
for line in redacted_target:
16+
line = replace_all(line, content)
17+
write_file.write(line)
2118
except FileNotFoundError:
2219
sys.exit(f"[ - ] {lookup_file} file was not found")
2320
except json.JSONDecodeError:
2421
sys.exit(f"[ - ] Issue decoding {lookup_file} file")
2522

2623

24+
def replace_all(text, dictionary):
25+
for k, v in dictionary.items():
26+
text = text.replace(k, v)
27+
return text
28+
29+
2730
unredact(".hashshadow.json", "redacted_test.txt")
31+
32+
33+
# with fileinput.FileInput("redacted_test.txt", inplace=True, backup='.bak') as file:
34+
# for line in file:
35+
# line.replace("c9b72036-172d-461a-ab87-b1098ad5649d",
36+
# "www.linkedin.com")
37+
38+
# file.write(line)

0 commit comments

Comments
 (0)