-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpgme.pas
144 lines (125 loc) · 3.73 KB
/
gpgme.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
unit gpgme;
{$mode delphi}{$H+}
interface
uses
Classes, SysUtils;
type
TGpgmeContext = class(TComponent)
protected
FLibrary: AnsiString;
FKeys: TStringList;
FAsciiArmor: Boolean;
procedure SetKeys(NewKeys: TStringList); virtual;
public
procedure Encrypt(SrcStream, DstStream: TStream); virtual; overload;
procedure Encrypt(SrcFile: AnsiString; DstStream: TStream); virtual; overload;
procedure Encrypt(SrcStream: TStream; DstFile: AnsiString); virtual; overload;
procedure Encrypt(SrcFile, DstFile: AnsiString); virtual; overload;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property LibraryLocation: AnsiString read FLibrary write FLibrary;
property Keys: TStringList read FKeys write SetKeys;
property AsciiArmor: Boolean read FAsciiArmor write FAsciiArmor;
end;
implementation
uses gpgme_h;
procedure TGpgmeContext.SetKeys(NewKeys: TStringList);
begin
FKeys.Assign(NewKeys);
end;
procedure TGpgmeContext.Encrypt(SrcStream, DstStream: TStream);
var
context: Tgpgme_ctx_t;
Src, Dst: Tgpgme_data_t;
keys: TDynPgpgmeKeyArray;
SrcAdapter, DstAdapter: TGpgmeStreamAdapter;
x: Integer;
Res: Tgpgme_error;
key: Pgpgme_key;
flags: Tgpgme_encrypt_flags_t;
begin
if FKeys.Count = 0 then raise GpgmeError.Create('No keys were selected for the encryption operation.');
LoadGpgme(FLibrary);
CheckGpgmeError(gpgme_new(@context));
try
gpgme_set_armor(context, True);
SetLength(Keys, FKeys.Count + 1);
for x := 0 to FKeys.Count - 1 do begin
Res := gpgme_get_key(context, PChar(FKeys.Strings[x]), @key, false);
if Res.error <> 0 then begin
if Res.errorcode = GPG_ERR_EOF
then raise GpgmeError.Create('The Key with ID "' + FKeys.Strings[x] + '" was not found.')
else CheckGpgmeError(Res);
end else begin
keys[x] := key;
end;
end;
keys[FKeys.Count] := nil;
try
DstAdapter := TGpgmeStreamAdapter.Create(DstStream);
try
dst := DstAdapter.DH;
SrcAdapter := TGpgmeStreamAdapter.Create(SrcStream);
try
Src := SrcAdapter.DH;
CheckGpgmeError(gpgme_op_encrypt(context, @(keys[0]), 0, Src, Dst));
finally
FreeAndNil(SrcAdapter);
end;
finally
FreeAndNil(DstAdapter);
end;
finally
for x := 0 to Length(keys) - 1
do if Assigned(keys[x]) then gpgme_key_release(keys[x]);
SetLength(keys, 0);
end;
finally
gpgme_release(context);
end;
end;
procedure TGpgmeContext.Encrypt(SrcFile: AnsiString; DstStream: TStream);
var
SrcStream: TFileStream;
begin
SrcStream := TFileStream.Create(SrcFile, fmOpenRead or fmShareDenyWrite);
try
Encrypt(SrcStream, DstStream);
finally
FreeAndNil(SrcStream);
end;
end;
procedure TGpgmeContext.Encrypt(SrcStream: TStream; DstFile: AnsiString);
var
DstStream: TFileStream;
begin
DstStream := TFileStream.Create(DstFile, fmOpenReadWrite or fmShareExclusive or fmCreate);
try
Encrypt(SrcStream, DstStream);
finally
FreeAndNil(DstStream);
end;
end;
procedure TGpgmeContext.Encrypt(SrcFile, DstFile: AnsiString);
var
DstStream: TFileStream;
begin
DstStream := TFileStream.Create(DstFile, fmOpenReadWrite or fmShareExclusive or fmCreate);
try
Encrypt(SrcFile, DstStream);
finally
FreeAndNil(DstStream);
end;
end;
constructor TGpgmeContext.Create(AOwner: TComponent);
begin
Inherited;
FKeys := TStringList.Create;
end;
destructor TGpgmeContext.Destroy;
begin
if Assigned(FKeys) then FreeAndNil(FKeys);
inherited;
end;
end.