Skip to content

Commit 69a049b

Browse files
committed
- implementado classe de conversao para hexadecimal
- correção do output de criptografias para string (formato binario)
1 parent ef9aac2 commit 69a049b

File tree

7 files changed

+386
-29
lines changed

7 files changed

+386
-29
lines changed

src/others/pascal_brotli

src/utils/RALCripto.pas

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ interface
55

66
uses
77
Classes, SysUtils,
8-
RALTypes, RALStream, RALBase64, RALConsts;
8+
RALTypes, RALStream, RALBase64, RALConsts, RALHexadecimal;
99

1010
type
1111

@@ -35,6 +35,7 @@ TRALCripto = class
3535
FOutputType : TRALCriptoInOutType;
3636
protected
3737
procedure SetKey(const AValue: StringRAL); virtual;
38+
function CanCript : boolean; virtual;
3839
function BeforeDecrypt(AValue: TStream): TStream;
3940
function BeforeEncrypt(AValue: TStream): TStream;
4041
public
@@ -128,7 +129,7 @@ function TRALCripto.Encrypt(AValue: TStream): StringRAL;
128129
vStream := BeforeEncrypt(AValue);
129130
vStream.Position := 0;
130131

131-
Result := StreamToString(vStream);
132+
Result := StreamToString(vStream, True);
132133
finally
133134
FreeAndNil(vStream);
134135
end;
@@ -138,6 +139,9 @@ function TRALCripto.BeforeDecrypt(AValue: TStream): TStream;
138139
var
139140
vStreamInput, vStreamEnc : TStream;
140141
begin
142+
if not CanCript then
143+
Exit;
144+
141145
case FIntputType of
142146
cotNone : begin
143147
Result := DecryptAsStream(AValue);
@@ -150,6 +154,14 @@ function TRALCripto.BeforeDecrypt(AValue: TStream): TStream;
150154
FreeAndNil(vStreamInput);
151155
end;
152156
end;
157+
cotHEX: begin
158+
vStreamInput := TRALHexadecimal.DecodeAsStream(AValue);
159+
try
160+
Result := DecryptAsStream(vStreamInput);
161+
finally
162+
FreeAndNil(vStreamInput);
163+
end;
164+
end;
153165
end;
154166

155167
Result.Position := 0;
@@ -160,13 +172,21 @@ function TRALCripto.BeforeDecrypt(AValue: TStream): TStream;
160172
FreeAndNil(Result);
161173
Result := vStreamEnc;
162174
end;
175+
cotHEX: begin
176+
vStreamEnc := TRALHexadecimal.EncodeAsStream(Result);
177+
FreeAndNil(Result);
178+
Result := vStreamEnc;
179+
end;
163180
end;
164181
end;
165182

166183
function TRALCripto.BeforeEncrypt(AValue: TStream): TStream;
167184
var
168185
vStreamInput, vStreamEnc : TStream;
169186
begin
187+
if not CanCript then
188+
Exit;
189+
170190
case FIntputType of
171191
cotNone : begin
172192
Result := EncryptAsStream(AValue);
@@ -179,6 +199,14 @@ function TRALCripto.BeforeEncrypt(AValue: TStream): TStream;
179199
FreeAndNil(vStreamInput);
180200
end;
181201
end;
202+
cotHEX: begin
203+
vStreamInput := TRALHexadecimal.DecodeAsStream(AValue);
204+
try
205+
Result := EncryptAsStream(vStreamInput);
206+
finally
207+
FreeAndNil(vStreamInput);
208+
end;
209+
end;
182210
end;
183211

184212
Result.Position := 0;
@@ -189,9 +217,19 @@ function TRALCripto.BeforeEncrypt(AValue: TStream): TStream;
189217
FreeAndNil(Result);
190218
Result := vStreamEnc;
191219
end;
220+
cotHEX: begin
221+
vStreamEnc := TRALHexadecimal.EncodeAsStream(Result);
222+
FreeAndNil(Result);
223+
Result := vStreamEnc;
224+
end;
192225
end;
193226
end;
194227

228+
function TRALCripto.CanCript: boolean;
229+
begin
230+
Result := True;
231+
end;
232+
195233
constructor TRALCripto.Create;
196234
begin
197235
FOutputType := cotNone;

src/utils/RALCriptoAES.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ function TRALCriptoAES.EncryptAES(AInput, AOutput: PByte; AInputLen: integer): i
468468

469469
Result := AInputLen;
470470
vTheads := RALCPUCount;
471-
vSize := AInputLen div (vTheads - 1) div 16 * 16;
471+
vSize := AInputLen div vTheads div 16 * 16;
472472
if vSize < 16 then
473473
vSize := 16;
474474

src/utils/RALCriptoOpenSSL.pas

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ TRALCriptoOpenSSL = class(TRALCripto)
1616
private
1717
FAESType : TRALCriptoOpenSSLTypes;
1818
FIV : StringRAL;
19+
protected
20+
function CanCript : boolean; override;
1921
public
2022
constructor Create;
2123

@@ -30,6 +32,15 @@ implementation
3032

3133
{ TRALCriptoOpenSSL }
3234

35+
function TRALCriptoOpenSSL.CanCript: boolean;
36+
begin
37+
Result := inherited;
38+
if TRALOpenSSL.GetInstance.LibraryHandle = 0 then begin
39+
Result := False;
40+
raise Exception.Create('OpenSSL not loaded');
41+
end;
42+
end;
43+
3344
constructor TRALCriptoOpenSSL.Create;
3445
begin
3546
inherited;

src/utils/RALHashBase.pas

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ interface
55

66
uses
77
Classes, SysUtils,
8-
RALBase64, RALTypes, RALTools, RALStream, RALConsts;
8+
RALBase64, RALTypes, RALTools, RALStream, RALConsts, RALHexadecimal;
99

1010
type
1111
TRALHashOutputType = (rhotNone, rhotHex, rhotBase64, rhotBase64Url);
@@ -101,19 +101,8 @@ function TRALHashBase.DigestToBase64Url(AValue: TBytes): StringRAL;
101101
end;
102102

103103
function TRALHashBase.DigestToHex(AValue: TBytes): StringRAL;
104-
const
105-
HexChar: array[0..15] of CharRAL = ('0', '1', '2', '3', '4', '5', '6', '7',
106-
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
107-
var
108-
vInt: IntegerRAL;
109104
begin
110-
vInt := 0;
111-
while vInt < Length(AValue) do
112-
begin
113-
Result := Result + HexChar[(AValue[vInt] shr 4) and $0f];
114-
Result := Result + HexChar[(AValue[vInt] and $0f)];
115-
Inc(vInt);
116-
end;
105+
Result := TRALHexadedimal.Encode(AValue);
117106
end;
118107

119108
function TRALHashBase.Finalize: TBytes;

0 commit comments

Comments
 (0)