Skip to content

Commit

Permalink
Bug fix - Cost less than 10
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciussanchez committed Apr 14, 2020
1 parent 35b548c commit 4fe20c0
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 15 deletions.
Binary file added samples/Samples.res
Binary file not shown.
12 changes: 1 addition & 11 deletions samples/src/Samples.Main.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ object FrmSamples: TFrmSamples
Top = 0
Width = 649
Height = 226
ActivePage = tabNeedsRehash
ActivePage = tabCompare
Align = alClient
TabOrder = 0
object tabGenerate: TTabSheet
Caption = 'Generate hash'
ExplicitWidth = 281
ExplicitHeight = 165
object Panel1: TPanel
Left = 0
Top = 0
Expand All @@ -35,10 +33,6 @@ object FrmSamples: TFrmSamples
Align = alClient
BevelOuter = bvNone
TabOrder = 0
ExplicitLeft = 232
ExplicitTop = 200
ExplicitWidth = 185
ExplicitHeight = 41
object edtPassword: TLabeledEdit
Left = 12
Top = 23
Expand Down Expand Up @@ -71,8 +65,6 @@ object FrmSamples: TFrmSamples
object tabCompare: TTabSheet
Caption = 'Compare hash'
ImageIndex = 1
ExplicitLeft = 0
ExplicitTop = 28
object lblCompareTrue: TLabel
Left = 93
Top = 114
Expand Down Expand Up @@ -134,8 +126,6 @@ object FrmSamples: TFrmSamples
object tabHashInfo: TTabSheet
Caption = 'Get hash info'
ImageIndex = 2
ExplicitLeft = 0
ExplicitTop = 28
object Label1: TLabel
Left = 12
Top = 64
Expand Down
7 changes: 3 additions & 4 deletions src/BCrypt.Core.pas
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ procedure TBCryptImpl.Encipher(var lr: array of DWORD; const AOffset: Longint);

function TBCryptImpl.FormatPasswordHash(const ASalt, AHash: TBytes; ACost: Byte; AHashType: THashType): string;
var
LHash, LPrefix, LSalt: string;
LHash, LPrefix, LSalt, LCost: string;
begin
case AHashType of
THashType.BSD:
Expand All @@ -222,7 +222,8 @@ function TBCryptImpl.FormatPasswordHash(const ASalt, AHash: TBytes; ACost: Byte;
end;
LSalt := BsdBase64Encode(ASalt, Length(ASalt));
LHash := BsdBase64Encode(AHash, Length(MagicText) * 4 - 1);
Result := Format('$%s$%d$%s%s', [LPrefix, ACost, LSalt, LHash]);
LCost := ACost.ToString.PadLeft(2, '0');
Result := Format('$%s$%s$%s%s', [LPrefix, LCost, LSalt, LHash]);
end;

procedure TBCryptImpl.InitializeKey();
Expand Down Expand Up @@ -313,8 +314,6 @@ function TBCryptImpl.GenerateHash(const APassword: UTF8String; AHashType: THashT
var
LPasswordKey, LSaltBytes, LHash: TBytes;
begin
if (ACost < 10) or (ACost > 30) then
raise EBCrypt.Create('Invalid value for cost. It must be between 10 and 30.');
SetLength(LPasswordKey, Length(APassword) + 1);
Move(APassword[1], LPasswordKey[0], Length(APassword));
LPasswordKey[high(LPasswordKey)] := 0;
Expand Down

0 comments on commit 4fe20c0

Please sign in to comment.