Skip to content

Commit a30b0ca

Browse files
authored
Merge pull request #6 from expyriment/new_dif_procedure
New dif procedure
2 parents c02c122 + f8dafc9 commit a30b0ca

File tree

4 files changed

+41
-39
lines changed

4 files changed

+41
-39
lines changed

dataintegrityfingerprint/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__author__ = 'Oliver Lindemann <oliver@expyriment.org>, ' \
22
'Florian Krause <florian@expyriment.org>'
33

4-
__version__ = '0.5.4'
4+
__version__ = '0.6.0'
55

66
from sys import version_info as _vi
77
if _vi.major< 3:

dataintegrityfingerprint/cli.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from . import DataIntegrityFingerprint
66
from . import __version__
77

8+
89
def run():
910

1011
def progress(count, total, status=''):
@@ -16,15 +17,19 @@ def progress(count, total, status=''):
1617
sys.stdout.flush()
1718

1819
parser = argparse.ArgumentParser(
19-
description="Create a Data Integrity Fingerprint (DIF). v{0}".format(
20-
__version__),
21-
epilog="(c) O. Lindemann & F. Krause")
20+
description="""Data Integrity Fingerprint (DIF)
21+
Python Reference Implementation v{0}""".format(__version__),
22+
formatter_class=argparse.RawDescriptionHelpFormatter,
23+
epilog="""Authors:
24+
Oliver Lindemann <oliver@expyriment.org>
25+
Florian Krause <florian@expyriment.org""")
2226

