-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKeyring.pas
200 lines (181 loc) · 5.68 KB
/
Keyring.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
unit Keyring;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TfrmSelectKeyring = class(TForm)
lblPub: TLabel;
lblSec: TLabel;
edtPub: TEdit;
btnPub: TButton;
edtSec: TEdit;
btnSec: TButton;
btnOK: TButton;
btnCancel: TButton;
OpenDlg: TOpenDialog;
SaveDlg: TSaveDialog;
procedure btnPubClick(Sender: TObject);
procedure edtPubChange(Sender: TObject);
procedure btnSecClick(Sender: TObject);
procedure edtSecChange(Sender: TObject);
private
public
OpenKeyring: Boolean;
FChanging: Boolean;
FNew: Boolean;
end;
implementation
{$R *.dfm}
function TryFileName(BaseFileName, NewFileName: String; OpenKeyring: Boolean;
var FoundFileName: String; ValidBaseName: String): Boolean;
var
BaseValid: Boolean;
begin
BaseValid := ExtractFileName(BaseFileName) = ValidBaseName;
BaseFileName := ExtractFilePath(BaseFileName) + NewFileName;
if OpenKeyring then
begin
Result := FileExists(BaseFileName);
if Result then
FoundFileName := BaseFileName;
end
else
begin
Result := BaseValid;
FoundFileName := BaseFileName;
end;
end;
function TryExtension(BaseFileName, NewExtension: String; OpenKeyring: Boolean;
var FoundFileName: String): Boolean;
begin
BaseFileName := ChangeFileExt(BaseFileName, NewExtension);
if OpenKeyring then
begin
Result := FileExists(BaseFileName);
if Result then
FoundFileName := BaseFileName;
end
else
begin
Result := True;
FoundFileName := BaseFileName;
end;
end;
function FindKeyRingPair(FileName: string; IsPublic: Boolean; OpenKeyring: Boolean;
var PubFileName, SecFileName: string): Boolean;
begin
Result := True;
if not OpenKeyring and (ExtractFileExt(FileName) = '') then
if IsPublic then
FileName := ChangeFileExt(FileName, '.pkr')
else
FileName := ChangeFileExt(FileName, '.skr');
PubFileName := '';
SecFileName := '';
if IsPublic then
PubFileName := FileName
else
SecFileName := FileName;
if Result then
begin
if PubFileName <> '' then
begin
if TryFileName(PubFileName, 'secring.skr', OpenKeyring, FileName, 'pubring.pkr') or
TryFileName(PubFileName, 'secring.seckr', OpenKeyring, FileName, 'pubring.pubkr') or
TryFileName(PubFileName, 'secring.pgp', OpenKeyring, FileName, 'pubring.pgp') or
TryExtension(PubFileName, '.skr', OpenKeyring, FileName) or
TryExtension(PubFileName, '.seckr', OpenKeyring, FileName) then
SecFileName := FileName;
end
else
begin
if TryFileName(PubFileName, 'pubring.pkr', OpenKeyring, FileName, 'secring.skr') or
TryFileName(PubFileName, 'pubring.pubkr', OpenKeyring, FileName, 'secring.seckr') or
TryFileName(PubFileName, 'pubring.pgp', OpenKeyring, FileName, 'secring.pgp') or
TryExtension(PubFileName, '.pkr', OpenKeyring, FileName) or
TryExtension(PubFileName, '.pubkr', OpenKeyring, FileName) then
PubFileName := FileName;
end;
Result := PubFileName <> '';
end;
end;
procedure TfrmSelectKeyring.btnPubClick(Sender: TObject);
begin
if OpenKeyring then
begin
OpenDlg.Filter := 'Public keyring files (*.pkr;*.pubkr;*.pgp)|*.pkr;*.pubkr;*.pgp|All files(*.*)|*.*';
OpenDlg.FilterIndex := 1;
OpenDlg.DefaultExt := '.pkr';
OpenDlg.Title := 'Select public keyring file';
OpenDlg.FileName := edtPub.Text;
if OpenDlg.Execute then
edtPub.Text := OpenDlg.FileName;
end
else
begin
SaveDlg.Filter := 'Public keyring files (*.pkr;*.pubkr;*.pgp)|*.pkr;*.pubkr;*.pgp|All files(*.*)|*.*';
SaveDlg.FilterIndex := 1;
SaveDlg.DefaultExt := '.pkr';
SaveDlg.Title := 'Select public keyring file';
SaveDlg.FileName := edtPub.Text;
if SaveDlg.Execute then
edtPub.Text := SaveDlg.FileName;
end;
end;
procedure TfrmSelectKeyring.btnSecClick(Sender: TObject);
begin
if OpenKeyring then
begin
OpenDlg.Filter := 'Private keyring files (*.skr;*.prvkr;*.pgp)|*.skr;*.prvkr;*.pgp|All files(*.*)|*.*';
OpenDlg.FilterIndex := 1;
OpenDlg.DefaultExt := '.skr';
OpenDlg.Title := 'Select private keyring file';
OpenDlg.FileName := edtSec.Text;
if OpenDlg.Execute then
edtSec.Text := OpenDlg.FileName;
end
else
begin
SaveDlg.Filter := 'Private keyring files (*.skr;*.prvkr;*.pgp)|*.skr;*.prvkr;*.pgp|All files(*.*)|*.*';
SaveDlg.FilterIndex := 1;
SaveDlg.DefaultExt := '.skr';
SaveDlg.Title := 'Select private keyring file';
SaveDlg.FileName := edtSec.Text;
if SaveDlg.Execute then
edtSec.Text := SaveDlg.FileName;
end;
end;
procedure TfrmSelectKeyring.edtPubChange(Sender: TObject);
{var
PubFileName, SecFileName: String;}
begin
{ if not FChanging and FindKeyRingPair(edtPub.Text, True, OpenKeyring, PubFileName,
SecFileName) then
begin
FChanging := True;
try
edtSec.Text := SecFileName;
finally
FChanging := False;
end;
end;}
btnOK.Enabled := edtPub.Text <> '';
end;
procedure TfrmSelectKeyring.edtSecChange(Sender: TObject);
{var
PubFileName, SecFileName: String;}
begin
{ if not FChanging and FindKeyRingPair(edtSec.Text, False, OpenKeyring, PubFileName,
SecFileName) then
begin
FChanging := True;
try
edtPub.Text := PubFileName;
finally
FChanging := False;
end;
end;}
btnOK.Enabled := edtPub.Text <> '';
end;
end.