Skip to content

Commit c5d68a5

Browse files
- implementado controle de input e output nos formato base64 para criptografia
- faltando implmentacao input e output no formato hex para criptografia
1 parent 64c464d commit c5d68a5

1 file changed

Lines changed: 77 additions & 4 deletions

File tree

src/utils/RALCripto.pas

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,23 @@ TRALCriptoOptions = class(TPersistent)
2323
property Key: StringRAL read FKey write FKey;
2424
end;
2525

26+
TRALCriptoInOutType = (cotNone, cotBase64, cotHEX);
27+
2628
{ TRALCripto }
2729

2830
/// Base class for all crypto functions
2931
TRALCripto = class
3032
private
3133
FKey: StringRAL;
34+
FIntputType : TRALCriptoInOutType;
35+
FOutputType : TRALCriptoInOutType;
3236
protected
3337
procedure SetKey(const AValue: StringRAL); virtual;
38+
function BeforeDecrypt(AValue: TStream): TStream;
39+
function BeforeEncrypt(AValue: TStream): TStream;
3440
public
41+
constructor Create;
42+
3543
function Decrypt(const AValue: StringRAL; ABinary : boolean = false): StringRAL; overload;
3644
function Decrypt(AValue: TBytes): StringRAL; overload;
3745
function Decrypt(AValue: TStream): StringRAL; overload;
@@ -48,6 +56,8 @@ TRALCripto = class
4856
function EncryptAsStream(AValue: TStream): TStream; virtual; abstract;
4957
published
5058
property Key: StringRAL read FKey write SetKey;
59+
property OutputType: TRALCriptoInOutType read FOutputType write FOutputType;
60+
property IntputType: TRALCriptoInOutType read FIntputType write FIntputType;
5161
end;
5262

5363
implementation
@@ -115,7 +125,7 @@ function TRALCripto.Encrypt(AValue: TStream): StringRAL;
115125
{ TODO -cCompatibilidade : Melhorar esse código pra compatibilizar com versões antigas do Delphi }
116126
vStream := nil;
117127
try
118-
vStream := EncryptAsStream(AValue);
128+
vStream := BeforeEncrypt(AValue);
119129
vStream.Position := 0;
120130

121131
Result := StreamToString(vStream);
@@ -124,14 +134,77 @@ function TRALCripto.Encrypt(AValue: TStream): StringRAL;
124134
end;
125135
end;
126136

137+
function TRALCripto.BeforeDecrypt(AValue: TStream): TStream;
138+
var
139+
vStreamInput, vStreamEnc : TStream;
140+
begin
141+
case FIntputType of
142+
cotNone : begin
143+
Result := DecryptAsStream(AValue);
144+
end;
145+
cotBase64: begin
146+
vStreamInput := TRALBase64.DecodeAsStream(AValue);
147+
try
148+
Result := DecryptAsStream(vStreamInput);
149+
finally
150+
FreeAndNil(vStreamInput);
151+
end;
152+
end;
153+
end;
154+
155+
Result.Position := 0;
156+
157+
case FOutputType of
158+
cotBase64: begin
159+
vStreamEnc := TRALBase64.EncodeAsStream(Result);
160+
FreeAndNil(Result);
161+
Result := vStreamEnc;
162+
end;
163+
end;
164+
end;
165+
166+
function TRALCripto.BeforeEncrypt(AValue: TStream): TStream;
167+
var
168+
vStreamInput, vStreamEnc : TStream;
169+
begin
170+
case FIntputType of
171+
cotNone : begin
172+
Result := EncryptAsStream(AValue);
173+
end;
174+
cotBase64: begin
175+
vStreamInput := TRALBase64.DecodeAsStream(AValue);
176+
try
177+
Result := EncryptAsStream(vStreamInput);
178+
finally
179+
FreeAndNil(vStreamInput);
180+
end;
181+
end;
182+
end;
183+
184+
Result.Position := 0;
185+
186+
case FOutputType of
187+
cotBase64: begin
188+
vStreamEnc := TRALBase64.EncodeAsStream(Result);
189+
FreeAndNil(Result);
190+
Result := vStreamEnc;
191+
end;
192+
end;
193+
end;
194+
195+
constructor TRALCripto.Create;
196+
begin
197+
FOutputType := cotNone;
198+
end;
199+
127200
function TRALCripto.Decrypt(AValue: TStream): StringRAL;
128201
var
129202
vStream: TStream;
130203
begin
131204
{ TODO -cCompatibilidade : Melhorar esse código pra compatibilizar com versões antigas do Delphi }
132205
vStream := nil;
133206
try
134-
vStream := DecryptAsStream(AValue);
207+
vStream := BeforeDecrypt(AValue);
135208
vStream.Position := 0;
136209

137210
Result := StreamToString(vStream);
@@ -192,7 +265,7 @@ function TRALCripto.EncryptAsBytes(AValue: TStream): TBytes;
192265
var
193266
vResult: TStream;
194267
begin
195-
vResult := EncryptAsStream(AValue);
268+
vResult := BeforeEncrypt(AValue);
196269
try
197270
Result := StreamToBytes(vResult);
198271
finally
@@ -204,7 +277,7 @@ function TRALCripto.DecryptAsBytes(AValue: TStream): TBytes;
204277
var
205278
vResult: TStream;
206279
begin
207-
vResult := DecryptAsStream(AValue);
280+
vResult := BeforeDecrypt(AValue);
208281
try
209282
Result := StreamToBytes(vResult);
210283
finally

0 commit comments

Comments
 (0)