Skip to content

Commit 334b87e

Browse files
committed
Changed most class functions of TDECPasswordHash into normal ones and made a small improvement of the modularity of the password unit tests.
1 parent dd86973 commit 334b87e

File tree

3 files changed

+74
-72
lines changed

3 files changed

+74
-72
lines changed

Source/DECHash.pas

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,11 +1257,10 @@ TBCryptBSDData = record
12571257
/// <returns>
12581258
/// Calculated hash value
12591259
/// </returns>
1260-
class function GetCryptHash(
1261-
const Password : string;
1262-
const Params : string;
1263-
const Salt : TBytes;
1264-
Format : TDECFormatClass):string; override;
1260+
function GetCryptHash(const Password : string;
1261+
const Params : string;
1262+
const Salt : TBytes;
1263+
Format : TDECFormatClass):string; override;
12651264
{$EndRegion}
12661265
public
12671266
/// <summary>
@@ -1271,11 +1270,11 @@ TBCryptBSDData = record
12711270
/// <summary>
12721271
/// Returns the maximum supported length of the salt value in byte
12731272
/// </summary>
1274-
class function MaxSaltLength:UInt8; override;
1273+
function MaxSaltLength:UInt8; override;
12751274
/// <summary>
12761275
/// Returns the minimum supported length of the salt value in byte
12771276
/// </summary>
1278-
class function MinSaltLength:UInt8; override;
1277+
function MinSaltLength:UInt8; override;
12791278
/// <summary>
12801279
/// Returns the maximum length of a user supplied password given for the
12811280
/// algorithm in byte
@@ -1288,11 +1287,11 @@ TBCryptBSDData = record
12881287
/// <summary>
12891288
/// Returns the minimum allowed value for the Cost property
12901289
/// </summary>
1291-
class function MinCost:UInt8;
1290+
function MinCost:UInt8;
12921291
/// <summary>
12931292
/// Returns the maximum allowed value for the Cost property
12941293
/// </summary>
1295-
class function MaxCost:UInt8;
1294+
function MaxCost:UInt8;
12961295

