-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevidence_record_print.py
174 lines (140 loc) · 6.65 KB
/
evidence_record_print.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
MIT License
Copyright (c) 2024 Florian Fischer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from datetime import datetime
import time
class PrintEr:
def spaces(self, n):
"""Helper function to generate spaces"""
return " " * n
def visualize_node(self, hash_value):
"""Visualizes a single node"""
width = len(hash_value)
top = f"+{'-' * (width + 2)}+"
middle = f"| {hash_value} |"
return [top, middle, top]
def connect_nodes(self, left_lines, right_lines, center_spacing):
"""Helper function to connect lines"""
result = []
# First line: Connection lines
first_line = ""
left_width = len(left_lines[0]) if left_lines else 0
right_width = len(right_lines[0]) if right_lines else 0
if left_lines and right_lines:
first_line = f"{self.spaces(left_width // 2)}/{self.spaces(center_spacing - 2)}\\{self.spaces(right_width // 2)}"
elif left_lines:
first_line = f"{self.spaces(left_width // 2)}|"
elif right_lines:
first_line = f"{self.spaces(center_spacing)}{self.spaces(right_width // 2)}|"
result.append(first_line)
return result
def visualize_tree(self, node, prefix=""):
"""Visualizes a hash tree"""
if not node:
return []
lines = []
node_lines = self.visualize_node(node['hash'])
# Recursively visualize child nodes
left_lines = self.visualize_tree(node.get('left'), prefix + " ")
right_lines = self.visualize_tree(node.get('right'), prefix + " ")
# Calculate widths
left_width = len(left_lines[0]) if left_lines else 0
right_width = len(right_lines[0]) if right_lines else 0
node_width = len(node_lines[0])
spacing = 4
# Center the node
node_padding = max(0, (left_width + right_width + spacing - node_width) // 2)
for line in node_lines:
lines.append(self.spaces(node_padding) + line)
# Add connection lines
if node.get('left') or node.get('right'):
connections = self.connect_nodes(
left_lines[0] if left_lines else None,
right_lines[0] if right_lines else None,
spacing
)
lines.extend(connections)
# Add child nodes
max_child_lines = max(len(left_lines), len(right_lines))
for i in range(max_child_lines):
left_line = left_lines[i] if left_lines and i < len(left_lines) else self.spaces(left_width)
right_line = right_lines[i] if right_lines and i < len(right_lines) else self.spaces(right_width)
lines.append(left_line + self.spaces(spacing) + right_line)
return lines
def create_header_box(self, text, width):
"""Helper function to create a header box"""
line = "-" * (width - 2)
return [
f"+{line}+",
f"| {text}{self.spaces(width - 4 - len(text))} |",
f"+{line}+"
]
def merge_lines_horizontally(self, left, right, spacing=4):
"""Helper function to merge lines side by side"""
result = []
max_lines = max(len(left), len(right))
left_width = len(left[0]) if left else 0
for i in range(max_lines):
left_line = left[i] if i < len(left) else self.spaces(left_width)
right_line = right[i] if i < len(right) else self.spaces(len(right[0]) if right else 0)
result.append(left_line + self.spaces(spacing) + right_line)
return result
def print_evidence_record(self, evidence_record):
"""Print an Evidence Record structure"""
if not evidence_record:
return ["No evidence record provided"]
lines = []
# Print header information
header_info = self.create_header_box(
f"Evidence Record v{evidence_record.get('version')} - {evidence_record.get('digestAlgorithm', 'Unknown Algorithm')}",
60
)
lines.extend(header_info)
lines.append("")
# Print Archive Time Stamp Sequence
ats_sequence = evidence_record.get('archiveTimeStampSequence', [])
if ats_sequence:
lines.append("Archive Time Stamp Sequence:")
lines.append("-" * 30)
for i, ats in enumerate(ats_sequence, 1):
# Create chain header
lines.append(f"Chain {i}:")
lines.append("")
# Print timestamp information
if timestamp := ats.get('timestamp'):
timestamp_str = datetime.fromtimestamp(timestamp.get('time')).strftime("%Y-%m-%d %H:%M:%S")
timestamp_box = self.create_header_box(
f"Timestamp: {timestamp_str} [{timestamp.get('algorithm', 'Unknown')}]",
50
)
for line in timestamp_box:
lines.append(" " + line) # indent timestamp
# Visualize reduced tree if present
if reduced := ats.get('reduced'):
tree_lines = self.visualize_tree(reduced)
for line in tree_lines:
lines.append(" " + line) # indent tree
lines.append("")
if i < len(ats_sequence):
lines.append("-" * 30)
return lines
def display_evidence_record(self, evidence_record):
"""Helper function to print the lines to console"""
for line in self.print_evidence_record(evidence_record):
print(line)