-
Notifications
You must be signed in to change notification settings - Fork 38
/
uKnownHashLists.pas
119 lines (100 loc) · 4 KB
/
uKnownHashLists.pas
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
// New to Jan 4th 2018 beta of v3.0.0 - provided for capability of users importing a known list of hashes
// from a text file and checking if they appear when the user selects a folder
// in the FileS tab.
{ Quick Hash GUI - A Linux, Windows and Apple Mac GUI for quickly selecting one or more files
and generating hash values for them.
Copyright (C) 2011-2019 Ted Smith www.quickhash-gui.org
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 2 of the License, or
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 can read a copy of the GNU General Public License at
http://www.gnu.org/licenses/>. Also, http://www.gnu.org/copyleft/gpl.html
Use of the name 'QuickHash GUI' must refer to this utility
only and must not be re-used in another tool if based upon this code.
The code is Copyright of Ted Smith 2011 - 2018 (www.quickhash-gui.org)
}
unit uKnownHashLists;
{$mode objfpc}{$H+} // {$H+} ensures strings are of unlimited size
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, contnrs;
var
HL1 : TFPHashList; // global hash list to store imported known hashes of files (HL1)
procedure CreateMemResidentHashLists();
procedure ImportHashList(Filename : string);
procedure Free;
function IsHashInTheKnownList(hashval : string) : boolean;
function CountHashesInKnownList() : integer;
implementation
procedure CreateMemResidentHashLists();
begin
try
HL1 := TFPHashList.Create;
except
ShowMessage('Could not allocate memory for hash lists');
end;
end;
// Read in existing hashes from hash file and add to HL1, inserting unique values only
procedure ImportHashList(Filename : string);
var
InFile : textfile;
SourceData : string;
LineCounter, NoOfDuplicates : integer;
begin
LineCounter := 0;
NoOfDuplicates := 0;
AssignFile(InFile, Filename);
try
reset(InFile);
while not EOF(InFile) do
begin
// Read input hash list line
readln(InFile, SourceData);
// Convert input line to uppercase to safeguard against any lowercase conflicts
SourceData := Uppercase(Trim(SourceData));
// Increment the line counter
inc(LineCounter, 1);
// Add the hash value, only if not already in the list, avoiding duplicates
if HL1.FindIndexOf(SourceData) < 0 then
begin
HL1.Add(SourceData, @SourceData);
end;
end;
finally
CloseFile(InFile);
NoOfDuplicates := LineCounter - HL1.Count;
ShowMessage(IntToStr(HL1.Count) + ' unique hashes imported into memory.' + #13#10 +
IntToStr(LineCounter) + ' lines read from input file.' + #13#10 +
IntToStr(NoOfDuplicates) + ' duplicates detected and ignored.' + #13#10 +
'Now select a folder to hash...');
end;
end;
// Is the current hash value existing in the imported hash list? If it is, returns true, false otherwise
function IsHashInTheKnownList(hashval : string) : boolean;
begin
if HL1.FindIndexOf(hashval) < 0 then // If it's there, the result will be greater than -1
begin
result := false;
end
else result := true;
end;
// Returns the count of newly imported UNIQUE hash values into the known hash list
// -1 otherwise
function CountHashesInKnownList() : integer;
begin
result := HL1.Count;
end;
// Close hash lists HL1 in case the user chooses another hashlist.
procedure Free;
begin
try
HL1.Free;
except
ShowMessage('The hash list of existing files stored in memory could not be freed.');
end;
end;
end.