12971296
/// <summary>
12981297
/// Checks whether a given password is the correct one for a password
@@ -5247,11 +5246,10 @@ procedure THash_BCrypt.Expandkey(Salt : TBytes;
52475246
end;
52485247
end;
52495248

5250-
class function THash_BCrypt.GetCryptHash(
5251-
const Password : string;
5252-
const Params : string;
5253-
const Salt : TBytes;
5254-
Format : TDECFormatClass): string;
5249+
function THash_BCrypt.GetCryptHash(const Password : string;
5250+
const Params : string;
5251+
const Salt : TBytes;
5252+
Format : TDECFormatClass): string;
52555253
var
52565254
Hash : THash_BCrypt;
52575255
begin
@@ -5396,7 +5394,7 @@ procedure THash_BCrypt.DoTransform(Buffer: PUInt32Array);
53965394
// is done directly in method Calc.
53975395
end;
53985396

5399-
class function THash_BCrypt.MaxCost: UInt8;
5397+
function THash_BCrypt.MaxCost: UInt8;
54005398
begin
54015399
Result := 31;
54025400
end;
@@ -5406,17 +5404,17 @@ class function THash_BCrypt.MaxPasswordLength: UInt8;
54065404
Result := 72;
54075405
end;
54085406

5409-
class function THash_BCrypt.MaxSaltLength: UInt8;
5407+
function THash_BCrypt.MaxSaltLength: UInt8;
54105408
begin
54115409
Result := 16;
54125410
end;
54135411

5414-
class function THash_BCrypt.MinCost: UInt8;
5412+
function THash_BCrypt.MinCost: UInt8;
54155413
begin
54165414
Result := 4;
54175415
end;
54185416

5419-
class function THash_BCrypt.MinSaltLength: UInt8;
5417+
function THash_BCrypt.MinSaltLength: UInt8;
54205418
begin
54215419
Result := 16;
54225420
end;

Source/DECHashAuthentication.pas

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,8 @@ TDECPasswordHash = class(TDECHashAuthentication, IDECHashPassword)
684684
/// <param name="Format">
685685
/// Format class for formatting the output
686686
/// </param>
687-
class function GetCryptSalt(const Salt : TBytes;
688-
Format : TDECFormatClass):string; virtual;
687+
function GetCryptSalt(const Salt : TBytes;
688+
Format : TDECFormatClass):string; virtual;
689689
/// <summary>
690690
/// Returns the hash required for the crypt-like password storing
691691
/// in that format. If a salt etc. is needed that needs to be scepcified
@@ -714,22 +714,22 @@ TDECPasswordHash = class(TDECHashAuthentication, IDECHashPassword)
714714
/// Returns an empty string if the the algorithm on which this is being
715715
/// used is not a Crypt/BSD compatible password hash algorithm.
716716
/// </returns>
717-
class function GetCryptHash(const Password : string;
718-
const Params : string;
719-
const Salt : TBytes;
720-
Format : TDECFormatClass):string; virtual;
717+
function GetCryptHash(const Password : string;
718+
const Params : string;
719+
const Salt : TBytes;
720+
Format : TDECFormatClass):string; virtual;
721721
{$EndRegion}
722722
public
723723
/// <summary>
724724
/// Returns the maximum length of a salt value given for the algorithm
725725
/// in byte
726726
/// </summary>
727-
class function MaxSaltLength:UInt8; virtual; abstract;
727+
function MaxSaltLength:UInt8; virtual; abstract;
728728
/// <summary>
729729
/// Returns the minimum length of a salt value given for the algorithm
730730
/// in byte
731731
/// </summary>
732-
class function MinSaltLength:UInt8; virtual; abstract;
732+
function MinSaltLength:UInt8; virtual; abstract;
733733
/// <summary>
734734
/// Returns the maximum length of a user supplied password given for the
735735
/// algorithm in byte
@@ -1460,10 +1460,6 @@ procedure TDECPasswordHash.SetSalt(const Value: TBytes);
14601460
if (Length(Value) < MinSaltLength) then
14611461
raise EDECHashException.CreateFmt(sSaltValueTooShort, [MinSaltLength]);
14621462

1463-
//// Angeblich verursacht das hier das Speicherleck?!
1464-
// SetLength(FSalt, Length(Value));
1465-
// if (Length(Value) > 0) then
1466-
// Move(Value[0], FSalt[0], Length(Value));
14671463
FSalt := Value;
14681464
end;
14691465

@@ -1478,15 +1474,12 @@ function TDECPasswordHash.GetCryptParams(const Params : string;
14781474
Result := '';
14791475
end;
14801476

1481-
class function TDECPasswordHash.GetCryptSalt(const Salt : TBytes;
1482-
Format : TDECFormatCLass): string;
1477+
function TDECPasswordHash.GetCryptSalt(const Salt : TBytes;
1478+
Format : TDECFormatCLass): string;
14831479
var
14841480
FormattedSalt : TBytes;
14851481
begin
14861482
FormattedSalt := Format.Encode(Salt);
1487-
// SetLength(Result, Length(FormattedSalt) + 1);
1488-
// Move(FormattedSalt[0], Result[Low(Result) + 1], Length(FormattedSalt));
1489-
// Result[Low(Result)] := '$';
14901483

14911484
Result := '$' + TEncoding.ASCII.GetString(FormattedSalt);
14921485
end;
@@ -1524,11 +1517,10 @@ procedure TDECPasswordHash.DoDone;
15241517
SetLength(FSalt, 0);
15251518
end;
15261519

1527-
class function TDECPasswordHash.GetCryptHash(
1528-
const Password : string;
1529-
const Params : string;
1530-
const Salt : TBytes;
1531-
Format : TDECFormatClass): string;
1520+
function TDECPasswordHash.GetCryptHash(const Password : string;
1521+
const Params : string;
1522+
const Salt : TBytes;
1523+
Format : TDECFormatClass): string;
15321524
begin
15331525
Result := '';
15341526
end;

Unit Tests/Tests/TestDECHash.pas

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,22 @@ THash_TestBaseExtended = class(THash_TestBase)
215215
procedure TestCalcStreamNoDoneMulti;
216216
end;
217217

218+
/// <summary>
219+
/// Type for test data specified for Crypt/BSD format tests. Not declared in
220+
/// THash_TestPasswordBase as the compiler couldn't find it there.
221+
/// </summary>
222+
TCryptTestData = record
223+
/// <summary>
224+
/// Number of the password from the password array
225+
/// to use for that test case
226+
/// </summary>
227+
PWNum: UInt8;
228+
/// <summary>
229+
/// Crypt/BSD formatted password storage data
230+
/// </summary>
231+
CryptData: string;
232+
end;
233+
218234
/// <summary>
219235
/// Adds test data initialization common for all password hash algorithms
220236
/// </summary>
@@ -676,38 +692,34 @@ TBCryptBSDTestData = record
676692
Cost : UInt8;
677693
end;
678694

679-
TPair = record
680-
pn: byte;
681-
bs: string;
682-
end;
683695
const
684696
Passwords: array[0..4] of string = ('', 'a', 'abc',
685697
'abcdefghijklmnopqrstuvwxyz',
686698
'~!@#$%^&*() ~!@#$%^&*()PNBFRD');
687699

688700
// Source of test data: Wolfgang Erhardt's implementation.
689701
// pn is the index into Passwords
690-
TestData: array[1..20] of TPair = (
691-
(pn: 0; bs: '$2a$06$DCq7YPn5Rq63x1Lad4cll.TV4S6ytwfsfvkgY8jIucDrjc8deX1s.'),
692-
(pn: 0; bs: '$2a$08$HqWuK6/Ng6sg9gQzbLrgb.Tl.ZHfXLhvt/SgVyWhQqgqcZ7ZuUtye'),
693-
(pn: 0; bs: '$2a$10$k1wbIrmNyFAPwPVPSVa/zecw2BCEnBwVS2GbrmgzxFUOqW9dk4TCW'),
694-
(pn: 0; bs: '$2a$12$k42ZFHFWqBp3vWli.nIn8uYyIkbvYRvodzbfbK18SSsY.CsIQPlxO'),
695-
(pn: 1; bs: '$2a$06$m0CrhHm10qJ3lXRY.5zDGO3rS2KdeeWLuGmsfGlMfOxih58VYVfxe'),
696-
(pn: 1; bs: '$2a$08$cfcvVd2aQ8CMvoMpP2EBfeodLEkkFJ9umNEfPD18.hUF62qqlC/V.'),
697-
(pn: 1; bs: '$2a$10$k87L/MF28Q673VKh8/cPi.SUl7MU/rWuSiIDDFayrKk/1tBsSQu4u'),
698-
(pn: 1; bs: '$2a$12$8NJH3LsPrANStV6XtBakCez0cKHXVxmvxIlcz785vxAIZrihHZpeS'),
699-
(pn: 2; bs: '$2a$06$If6bvum7DFjUnE9p2uDeDu0YHzrHM6tf.iqN8.yx.jNN1ILEf7h0i'),
700-
(pn: 2; bs: '$2a$08$Ro0CUfOqk6cXEKf3dyaM7OhSCvnwM9s4wIX9JeLapehKK5YdLxKcm'),
701-
(pn: 2; bs: '$2a$10$WvvTPHKwdBJ3uk0Z37EMR.hLA2W6N9AEBhEgrAOljy2Ae5MtaSIUi'),
702-
(pn: 2; bs: '$2a$12$EXRkfkdmXn2gzds2SSitu.MW9.gAVqa9eLS1//RYtYCmB1eLHg.9q'),
703-
(pn: 3; bs: '$2a$06$.rCVZVOThsIa97pEDOxvGuRRgzG64bvtJ0938xuqzv18d3ZpQhstC'),
704-
(pn: 3; bs: '$2a$08$aTsUwsyowQuzRrDqFflhgekJ8d9/7Z3GV3UcgvzQW3J5zMyrTvlz.'),
705-
(pn: 3; bs: '$2a$10$fVH8e28OQRj9tqiDXs1e1uxpsjN0c7II7YPKXua2NAKYvM6iQk7dq'),
706-
(pn: 3; bs: '$2a$12$D4G5f18o7aMMfwasBL7GpuQWuP3pkrZrOAnqP.bmezbMng.QwJ/pG'),
707-
(pn: 4; bs: '$2a$06$fPIsBO8qRqkjj273rfaOI.HtSV9jLDpTbZn782DC6/t7qT67P6FfO'),
708-
(pn: 4; bs: '$2a$08$Eq2r4G/76Wv39MzSX262huzPz612MZiYHVUJe/OcOql2jo4.9UxTW'),
709-
(pn: 4; bs: '$2a$10$LgfYWkbzEvQ4JakH7rOvHe0y8pHKF9OaFgwUZ2q7W2FFZmZzJYlfS'),
710-
(pn: 4; bs: '$2a$12$WApznUOJfkEGSmYRfnkrPOr466oFDCaj4b6HY3EXGvfxm43seyhgC'));
702+
TestData: array[1..20] of TCryptTestData = (
703+
(PWNum: 0; CryptData: '$2a$06$DCq7YPn5Rq63x1Lad4cll.TV4S6ytwfsfvkgY8jIucDrjc8deX1s.'),
704+
(PWNum: 0; CryptData: '$2a$08$HqWuK6/Ng6sg9gQzbLrgb.Tl.ZHfXLhvt/SgVyWhQqgqcZ7ZuUtye'),
705+
(PWNum: 0; CryptData: '$2a$10$k1wbIrmNyFAPwPVPSVa/zecw2BCEnBwVS2GbrmgzxFUOqW9dk4TCW'),
706+
(PWNum: 0; CryptData: '$2a$12$k42ZFHFWqBp3vWli.nIn8uYyIkbvYRvodzbfbK18SSsY.CsIQPlxO'),
707+
(PWNum: 1; CryptData: '$2a$06$m0CrhHm10qJ3lXRY.5zDGO3rS2KdeeWLuGmsfGlMfOxih58VYVfxe'),
708+
(PWNum: 1; CryptData: '$2a$08$cfcvVd2aQ8CMvoMpP2EBfeodLEkkFJ9umNEfPD18.hUF62qqlC/V.'),
709+
(PWNum: 1; CryptData: '$2a$10$k87L/MF28Q673VKh8/cPi.SUl7MU/rWuSiIDDFayrKk/1tBsSQu4u'),
710+
(PWNum: 1; CryptData: '$2a$12$8NJH3LsPrANStV6XtBakCez0cKHXVxmvxIlcz785vxAIZrihHZpeS'),
711+
(PWNum: 2; CryptData: '$2a$06$If6bvum7DFjUnE9p2uDeDu0YHzrHM6tf.iqN8.yx.jNN1ILEf7h0i'),
712+
(PWNum: 2; CryptData: '$2a$08$Ro0CUfOqk6cXEKf3dyaM7OhSCvnwM9s4wIX9JeLapehKK5YdLxKcm'),
713+
(PWNum: 2; CryptData: '$2a$10$WvvTPHKwdBJ3uk0Z37EMR.hLA2W6N9AEBhEgrAOljy2Ae5MtaSIUi'),
714+
(PWNum: 2; CryptData: '$2a$12$EXRkfkdmXn2gzds2SSitu.MW9.gAVqa9eLS1//RYtYCmB1eLHg.9q'),
715+
(PWNum: 3; CryptData: '$2a$06$.rCVZVOThsIa97pEDOxvGuRRgzG64bvtJ0938xuqzv18d3ZpQhstC'),
716+
(PWNum: 3; CryptData: '$2a$08$aTsUwsyowQuzRrDqFflhgekJ8d9/7Z3GV3UcgvzQW3J5zMyrTvlz.'),
717+
(PWNum: 3; CryptData: '$2a$10$fVH8e28OQRj9tqiDXs1e1uxpsjN0c7II7YPKXua2NAKYvM6iQk7dq'),
718+
(PWNum: 3; CryptData: '$2a$12$D4G5f18o7aMMfwasBL7GpuQWuP3pkrZrOAnqP.bmezbMng.QwJ/pG'),
719+
(PWNum: 4; CryptData: '$2a$06$fPIsBO8qRqkjj273rfaOI.HtSV9jLDpTbZn782DC6/t7qT67P6FfO'),
720+
(PWNum: 4; CryptData: '$2a$08$Eq2r4G/76Wv39MzSX262huzPz612MZiYHVUJe/OcOql2jo4.9UxTW'),
721+
(PWNum: 4; CryptData: '$2a$10$LgfYWkbzEvQ4JakH7rOvHe0y8pHKF9OaFgwUZ2q7W2FFZmZzJYlfS'),
722+
(PWNum: 4; CryptData: '$2a$12$WApznUOJfkEGSmYRfnkrPOr466oFDCaj4b6HY3EXGvfxm43seyhgC'));
711723

712724
function SplitTestVector(const Vector: string):TBCryptBSDTestData;
713725
protected
@@ -6371,14 +6383,14 @@ procedure TestTHash_BCrypt.TestCreateCryptBSDFormat;
63716383
try
63726384
for i := Low(TestData) to High(TestData) do
63736385
begin
6374-
SplitData := SplitTestVector(TestData[i].bs);
6375-
Result := HashInst.GetDigestInCryptFormat(Passwords[TestData[i].pn],
6386+
SplitData := SplitTestVector(TestData[i].CryptData);
6387+
Result := HashInst.GetDigestInCryptFormat(Passwords[TestData[i].PWNum],
63766388
SplitData.Cost.ToString,
63776389
SplitData.Salt,
63786390
False,
63796391
TFormat_BCryptBSD);
63806392

6381-
CheckEquals(TestData[i].bs, Result);
6393+
CheckEquals(TestData[i].CryptData, Result);
63826394
end;
63836395
finally
63846396
HashInst.Free;
@@ -6415,13 +6427,13 @@ procedure TestTHash_BCrypt.TestIsValidPasswordFalse;
64156427
CheckEquals(false, Result, 'Failure at wrong CryptData length');
64166428

64176429
Result := HashInst.IsValidPassword('a',
6418-
TestData[1].bs,
6430+
TestData[1].CryptData,
64196431
TFormat_BCryptBSD);
64206432

64216433
CheckEquals(false, Result, 'Failed to detect wrong password for empty password');
64226434

64236435
Result := HashInst.IsValidPassword('ab',
6424-
TestData[5].bs,
6436+
TestData[5].CryptData,
64256437
TFormat_BCryptBSD);
64266438

64276439
CheckEquals(false, Result, 'Failed to detect wrong password for password a');
@@ -6479,8 +6491,8 @@ procedure TestTHash_BCrypt.TestIsValidPasswordOK;
64796491
try
64806492
for i := Low(TestData) to High(TestData) do
64816493
begin
6482-
Result := HashInst.IsValidPassword(Passwords[TestData[i].pn],
6483-
TestData[i].bs,
6494+
Result := HashInst.IsValidPassword(Passwords[TestData[i].PWNum],
6495+
TestData[i].CryptData,
64846496
TFormat_BCryptBSD);
64856497

64866498
CheckEquals(true, Result, 'Failure at test data index: ' + i.ToString);

0 commit comments

Comments
 (0)