2327
parser.add_argument("PATH", nargs='?', default=None,
2428
help="the path to the data folder or file")
25-
parser.add_argument("-f", "--from-checksums-file", dest="fromchecksumsfile",
26-
action="store_true",
27-
help="Calculate dif from checksums file. PATH is a checksums file",
29+
parser.add_argument("-f", "--from-checksums-file",
30+
dest="fromchecksumsfile", action="store_true",
31+
help="Calculate dif from checksums file. " +
32+
"PATH is a checksums file",
2833
default=False)
2934
parser.add_argument("-a", "--algorithm", metavar="ALGORITHM",
3035
type=str,
@@ -46,13 +51,14 @@ def progress(count, total, status=''):
4651
action="store_true",
4752
help="print available algorithms",
4853
default=False)
49-
parser.add_argument("-s", "--save-checksums-file", dest="savechecksumsfile",
50-
action="store_true",
54+
parser.add_argument("-s", "--save-checksums-file",
55+
dest="savechecksumsfile", action="store_true",
5156
help="save checksums to file",
5257
default=False)
5358
parser.add_argument("-d", "--diff-checksums-file", metavar="CHECKSUMSFILE",
5459
type=str,
55-
help="Calculate differences of checksums file to CHECKSUMSFILE")
60+
help="Calculate differences of checksums file to " +
61+
"CHECKSUMSFILE")
5662
parser.add_argument("-n", "--no-multi-processing", dest="nomultiprocess",
5763
action="store_true",
5864
help="switch of multi processing",
@@ -64,10 +70,10 @@ def progress(count, total, status=''):
6470
parser.add_argument("--non-crypthographic",
6571
dest="noncrypto",
6672
action="store_true",
67-
help="allow non crypthographic algorithms (Not suggested, please " +
68-
"read documentation carefully!) ",
73+
help="allow non crypthographic algorithms " +
74+
"(Not suggested, please read documentation " +
75+
"carefully!)",
6976
default=False)
70-
7177
args = vars(parser.parse_args())
7278

7379
if args['listalgos']:
@@ -100,7 +106,7 @@ def progress(count, total, status=''):
100106
dif.generate(progress=progress)
101107
print("")
102108

103-
#output
109+
# Output
104110
if args['savechecksumsfile']:
105111
outfile = os.path.split(
106112
dif.data)[-1] + ".{0}".format(dif._hash_algorithm)
@@ -124,15 +130,13 @@ def progress(count, total, status=''):
124130
elif args['checksums']:
125131
print(dif.checksums.strip())
126132
else:
127-
print("Data Integrity Fingerprint (DIF) v{0}".format(__version__))
128-
129-
print("\nFolder: {0}".format(dif.data))
133+
print("Data Integrity Fingerprint (DIF)".format(__version__))
134+
print("")
135+
print("Folder: {0}".format(dif.data))
130136
print("Files: {0} included".format(dif.count_files()))
131137
print("Algorithm: {0}".format(dif.hash_algorithm))
132138
print("DIF: {}".format(dif))
133139

134140

135-
136-
137141
if __name__ == "__main__":
138142
run()

dataintegrityfingerprint/dif.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class DataIntegrityFingerprint:
3030

3131
CRYPTOGRAPHIC_ALGORITHMS = OpenSSLHashAlgorithm.SUPPORTED_ALGORITHMS
3232
NON_CRYPTOGRAPHIC_ALGORITHMS = ZlibHashAlgorithm.SUPPORTED_ALGORITHMS
33-
SEPARATOR = "_"
3433
CHECKSUM_FILENAME_SEPARATOR = " "
3534

3635
def __init__(self, data, from_checksums_file=False,
@@ -105,33 +104,24 @@ def file_hash_list(self):
105104
@property
106105
def checksums(self):
107106
rtn = ""
108-
for h, fl in self.file_hash_list:
107+
for h, fl in sorted(self.file_hash_list, key=lambda x: x[1]):
109108
rtn += u"{0}{1}{2}\n".format(h, self.CHECKSUM_FILENAME_SEPARATOR,
110109
fl)
111110
return rtn
112111

113112
@property
114-
def prefix(self):
115-
return "".join(x for x in self.hash_algorithm.lower() if x.isalnum())
116-
117-
@property
118-
def postfix(self):
113+
def dif(self):
119114
if len(self.file_hash_list) < 1:
120115
return None
121116

122117
hasher = new_hash_instance(self._hash_algorithm,
123118
self.allow_non_cryptographic_algorithms)
124-
hasher.update(self.checksums.encode("utf-8"))
119+
concat = "".join(["".join(x) for x in self.file_hash_list])
120+
hasher.update(concat.encode("utf-8"))
125121
return hasher.checksum
126122

127-
@property
128-
def dif(self):
129-
if self.postfix is not None:
130-
return self.prefix + self.SEPARATOR + self.postfix
131-
132123
def count_files(self):
133-
"""Returns the number of files that are included in the dif
134-
"""
124+
"""Return the number of files that are included in the dif."""
135125

136126
return len(self.get_files())
137127

dataintegrityfingerprint/gui.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import tkinter.ttk as ttk
1818
from tkinter import filedialog, messagebox
1919

20+
from . import __version__
2021
from .dif import DataIntegrityFingerprint as DIF
2122
from .dif import new_hash_instance
2223

@@ -30,13 +31,12 @@ def __init__(self, master, *args, **kwargs):
3031
self.master = master
3132
self.master.title("Data Integrity Fingerprint (DIF)")
3233
self.about_text = """Data Integrity Fingerprint (DIF)
33-
34-
Reference Python implementation
34+
Reference Python implementation v{0}
3535
3636
Authors:
3737
Oliver Lindemann <oliver@expyriment.org>
3838
Florian Krause <florian@expyriment.org>
39-
"""
39+
""".format(__version__)
4040
self.dif = None
4141
self.create_widgets()
4242
self.dir_button.focus()
@@ -88,6 +88,7 @@ def create_widgets(self):
8888
self.algorithm_menu = tk.Menu(self.menubar)
8989
self.algorithm_var = tk.StringVar()
9090
self.algorithm_var.set("SHA-256")
91+
self.algorithm_var.trace('w', self.set_dif_label)
9192
for algorithm in DIF.CRYPTOGRAPHIC_ALGORITHMS:
9293
self.algorithm_menu.add_radiobutton(label=algorithm,
9394
value=algorithm,
@@ -172,8 +173,9 @@ def create_widgets(self):
172173
self.frame2 = ttk.Frame(self.master)
173174
self.frame2.grid(row=3, column=0, sticky="NSWE")
174175
self.frame2.grid_columnconfigure(1, weight=1)
175-
self.dif_label = ttk.Label(self.frame2, text="DIF:")
176+
self.dif_label = ttk.Label(self.frame2)
176177
self.dif_label.grid(row=0, column=0)
178+
self.set_dif_label()
177179
self.dif_var = tk.StringVar()
178180
self.dif_var.set("")
179181
self.dif_entry = ttk.Entry(self.frame2, textvariable=self.dif_var,
@@ -208,6 +210,10 @@ def set_data_directory(self, data_dir=None, *args):
208210
self.statusbar["text"] = "Ready"
209211
self.unblock_gui(enable_save_checksums=False)
210212

213+
def set_dif_label(self, *args):
214+
self.dif_label.config(text="DIF [{0}]:".format(
215+
self.algorithm_var.get()))
216+
211217
def block_gui(self):
212218
"""Block GUI from user entry."""
213219

@@ -266,6 +272,7 @@ def _progress(count, total, status=''):
266272
self.checksum_list.insert(1.0, self.dif.checksums.strip("\n"))
267273
self.checksum_list["state"] = tk.DISABLED
268274
self.dif_var.set(self.dif.dif)
275+
self.set_dif_label()
269276
self.copy_button["state"] = tk.NORMAL
270277
self.copy_button.focus()
271278
self.statusbar["text"] = "Done"
@@ -304,6 +311,7 @@ def open_checksums(self, *args):
304311
self.checksum_list.delete(1.0, tk.END)
305312
self.checksum_list.insert(1.0, checksums)
306313
self.checksum_list["state"] = tk.DISABLED
314+
self.set_dif_label()
307315
self.dif_var.set(master_hash)
308316
self.copy_button["state"] = tk.NORMAL
309317
self.copy_button.focus()
@@ -322,7 +330,7 @@ def save_checksums(self, *args):
322330
"""Save checksums file."""
323331

324332
if self.checksum_list.get(1.0, tk.END).strip("\n") != "":
325-
algorithm = self.dif_var.get().split(DIF.SEPARATOR)[0]
333+
algorithm = self.algorithm_var.get()
326334
extension = "".join(x for x in algorithm.lower() if x.isalnum())
327335
filename = filedialog.asksaveasfilename(
328336
defaultextension=extension,

0 commit comments

Comments
 (0)