Skip to content

Commit 05ddfad

Browse files
committed
Start of the implementation of IsValidPassword for BCrypt.
1 parent 750d068 commit 05ddfad

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

Source/DECHash.pas

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,24 @@ THash_BCrypt = class(TDECPasswordHash)
11021102
IncProc : TBFIncProc;
11031103
end;
11041104

1105+
/// <summary>
1106+
/// Parts of the BSD/Crypt style password storage for BCrypt
1107+
/// </summary>
1108+
TBCryptBSDData = record
1109+
/// <summary>
1110+
/// Algorithm ID
1111+
/// </summary>
1112+
ID : string;
1113+
/// <summary>
1114+
/// Salt in Crypt encoding
1115+
/// </summary>
1116+
Salt : string;
1117+
/// <summary>
1118+
/// Cost factor
1119+
/// </summary>
1120+
Cost : UInt8;
1121+
end;
1122+
11051123
var
11061124
/// <summary>
11071125
/// The calculated hash value
@@ -1175,6 +1193,11 @@ THash_BCrypt = class(TDECPasswordHash)
11751193
/// Block to store the result in
11761194
/// </param>
11771195
procedure BF_XorBlock(const B1, B2: TBFBlock; var B3: TBFBlock);
1196+
1197+
/// <summary>
1198+
/// Splits a given Crypt/BSD style password record into its parts
1199+
/// </summary>
1200+
function SplitTestVector(const Vector: string):TBCryptBSDData;
11781201
strict protected
11791202
procedure DoInit; override;
11801203
procedure DoTransform(Buffer: PUInt32Array); override;
@@ -1261,6 +1284,23 @@ THash_BCrypt = class(TDECPasswordHash)
12611284
/// </summary>
12621285
class function MaxCost:UInt8;
12631286

1287+
/// <summary>
1288+
/// Checks whether a given password is the correct one for a password
1289+
/// storage "record"/entry in Crypt/BSD format.
1290+
/// </summary>
1291+
/// <param name="Password">
1292+
/// Password to check for validity
1293+
/// </param>
1294+
/// <param name="CryptData">
1295+
/// The data needed to "compare" the password against in Crypt/BSD like
1296+
/// format: $<id>[$<param>=<value>(,<param>=<value>)*][$<salt>[$<hash>]]
1297+
/// </param>
1298+
/// <returns>
1299+
/// True if the password given is correct.
1300+
/// </returns>
1301+
class function IsValidPassword(const Password : string;
1302+
const CryptData : string): Boolean; override;
1303+
12641304
/// <summary>
12651305
/// Processes one chunk of data to be hashed.
12661306
/// </summary>
@@ -5238,6 +5278,12 @@ class function THash_BCrypt.GetCryptParams(
52385278
Result := '$' + Result;
52395279
end;
52405280

5281+
class function THash_BCrypt.IsValidPassword(const Password,
5282+
CryptData: string): Boolean;
5283+
begin
5284+
5285+
end;
5286+
52415287
procedure THash_BCrypt.BF_Encrypt(const BI: TBFBlock; var BO: TBFBlock);
52425288
var
52435289
xl, xr : UInt32;
@@ -5356,6 +5402,16 @@ procedure THash_BCrypt.SetCost(const Value: UInt8);
53565402
raise EDECHashException.CreateFmt(sCostFactorInvalid, [MinCost, MaxCost]);
53575403
end;
53585404

5405+
function THash_BCrypt.SplitTestVector(const Vector: string): TBCryptBSDData;
5406+
var
5407+
Parts : TArray<string>;
5408+
begin
5409+
Parts := Vector.Split(['$'], TStringSplitOptions.ExcludeEmpty);
5410+
Result.ID := Parts[0];
5411+
Result.Cost := Copy(Parts[1], Low(Parts[1]), Length(Parts[1])).ToInteger;
5412+
Result.Salt := Copy(Parts[2], Low(Parts[2]), 22);
5413+
end;
5414+
53595415
{$IFDEF RESTORE_RANGECHECKS}{$R+}{$ENDIF}
53605416
{$IFDEF RESTORE_OVERFLOWCHECKS}{$Q+}{$ENDIF}
53615417

Source/DECHashAuthentication.pas

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,23 @@ TDECPasswordHash = class(TDECHashAuthentication)
798798
SaltIsRaw : Boolean;
799799
Format : TDECFormatClass):string; virtual;
800800

801+
/// <summary>
802+
/// Checks whether a given password is the correct one for a password
803+
/// storage "record"/entry in Crypt/BSD format.
804+
/// </summary>
805+
/// <param name="Password">
806+
/// Password to check for validity
807+
/// </param>
808+
/// <param name="CryptData">
809+
/// The data needed to "compare" the password against in Crypt/BSD like
810+
/// format: $<id>[$<param>=<value>(,<param>=<value>)*][$<salt>[$<hash>]]
811+
/// </param>
812+
/// <returns>
813+
/// True if the password given is correct.
814+
/// </returns>
815+
class function IsValidPassword(const Password : string;
816+
const CryptData : string): Boolean; virtual;
817+
801818
// /// <summary>
802819
// /// Calculates a passwort hash for the given password and returns it in
803820
// /// a BSDCrypt compatible format. This method only works for those hash
@@ -1588,4 +1605,10 @@ class function TDECPasswordHash.GetDigestInCryptFormat(
15881605
SetLength(SaltBytes, 0);
15891606
end;
15901607

1608+
class function TDECPasswordHash.IsValidPassword(const Password : string;
1609+
const CryptData : string): Boolean;
1610+
begin
1611+
Result := false;
1612+
end;
1613+
15911614
end.

Unit Tests/Tests/TestDECHash.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ TBCryptBSDTestData = record
676676
Cost : UInt8;
677677
end;
678678

679-
function SplitTestVector(Vector: string):TBCryptBSDTestData;
679+
function SplitTestVector(const Vector: string):TBCryptBSDTestData;
680680
protected
681681
procedure ConfigHashClass(aHashClass: TDECHash; aIdxTestData:Integer); override;
682682
public
@@ -6296,7 +6296,7 @@ procedure TestTHash_BCrypt.SetUp;
62966296
lDataRow.AddInputVector('~!@#$%^&*() ~!@#$%^&*()PNBFRD');
62976297
end;
62986298

6299-
function TestTHash_BCrypt.SplitTestVector(Vector: string): TBCryptBSDTestData;
6299+
function TestTHash_BCrypt.SplitTestVector(const Vector: string): TBCryptBSDTestData;
63006300
var
63016301
Parts : TArray<string>;
63026302
begin

0 commit comments

Comments
 (0)